Java Code Examples for org.elasticsearch.action.search.SearchRequestBuilder#addAggregation()

The following examples show how to use org.elasticsearch.action.search.SearchRequestBuilder#addAggregation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticsearchTransportFactory.java    From database-transform-tool with Apache License 2.0 7 votes vote down vote up
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;
}
 
Example 2
Source File: ElasticsearchExtendTransportFactory.java    From database-transform-tool with Apache License 2.0 7 votes vote down vote up
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;
}
 
Example 3
Source File: EsEntityIndexImpl.java    From usergrid with Apache License 2.0 6 votes vote down vote up
private long getEntitySizeAggregation( final SearchRequestBuilder builder ) {
    final String key = "entitySize";
    SumBuilder sumBuilder = new SumBuilder(key);
    sumBuilder.field("entitySize");
    builder.addAggregation(sumBuilder);

    Observable<Number> o = Observable.from(builder.execute())
        .map(response -> {
            Sum aggregation = (Sum) response.getAggregations().get(key);
            if(aggregation == null){
                return -1;
            }else{
                return aggregation.getValue();
            }
        });
    Number val =   ObservableTimer.time(o,aggregationTimer).toBlocking().lastOrDefault(-1);
    return val.longValue();
}
 
Example 4
Source File: GroupConditionBuilder.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
/**
 * 封装查询请求的聚合条件
 *
 * @param rootTermsBuilder     聚合根
 * @param termsBuilder         当前聚合
 * @param functions            聚合函数集
 * @param searchRequestBuilder 聚合功能查询请求
 */
private void aggregationFunction(AggregationBuilder rootTermsBuilder, AggregationBuilder termsBuilder,
                                 final List<FunctionCondition> functions,
                                 final SearchRequestBuilder searchRequestBuilder) {

    for (FunctionCondition functionCondition : functions) {

        AbstractAggregationBuilder aggregation = getAggregationFunction(functionCondition);

        if (termsBuilder != null) {
            termsBuilder.subAggregation(aggregation);
        } else {
            searchRequestBuilder.addAggregation(aggregation);
        }
    }
    if (termsBuilder != null) {
        searchRequestBuilder.addAggregation(rootTermsBuilder);
    }
}
 
Example 5
Source File: SearchQueryServiceImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private SearchResponse executeSearch(final RepositoryQueryBuilder repoQuery,
                                     final String[] searchableIndexes,
                                     final List<AggregationBuilder> aggregations,
                                     @Nullable final QueryBuilder postFilter)
{
  SearchRequestBuilder searchRequestBuilder = client.get().prepareSearch(searchableIndexes)
      .setTypes(TYPE)
      .setQuery(repoQuery)
      .setFrom(0)
      .setSize(0)
      .setProfile(profile)
      .setTrackScores(true);

  for (AggregationBuilder aggregation : aggregations) {
    searchRequestBuilder.addAggregation(aggregation);
  }

  if (repoQuery.sort != null) {
    for (SortBuilder entry : repoQuery.sort) {
      searchRequestBuilder.addSort(entry);
    }
  }

  if (postFilter != null) {
    searchRequestBuilder.setPostFilter(postFilter);
  }

  if (repoQuery.timeout != null) {
    searchRequestBuilder.setTimeout(repoQuery.timeout.getSeconds() + "s");
  }

  SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();

  if (profile) {
    logProfileResults(searchResponse);
  }

  return searchResponse;
}
 
Example 6
Source File: ElasticsearchHelper.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * Adds facet field aggregations
 *
 * @param facetFieldsList facet field list
 * @param searchRequestBuilder search request builder
 *
 * @return the specified search request builder with the aggregations applied to it
 */
