本文整理汇总了Java中org.elasticsearch.action.search.SearchRequestBuilder.addAggregation方法的典型用法代码示例。如果您正苦于以下问题:Java SearchRequestBuilder.addAggregation方法的具体用法?Java SearchRequestBuilder.addAggregation怎么用?Java SearchRequestBuilder.addAggregation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.elasticsearch.action.search.SearchRequestBuilder
的用法示例。
在下文中一共展示了SearchRequestBuilder.addAggregation方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: selectMatchAll
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
public String selectMatchAll(String indexs,String types,String field,String value){
try {
if(client==null){
init();
}
SearchRequestBuilder request = client.prepareSearch(indexs.split(",")).setTypes(types.split(","));
request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
request.setQuery(QueryBuilders.matchQuery(field, value));
request.highlighter(new HighlightBuilder().field(field));
request.addAggregation(AggregationBuilders.terms("data").field(field+".keyword"));
request.setExplain(false);
SearchResponse response = request.get();
return response.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
示例2: selectTermAll
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
public String selectTermAll(String indexs,String types,String field,String value){
try {
if(client==null){
init();
}
SearchRequestBuilder request = client.prepareSearch(indexs.split(",")).setTypes(types.split(","));
request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
request.setQuery(QueryBuilders.termQuery(field, value));
request.highlighter(new HighlightBuilder().field(field));
request.addAggregation(AggregationBuilders.terms("data").field(field+".keyword"));
request.setExplain(false);
SearchResponse response = request.get();
return response.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
示例3: explanFields
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
private void explanFields(SearchRequestBuilder request, List<Field> fields, AggregationBuilder groupByAgg) throws SqlParseException {
for (Field field : fields) {
if (field instanceof MethodField) {
if (field.getName().equals("script")) {
request.addStoredField(field.getAlias());
DefaultQueryAction defaultQueryAction = new DefaultQueryAction(client, select);
defaultQueryAction.intialize(request);
List<Field> tempFields = Lists.newArrayList(field);
defaultQueryAction.setFields(tempFields);
continue;
}
AggregationBuilder makeAgg = aggMaker.makeFieldAgg((MethodField) field, groupByAgg);
if (groupByAgg != null) {
groupByAgg.subAggregation(makeAgg);
} else {
request.addAggregation(makeAgg);
}
} else if (field instanceof Field) {
request.addStoredField(field.getName());
} else {
throw new SqlParseException("it did not support this field method " + field);
}
}
}
示例4: buildQuery
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
private SearchRequestBuilder buildQuery(EnumSet<ElasticsearchDocumentType> elementType, boolean includeAggregations) {
if (QUERY_LOGGER.isTraceEnabled()) {
QUERY_LOGGER.trace("searching for: " + toString());
}
List<QueryBuilder> filters = getFilters(elementType);
QueryBuilder query = createQuery(getParameters());
query = scoringStrategy.updateQuery(query);
QueryBuilder filterBuilder = getFilterBuilder(filters);
String[] indicesToQuery = getIndexSelectionStrategy().getIndicesToQuery(this, elementType);
if (QUERY_LOGGER.isTraceEnabled()) {
QUERY_LOGGER.trace("indicesToQuery: %s", Joiner.on(", ").join(indicesToQuery));
}
SearchRequestBuilder searchRequestBuilder = getClient()
.prepareSearch(indicesToQuery)
.setTypes(Elasticsearch5SearchIndex.ELEMENT_TYPE)
.setQuery(QueryBuilders.boolQuery().must(query).filter(filterBuilder))
.storedFields(
Elasticsearch5SearchIndex.ELEMENT_TYPE_FIELD_NAME,
Elasticsearch5SearchIndex.EXTENDED_DATA_ELEMENT_ID_FIELD_NAME,
Elasticsearch5SearchIndex.EXTENDED_DATA_TABLE_NAME_FIELD_NAME,
Elasticsearch5SearchIndex.EXTENDED_DATA_TABLE_ROW_ID_FIELD_NAME
);
if (includeAggregations) {
List<AggregationBuilder> aggs = getElasticsearchAggregations(getAggregations());
for (AggregationBuilder aggregationBuilder : aggs) {
searchRequestBuilder.addAggregation(aggregationBuilder);
}
}
applySort(searchRequestBuilder);
return searchRequestBuilder;
}
示例5: addAggregations
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
private static void addAggregations(
SearchRequestBuilder searchRequestBuilder,
List<Map<String, String>> facets) {
long startTime = System.currentTimeMillis();
ProjectLogger.log("ElasticSearchUtil addAggregations method started at ==" +startTime, LoggerEnum.PERF_LOG);
Map<String, String> map = facets.get(0);
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (JsonKey.DATE_HISTOGRAM.equalsIgnoreCase(value)) {
searchRequestBuilder
.addAggregation(AggregationBuilders
.dateHistogram(key)
.field(key + RAW_APPEND)
.dateHistogramInterval(DateHistogramInterval.days(1)));
} else if (null == value) {
searchRequestBuilder
.addAggregation(AggregationBuilders.terms(key).field(key + RAW_APPEND));
}
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
ProjectLogger.log("ElasticSearchUtil addAggregations method end at ==" +stopTime+" ,Total time elapsed = "+elapsedTime, LoggerEnum.PERF_LOG);
}
示例6: getVertexPropertyCountByValue
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
@Override
public Map<Object, Long> getVertexPropertyCountByValue(Graph graph, String propertyName, Authorizations authorizations) {
TermQueryBuilder elementTypeFilterBuilder = new TermQueryBuilder(ELEMENT_TYPE_FIELD_NAME, ElasticsearchDocumentType.VERTEX.getKey());
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
.must(QueryBuilders.matchAllQuery())
.filter(elementTypeFilterBuilder);
SearchRequestBuilder q = getClient().prepareSearch(getIndexNamesAsArray(graph))
.setQuery(queryBuilder)
.setSize(0);
for (String p : getAllMatchingPropertyNames(graph, propertyName, authorizations)) {
String countAggName = "count-" + p;
PropertyDefinition propertyDefinition = getPropertyDefinition(graph, p);
p = p.replace(".", FIELDNAME_DOT_REPLACEMENT);
if (propertyDefinition != null && propertyDefinition.getTextIndexHints().contains(TextIndexHint.EXACT_MATCH)) {
p = p + EXACT_MATCH_PROPERTY_NAME_SUFFIX;
}
TermsAggregationBuilder countAgg = AggregationBuilders
.terms(countAggName)
.field(p)
.size(500000);
q = q.addAggregation(countAgg);
}
if (ElasticsearchSearchQueryBase.QUERY_LOGGER.isTraceEnabled()) {
ElasticsearchSearchQueryBase.QUERY_LOGGER.trace("query: %s", q);
}
SearchResponse response = getClient().search(q.request()).actionGet();
Map<Object, Long> results = new HashMap<>();
for (Aggregation agg : response.getAggregations().asList()) {
Terms propertyCountResults = (Terms) agg;
for (Terms.Bucket propertyCountResult : propertyCountResults.getBuckets()) {
String mapKey = ((String) propertyCountResult.getKey()).toLowerCase();
Long previousValue = results.get(mapKey);
if (previousValue == null) {
previousValue = 0L;
}
results.put(mapKey, previousValue + propertyCountResult.getDocCount());
}
}
return results;
}
示例7: populateBuffer
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
/** run a query on ES to populate the internal buffer **/
protected void populateBuffer() {
Date now = new Date();
LOG.info("{} Populating buffer with nextFetchDate <= {}", logIdprefix,
now);
QueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(
"nextFetchDate").lte(now);
SearchRequestBuilder srb = client.prepareSearch(indexName)
.setTypes(docType).setSearchType(SearchType.QUERY_THEN_FETCH)
.setQuery(rangeQueryBuilder).setFrom(0).setSize(0)
.setExplain(false);
TermsBuilder aggregations = AggregationBuilders.terms("partition")
.field(partitionField).size(maxBucketNum);
TopHitsBuilder tophits = AggregationBuilders.topHits("docs")
.setSize(maxURLsPerBucket).setExplain(false);
// sort within a bucket
if (StringUtils.isNotBlank(bucketSortField)) {
FieldSortBuilder sorter = SortBuilders.fieldSort(bucketSortField)
.order(SortOrder.ASC);
tophits.addSort(sorter);
}
aggregations.subAggregation(tophits);
// sort between buckets
if (StringUtils.isNotBlank(totalSortField)) {
MinBuilder minBuilder = AggregationBuilders.min("top_hit").field(
totalSortField);
aggregations.subAggregation(minBuilder);
aggregations.order(Terms.Order.aggregation("top_hit", true));
}
srb.addAggregation(aggregations);
// https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-preference.html
// _shards:2,3
if (shardID != -1) {
srb.setPreference("_shards:" + shardID);
}
// dump query to log
LOG.debug("{} ES query {}", logIdprefix, srb.toString());
timeStartESQuery = System.currentTimeMillis();
isInESQuery.set(true);
srb.execute(this);
}
示例8: buildSearchRequest
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
private SearchRequestBuilder buildSearchRequest (EsQueryDo esQueryObj) throws EsException {
if (Check.NuNStrStrict(esClientFactory.getIndexs(esQueryObj.getIndexName()))) {
throw new EsException("没有指定要搜索的索引名称(indexName)");
}
for (ThreadPoolStats.Stats stats : esClientFactory.getClient().threadPool().stats()) {
logger.info(JSON.toJSONString(stats));
}
//加载要搜索索引
SearchRequestBuilder searchRequestBuilder = esClientFactory.getClient().prepareSearch(esClientFactory.getIndexs(esQueryObj.getIndexName()));
//由spring从配置加载要搜索的index的类型
searchRequestBuilder.setTypes(esQueryObj.getTypeName());
//由spring从配置加载要搜索的类型
searchRequestBuilder.setSearchType(SearchType.fromId(esQueryObj.getSearchType()));
//查询可以为null
searchRequestBuilder.setQuery(esQueryObj.getQueryBuilder());
if (!Check.NuNCollection(esQueryObj.getSortBuilders())) {
for (SortBuilder sortBuilder : esQueryObj.getSortBuilders()) {
searchRequestBuilder.addSort(sortBuilder);
}
}
if (!Check.NuNCollection(esQueryObj.getAggregationBuilders())) {
for (AbstractAggregationBuilder aggregationBuilder : esQueryObj.getAggregationBuilders()) {
searchRequestBuilder.addAggregation(aggregationBuilder);
}
}
//设置高亮域
if (esQueryObj.isHighLigth()) {
if (!Check.NuNObject(esQueryObj.highLigthFields())) {
for (String hlFieldName : esQueryObj.highLigthFields()) {
searchRequestBuilder.addHighlightedField(hlFieldName).setHighlighterPreTags(esQueryObj.getHighLigthPreTag())
.setHighlighterPostTags(esQueryObj.getHighLigthPostTag());
}
}
}
//分页
searchRequestBuilder.setFrom(esQueryObj.getFromIndex()).setSize(esQueryObj.getSize());
searchRequestBuilder.setExplain(esQueryObj.isExplain());
return searchRequestBuilder;
}