Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.ff4j.elastic;
/*
* #%L
* ff4j-store-elastic
* %%
* Copyright (C) 2013 - 2016 FF4J
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.ff4j.audit.Event;
import org.ff4j.audit.EventConstants;
import org.ff4j.audit.EventQueryDefinition;
import org.ff4j.core.Feature;
import org.ff4j.property.Property;
import io.searchbox.client.JestResult;
import io.searchbox.core.Delete;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.SearchResult.Hit;
import io.searchbox.core.Update;
import io.searchbox.indices.DeleteIndex;
import io.searchbox.indices.Flush;
/**
* Helper to create Jest queries.
*
* @author Cedrick LUNVEN (@clunven)
* @author Andre BLASZCZYK ([email protected])
*/
public class ElasticQueryBuilder {
/** Connection. */
private final ElasticConnection connection;
/**
* Initialization of the builder with a dedicated connection.
*
* @param conn
* current elastic collection.
*/
public ElasticQueryBuilder(ElasticConnection conn) {
this.connection = conn;
}
public Flush queryFlushIndex() {
return new Flush.Builder().addIndex(connection.getIndexName()).build();
}
/**
* Syntaxic sugar to have query on feature.
*
* @param uid
* target feature uid
* @return query for JEST
*/
public Search queryGetFeatureById(String uid) {
SearchSourceBuilder source = new SearchSourceBuilder();
source.query(QueryBuilders.matchQuery("uid", uid));
return new Search.Builder(source.toString()).addIndex(connection.getIndexName())
.addType(ElasticConstants.TYPE_FEATURE).build();
}
public Search getGroupByGroupName(String groupName) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("group", groupName));
return new Search.Builder(searchSourceBuilder.toString()) //
.addIndex(connection.getIndexName()) //
.addType(ElasticConstants.TYPE_FEATURE).build();
}
public Index queryCreateFeature(Feature fp) {
return new Index.Builder(fp).index(connection.getIndexName()).type(ElasticConstants.TYPE_FEATURE).refresh(true)
.build();
}
public Search queryReadAllFeatures() {
return new Search.Builder(new SearchSourceBuilder().toString()).addIndex(connection.getIndexName())
.addType(ElasticConstants.TYPE_FEATURE).build();
}
public Search queryReadAllFeatures(Integer totalCount) {
return new Search.Builder(new SearchSourceBuilder().size(totalCount).toString()).addIndex(connection.getIndexName())
.addType(ElasticConstants.TYPE_FEATURE).build();
}
public Delete queryDeleteFeature(String uid) {
return new Delete.Builder(uid).index(connection.getIndexName()).type(ElasticConstants.TYPE_FEATURE)
.id(getFeatureTechId(uid)).refresh(true).build();
}
public Index queryUpdateFeature(Feature fp) {
return new Index.Builder(fp).index(connection.getIndexName()).type(ElasticConstants.TYPE_FEATURE)
.id(getFeatureTechId(fp.getUid())).refresh(true).build();
}
public String getFeatureTechId(String uid) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("uid", uid));
Search search = new Search.Builder(searchSourceBuilder.toString()) //
.addIndex(connection.getIndexName()) //
.addType(ElasticConstants.TYPE_FEATURE) //
.build();
// feature existence must have been checked before (technical function)
@SuppressWarnings("rawtypes")
List> items = connection.search(search).getHits(Map.class);
if (null != items && !items.isEmpty()) {
return connection.search(search).getHits(Map.class).get(0).source.get(JestResult.ES_METADATA_ID).toString();
}
return null;
}
@SuppressWarnings({ "rawtypes" })
public Set getFeatureTechIdByGroup(String groupName) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("group", groupName));
Search search = new Search.Builder(searchSourceBuilder.toString()) //
.addIndex(connection.getIndexName()) //
.addType(ElasticConstants.TYPE_FEATURE) //
.build();
SearchResult result = connection.search(search, true);
Set metadatas = new HashSet();
if (null != result && result.isSucceeded()) {
List> features = result.getHits(Map.class);
for (Hit