public SearchRequestBuilder addFacetFieldAggregations(Set<String> facetFieldsList, SearchRequestBuilder searchRequestBuilder)
{
    if (CollectionUtils.isNotEmpty(facetFieldsList))
    {
        if (facetFieldsList.contains(TAG_FACET))
        {
            searchRequestBuilder.addAggregation(AggregationBuilders.nested(TAG_FACET_AGGS, NESTED_BDEFTAGS_PATH).subAggregation(
                AggregationBuilders.terms(TAGTYPE_CODE_AGGREGATION).field(BDEF_TAGTYPE_CODE_FIELD).subAggregation(
                    AggregationBuilders.terms(TAGTYPE_NAME_AGGREGATION).field(BDEF_TAGTYPE_NAME_FIELD).subAggregation(
                        AggregationBuilders.terms(TAG_CODE_AGGREGATION).field(BDEF_TAG_CODE_FIELD)
                            .subAggregation(AggregationBuilders.terms(TAG_NAME_AGGREGATION).field(BDEF_TAG_NAME_FIELD))))));

            searchRequestBuilder.addAggregation(AggregationBuilders.terms(TAG_TYPE_FACET_AGGS).field(TAGTYPE_CODE_FIELD_TAG_INDEX).subAggregation(
                AggregationBuilders.terms(TAGTYPE_NAME_AGGREGATION).field(TAGTYPE_NAME_FIELD_TAG_INDEX).subAggregation(
                    AggregationBuilders.terms(TAG_CODE_AGGREGATION).field(TAG_CODE_FIELD_TAG_INDEX)
                        .subAggregation(AggregationBuilders.terms(TAG_NAME_AGGREGATION).field(TAG_NAME_FIELD_TAG_INDEX)))));

        }
        if (facetFieldsList.contains(RESULT_TYPE_FACET))
        {
            searchRequestBuilder.addAggregation(AggregationBuilders.terms(RESULT_TYPE_AGGS).field(RESULT_TYPE_FIELD));
        }
    }

    return searchRequestBuilder;
}
 
Example 7
Source File: QuestionElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
protected SearchRequestBuilder addSearchAggregation(SearchRequestBuilder searchRequestBuilder, String field) {
    if(useAggregation) {
        return searchRequestBuilder.addAggregation(
                AggregationBuilders.terms(AGGREGATION_NAME).field(field).size(serverConfigurationService.getInt("samigo.search.maxResults",50))
                        .subAggregation(AggregationBuilders.topHits(AGGREGATION_TOP_HITS).setSize(1).addFieldDataField("assessmentId").addFieldDataField("site").addFieldDataField("questionPoolId").addFieldDataField("typeId").addFieldDataField("tags").addFieldDataField("qText")));
        //.subAggregation(AggregationBuilders.topHits(AGGREGATION_TOP_HITS).setSize(1).addFieldDataField("origin")));

    }
    return searchRequestBuilder;
}
 
Example 8
Source File: DistributedTableMetadataManager.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
private void evaluateDoubleAggregation(String table, String field, FieldType type, SearchRequestBuilder query) {
    logger.info("table:{} field:{} type:{} aggregationType:{}", table, field, type,
            "percentile"
    );
    query.addAggregation(AggregationBuilders.percentiles(field)
            .field(field)
            .percentiles(10, 20, 30, 40, 50, 60, 70, 80, 90, 100));
    query.addAggregation(AggregationBuilders.cardinality("_" + field)
            .field(field)
            .precisionThreshold(PRECISION_THRESHOLD));
}
 
Example 9
Source File: QuestionElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
protected SearchRequestBuilder addSearchAggregation(SearchRequestBuilder searchRequestBuilder, String field) {
    if(useAggregation) {
        return searchRequestBuilder.addAggregation(
                AggregationBuilders.terms(AGGREGATION_NAME).field(field).size(serverConfigurationService.getInt("samigo.search.maxResults",50))
                        .subAggregation(AggregationBuilders.topHits(AGGREGATION_TOP_HITS).setSize(1).addFieldDataField("assessmentId").addFieldDataField("site").addFieldDataField("questionPoolId").addFieldDataField("typeId").addFieldDataField("tags").addFieldDataField("qText")));
        //.subAggregation(AggregationBuilders.topHits(AGGREGATION_TOP_HITS).setSize(1).addFieldDataField("origin")));

    }
    return searchRequestBuilder;
}
 
Example 10
Source File: ElasticSearchHelper.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
/**
 * This method adds aggregations to the incoming SearchRequestBuilder object
 *
 * @param searchRequestBuilder which will be updated with facets if any present
 * @param facets Facets provide aggregated data based on a search query
 * @return SearchRequestBuilder
 */
public static SearchRequestBuilder addAggregations(
    SearchRequestBuilder searchRequestBuilder, List<Map<String, String>> facets) {
  long startTime = System.currentTimeMillis();
  ProjectLogger.log(
      "ElasticSearchHelper:addAggregations: method started at ==" + startTime,
      LoggerEnum.PERF_LOG.name());
  if (facets != null && !facets.isEmpty()) {
    Map<String, String> map = facets.get(0);
    if (!MapUtils.isEmpty(map)) {
      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 elapsedTime = calculateEndTime(startTime);
    ProjectLogger.log(
        "ElasticSearchHelper:addAggregations method end =="
            + " ,Total time elapsed = "
            + elapsedTime,
        LoggerEnum.PERF_LOG.name());
  }

  return searchRequestBuilder;
}
 
Example 11
Source File: EsQuery.java    From AsuraFramework with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 12
Source File: ESQueryState.java    From sql4es with Apache License 2.0 4 votes vote down vote up
/**
 * Builds the Elasticsearch query object based on the parsed information from the SQL query
 * @param searchReq
 * @param info
 */
private void buildQuery(SearchRequestBuilder searchReq, ParseResult info) {
	String[] types = new String[info.getSources().size()];
	for(int i=0; i<info.getSources().size(); i++) types[i] = info.getSources().get(i).getSource(); 
	SearchRequestBuilder req = searchReq.setTypes(types);
	
	// add filters and aggregations
	if(info.getAggregation() != null){
		// when aggregating the query must be a query and not a filter
		if(info.getQuery() != null)	req.setQuery(info.getQuery());
		req.addAggregation(info.getAggregation());
		
	// ordering does not work on aggregations (has to be done in client)
	}else if(info.getQuery() != null){
		if(info.getRequestScore()) req.setQuery(info.getQuery()); // use query instead of filter to get a score
		else req.setPostFilter(info.getQuery());
		
		// add order
		for(OrderBy ob : info.getSorts()){
			req.addSort(ob.getField(), ob.getOrder());
		}
	} else req.setQuery(QueryBuilders.matchAllQuery());
	
	this.limit = info.getLimit();
	if(splitRS) maxRowsRS = fetchSize;
	
	//System.out.println("fetch: "+fetchSize+" limit: "+limit+" split: "+splitRS);
	
	// add limit and determine to use scroll
	if(info.getAggregation() != null) {
		req = req.setSize(0);
	} else{
		if(limit > 0 && limit < fetchSize){ // no scroll needed
			req.setSize(limit);
		} else{ // use scrolling
			req.setSize(fetchSize);
			req.setScroll(new TimeValue(Utils.getIntProp(props, Utils.PROP_SCROLL_TIMEOUT_SEC, 60)*1000));
			if (info.getSorts().isEmpty()) req.addSort("_doc", SortOrder.ASC); // scroll works fast with sort on _doc
		}
	}
	
	// use query cache when this was indicated in FROM clause
	if(info.getUseCache()) req.setRequestCache(true);
	req.setTimeout(TimeValue.timeValueMillis(Utils.getIntProp(props, Utils.PROP_QUERY_TIMEOUT_MS, 10000)));
}
 
Example 13
Source File: Elasticsearch7SearchIndex.java    From vertexium with Apache License 2.0 4 votes vote down vote up
@Override
@Deprecated
public Map<Object, Long> getVertexPropertyCountByValue(Graph graph, String propertyName, Authorizations authorizations) {
    if (shouldRefreshIndexOnQuery()) {
        indexRefreshTracker.refresh(client);
    }

    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)
        .setTrackTotalHits(true);

    for (String p : getAllMatchingPropertyNames(graph, propertyName, authorizations)) {
        String countAggName = "count-" + p;
        PropertyDefinition propertyDefinition = getPropertyDefinition(graph, p);
        p = replaceFieldnameDots(p);
        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 = checkForFailures(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;
}
 
Example 14
Source File: ElasticsearchSearchQueryBase.java    From vertexium with Apache License 2.0 4 votes vote down vote up
private SearchRequestBuilder buildQuery(EnumSet<ElasticsearchDocumentType> elementType, FetchHints fetchHints, boolean includeAggregations) {
    if (QUERY_LOGGER.isTraceEnabled()) {
        QUERY_LOGGER.trace("searching for: " + toString());
    }
    List<QueryBuilder> filters = getFilters(elementType, fetchHints);
    QueryBuilder query = createQuery(getParameters());

    QueryBuilder filterBuilder = getFilterBuilder(filters, fetchHints);
    String[] indicesToQuery = getIndexSelectionStrategy().getIndicesToQuery(this, elementType);
    if (QUERY_LOGGER.isTraceEnabled()) {
        QUERY_LOGGER.trace("indicesToQuery: %s", Joiner.on(", ").join(indicesToQuery));
    }
    if (getSearchIndex().shouldRefreshIndexOnQuery()) {
        getSearchIndex().getIndexRefreshTracker().refresh(client, indicesToQuery);
    }

    SearchRequestBuilder searchRequestBuilder = getClient()
        .prepareSearch(indicesToQuery)
        .setQuery(QueryBuilders.boolQuery().must(query).filter(filterBuilder))
        .storedFields(
            Elasticsearch7SearchIndex.ELEMENT_ID_FIELD_NAME,
            Elasticsearch7SearchIndex.ELEMENT_TYPE_FIELD_NAME,
            Elasticsearch7SearchIndex.EXTENDED_DATA_TABLE_NAME_FIELD_NAME,
            Elasticsearch7SearchIndex.EXTENDED_DATA_TABLE_ROW_ID_FIELD_NAME
        );
    if (fetchHints.equals(FetchHints.NONE)) {
        searchRequestBuilder.storedFields(
            Elasticsearch7SearchIndex.OUT_VERTEX_ID_FIELD_NAME,
            Elasticsearch7SearchIndex.IN_VERTEX_ID_FIELD_NAME,
            Elasticsearch7SearchIndex.EDGE_LABEL_FIELD_NAME
        );
    }
    if (getParameters().getMinScore() != null) {
        searchRequestBuilder.setMinScore(getParameters().getMinScore().floatValue());
    }
    if (includeAggregations) {
        List<AggregationBuilder> aggs = getElasticsearchAggregations(getAggregations());
        for (AggregationBuilder aggregationBuilder : aggs) {
            searchRequestBuilder.addAggregation(aggregationBuilder);
        }
    }

    applySort(searchRequestBuilder);

    return searchRequestBuilder;
}
 
Example 15
Source File: Elasticsearch5SearchIndex.java    From vertexium with Apache License 2.0 4 votes vote down vote up
@Override
@Deprecated
public Map<Object, Long> getVertexPropertyCountByValue(Graph graph, String propertyName, Authorizations authorizations) {
    if (shouldRefreshIndexOnQuery()) {
        indexRefreshTracker.refresh(client);
    }

    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 = replaceFieldnameDots(p);
        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 = checkForFailures(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;
}
 
Example 16
Source File: ElasticsearchSearchQueryBase.java    From vertexium with Apache License 2.0 4 votes vote down vote up
private SearchRequestBuilder buildQuery(EnumSet<ElasticsearchDocumentType> elementType, FetchHints fetchHints, boolean includeAggregations) {
    if (QUERY_LOGGER.isTraceEnabled()) {
        QUERY_LOGGER.trace("searching for: " + toString());
    }
    List<QueryBuilder> filters = getFilters(elementType, fetchHints);
    QueryBuilder query = createQuery(getParameters());

    QueryBuilder filterBuilder = getFilterBuilder(filters, fetchHints);
    String[] indicesToQuery = getIndexSelectionStrategy().getIndicesToQuery(this, elementType);
    if (QUERY_LOGGER.isTraceEnabled()) {
        QUERY_LOGGER.trace("indicesToQuery: %s", Joiner.on(", ").join(indicesToQuery));
    }
    if (getSearchIndex().shouldRefreshIndexOnQuery()) {
        getSearchIndex().getIndexRefreshTracker().refresh(client, indicesToQuery);
    }

    SearchRequestBuilder searchRequestBuilder = getClient()
        .prepareSearch(indicesToQuery)
        .setTypes(getSearchIndex().getIdStrategy().getType())
        .setQuery(QueryBuilders.boolQuery().must(query).filter(filterBuilder))
        .storedFields(
            Elasticsearch5SearchIndex.ELEMENT_ID_FIELD_NAME,
            Elasticsearch5SearchIndex.ELEMENT_TYPE_FIELD_NAME,
            Elasticsearch5SearchIndex.EXTENDED_DATA_TABLE_NAME_FIELD_NAME,
            Elasticsearch5SearchIndex.EXTENDED_DATA_TABLE_ROW_ID_FIELD_NAME
        );
    if (fetchHints.equals(FetchHints.NONE)) {
        searchRequestBuilder.storedFields(
            Elasticsearch5SearchIndex.OUT_VERTEX_ID_FIELD_NAME,
            Elasticsearch5SearchIndex.IN_VERTEX_ID_FIELD_NAME,
            Elasticsearch5SearchIndex.EDGE_LABEL_FIELD_NAME
        );
    }
    if (getParameters().getMinScore() != null) {
        searchRequestBuilder.setMinScore(getParameters().getMinScore().floatValue());
    }
    if (includeAggregations) {
        List<AggregationBuilder> aggs = getElasticsearchAggregations(getAggregations());
        for (AggregationBuilder aggregationBuilder : aggs) {
            searchRequestBuilder.addAggregation(aggregationBuilder);
        }
    }

    applySort(searchRequestBuilder);

    return searchRequestBuilder;
}
 
Example 17
Source File: AggregationQueryAction.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
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")) {
                //question addStoredField()是什么鬼?
                request.addStoredField(field.getAlias());

                /*
                zhongshu-comment 将request传进去defaultQueryAction对象是为了调用setFields()中的这一行代码:request.setFetchSource(),
                                 给request设置include字段和exclude字段
                 */
                DefaultQueryAction defaultQueryAction = new DefaultQueryAction(client, select);
                defaultQueryAction.intialize(request);
                List<Field> tempFields = Lists.newArrayList(field);
                defaultQueryAction.setFields(tempFields);

                /*
                 zhongshu-comment 因为field.getName().equals("script")的那些字段一般都是作为维度而不是统计指标、度量metric,
                                    所以就要continue,不能继续下边的创建agg
                */
                continue;
            }

            //modify by xzb 类型无法转换,只能新增一个 makeMovingFieldAgg方法
            if (groupByAgg != null) {
                if (field.getName().startsWith("rollingstd") || field.getName().startsWith("movingavg")) {
                    groupByAgg.subAggregation(aggMaker.makeMovingFieldAgg((MethodField) field, groupByAgg));
                } else {
                    groupByAgg.subAggregation(aggMaker.makeFieldAgg((MethodField) field, groupByAgg));
                }
            } else {
                //question 不懂为什么将一个null的agg加到request中,这应该是dsl语法问题,先不需要深究
                request.addAggregation(aggMaker.makeFieldAgg((MethodField) field, groupByAgg));
            }
        } else if (field instanceof Field) {

            //question 为什么Filed类型的字段不需要像MethodField类型字段一样设置include、exclude字段:request.setFetchSource()
            request.addStoredField(field.getName());
        } else {
            throw new SqlParseException("it did not support this field method " + field);
        }
    }
}
 
Example 18
Source File: StatsAction.java    From foxtrot with Apache License 2.0 4 votes vote down vote up
@Override
public SearchRequestBuilder getRequestBuilder(StatsRequest parameter) {
    SearchRequestBuilder searchRequestBuilder;
    try {
        final String table = parameter.getTable();
        searchRequestBuilder = getConnection().getClient()
                .prepareSearch(ElasticsearchUtils.getIndices(table, parameter))
                .setTypes(ElasticsearchUtils.DOCUMENT_TYPE_NAME)
                .setIndicesOptions(Utils.indicesOptions())
                .setQuery(new ElasticSearchQueryGenerator().genFilter(parameter.getFilters()))
                .setSize(QUERY_SIZE);
        AbstractAggregationBuilder percentiles = null;
        final String field = getParameter().getField();
        boolean isNumericField = Utils.isNumericField(getTableMetadataManager(), table, field);
        final AbstractAggregationBuilder extendedStats;
        if (isNumericField) {
            if (!AnalyticsRequestFlags.hasFlag(parameter.getFlags(),
                                               AnalyticsRequestFlags.STATS_SKIP_PERCENTILES)) {
                percentiles = Utils.buildPercentileAggregation(field, getParameter().getPercentiles());
                searchRequestBuilder.addAggregation(percentiles);
            }
            extendedStats = Utils.buildStatsAggregation(field, getParameter().getStats());
        }
        else {
            extendedStats = Utils.buildStatsAggregation(field, Collections.singleton(Stat.COUNT));
        }
        searchRequestBuilder.addAggregation(extendedStats);
        if (!CollectionUtils.isNullOrEmpty(getParameter().getNesting())) {
            final HashSet<AggregationBuilder> subAggregations = new HashSet<>();
            subAggregations.add(extendedStats);
            if (null != percentiles) {
                subAggregations.add(percentiles);
            }
            searchRequestBuilder.addAggregation(
                    Utils.buildTermsAggregation(getParameter().getNesting()
                                                        .stream()
                                                        .map(x -> new ResultSort(x, ResultSort.Order.asc))
                                                        .collect(Collectors.toList()),
                                                subAggregations,
                                                elasticsearchTuningConfig.getAggregationSize()));
        }
    }
    catch (Exception e) {
        throw FoxtrotExceptions.queryCreationException(parameter, e);
    }
    return searchRequestBuilder;
}
 
Example 19
Source File: DistributedTableMetadataManager.java    From foxtrot with Apache License 2.0 4 votes vote down vote up
private void evaluateStringAggregation(String table, String field, FieldType type, SearchRequestBuilder query) {
    logger.info("table:{} field:{} type:{} aggregationType:{}", table, field, type, CARDINALITY);
    query.addAggregation(AggregationBuilders.cardinality(field)
            .field(field)
            .precisionThreshold(PRECISION_THRESHOLD));
}