Java Code Examples for org.elasticsearch.action.search.SearchResponse#getAggregations()

The following examples show how to use org.elasticsearch.action.search.SearchResponse#getAggregations() . 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: UKAggTopResultExtractor.java    From youkefu with Apache License 2.0 7 votes vote down vote up
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
	Aggregations aggregations = response.getAggregations();
	Terms agg = aggregations.get(term) ;
	long total = agg.getSumOfOtherDocCounts() ;
	List<T> results = new ArrayList<T>();
	if(agg.getBuckets()!=null && agg.getBuckets().size()>0){
		for (int i = pageable.getPageNumber()*pageable.getPageSize();i<agg.getBuckets().size()  ; i++) {
			Terms.Bucket entry = agg.getBuckets().get(i) ;
			if(!StringUtils.isBlank(name) && entry.getAggregations().get(name)!=null){
				TopHits topHits = entry.getAggregations().get(name);
				for (SearchHit hit : topHits.getHits().getHits()) {
					T data = mapEntity(hit.getSourceAsString() , hit , clazz) ;
					if(data instanceof UKAgg){
						((UKAgg) data).setRowcount((int) topHits.getHits().getTotalHits());
					}
					results.add(data) ;
				}
			}
		}
	}
	return new AggregatedPageImpl<T>(results, pageable, total);
}
 
Example 2
Source File: ChildrenAggregationMain.java    From elasticsearch-pool with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    RestHighLevelClient client = HighLevelClient.getInstance();
    try{

        ChildrenAggregationBuilder aggregationBuilder = new ChildrenAggregationBuilder("utm","histogram");
        aggregationBuilder.field("utm");

        SearchRequest searchRequest = new SearchRequest("serverlog_20180710");//限定index
        searchRequest.types("log");//限定type

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.aggregation(aggregationBuilder);
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest);
        System.out.println(searchResponse);
        Aggregations aggregations = searchResponse.getAggregations();
        List<Aggregation> aggregationList = aggregations.asList();
        for(Aggregation each: aggregationList){
            System.out.println(each);
        }
    }finally{
        HighLevelClient.close();
    }
}
 
Example 3
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
@Test
public void postFilterTest() throws Exception {
    SqlElasticSearchRequestBuilder select = getSearchRequestBuilder(String.format("SELECT /*! POST_FILTER({\"term\":{\"gender\":\"m\"}}) */ COUNT(*) FROM %s/account GROUP BY gender", TEST_INDEX_ACCOUNT));
    SearchResponse res = (SearchResponse) select.get();
    Assert.assertEquals(507, res.getHits().getTotalHits().value);

    Aggregations result = res.getAggregations();
    Terms gender = result.get("gender");
    for (Terms.Bucket bucket : gender.getBuckets()) {
        String key = bucket.getKey().toString();
        long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue();
        if (key.equalsIgnoreCase("m")) {
            Assert.assertEquals(507, count);
        } else if (key.equalsIgnoreCase("f")) {
            Assert.assertEquals(493, count);
        } else {
            throw new Exception(String.format("Unexpected key. expected: m OR f. found: %s", key));
        }
    }
}
 
Example 4
Source File: ElasticsearchDataModel.java    From elasticsearch-taste with Apache License 2.0 6 votes vote down vote up
protected synchronized void loadValueStats() {
    if (stats != null) {
        return;
    }
    // TODO join userQueryBuilder and itemQueryBuilder
    final SearchResponse response = client
            .prepareSearch(preferenceIndex)
            .setTypes(preferenceType)
            .setQuery(getLastAccessedFilterQuery())
            .setSize(0)
            .addAggregation(
                    AggregationBuilders.stats(valueField).field(valueField))
            .execute().actionGet();
    final Aggregations aggregations = response.getAggregations();
    stats = aggregations.get(valueField);
}
 
Example 5
Source File: ElasticsearchHelper.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a representation of a set of aggregations from the search response. This method also validates that the retrieved object is not null.
 *
 * @param searchResponse the response of the search request
 *
 * @return the aggregations
 */
protected Aggregations getAggregationsFromSearchResponse(SearchResponse searchResponse)
{
    // Retrieve the aggregations.
    Aggregations aggregations = searchResponse.getAggregations();

    // Fail if the retrieved object is null.
    if (aggregations == null)
    {
        // Log the error along with the search response contents.
        LOGGER.error("Failed to retrieve aggregations from the search response. searchResponse={}", jsonHelper.objectToJson(searchResponse));

        // Throw an exception.
        throw new IllegalStateException("Invalid search result.");
    }

    return aggregations;
}
 
Example 6
Source File: ResultMapperExt.java    From roncoo-education with MIT License 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
	long totalHits = response.getHits().totalHits();
	List<T> results = new ArrayList<>();
	for (SearchHit hit : response.getHits()) {
		if (hit != null) {
			T result = null;
			if (StringUtils.hasText(hit.sourceAsString())) {
				result = mapEntity(hit.sourceAsString(), clazz);
			} else {
				result = mapEntity(hit.getFields().values(), clazz);
			}
			setPersistentEntityId(result, hit.getId(), clazz);
			setPersistentEntityVersion(result, hit.getVersion(), clazz);
			populateScriptFields(result, hit);

			// 高亮查询
			populateHighLightedFields(result, hit.getHighlightFields());
			results.add(result);
		}
	}

	return new AggregatedPageImpl<T>(results, pageable, totalHits, response.getAggregations(), response.getScrollId());
}
 
Example 7
Source File: TorrentDaoImpl.java    From Dodder with MIT License 6 votes vote down vote up
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {

	long totalHits = response.getHits().getTotalHits();
	float maxScore = response.getHits().getMaxScore();

	List<Torrent> results = new ArrayList<>();
	for (SearchHit hit : response.getHits().getHits()) {
		if (hit == null)
			continue;
		Torrent result;
		result = JSONUtil.parseObject(hit.getSourceAsString(), Torrent.class);
		result.setInfoHash(hit.getId());
		if (hit.getHighlightFields().containsKey("fileName"))
			result.setFileName(hit.getHighlightFields().get("fileName").fragments()[0].toString());
		else
			result.setFileName((String) hit.getSourceAsMap().get("fileName"));
		results.add(result);
	}
	return new AggregatedPageImpl<>((List<T>) results, pageable, totalHits, response.getAggregations(), response.getScrollId(),
			maxScore);
}
 
Example 8
Source File: HighlightResultHelper.java    From mogu_blog_v2 with Apache License 2.0 6 votes vote down vote up
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
    List<T> results = new ArrayList<>();
    for (SearchHit hit : response.getHits()) {
        if (hit != null) {
            T result = null;
            if (StringUtils.hasText(hit.getSourceAsString())) {
                result = JSONObject.parseObject(hit.getSourceAsString(), clazz);
            }
            // 高亮查询
            for (HighlightField field : hit.getHighlightFields().values()) {
                try {
                    PropertyUtils.setProperty(result, field.getName(), concat(field.fragments()));
                } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                    log.error("设置高亮字段异常:{}", e.getMessage(), e);
                }
            }
            results.add(result);
        }
    }
    return new AggregatedPageImpl<T>(results, pageable, response.getHits().getTotalHits(), response.getAggregations(), response.getScrollId());
}
 
Example 9
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleSubAggregations() throws  Exception {
	final String query = String.format("SELECT /*! DOCS_WITH_AGGREGATION(10) */ * FROM %s/account GROUP BY (gender), (state) ", TEST_INDEX_ACCOUNT);

       SqlElasticSearchRequestBuilder select = getSearchRequestBuilder(query);
	SearchResponse response = (SearchResponse) select.get();
	Aggregations result = response.getAggregations();

	Terms gender = result.get("gender");
	for(Terms.Bucket genderBucket : gender.getBuckets()) {
		String genderKey = genderBucket.getKey().toString();
		Assert.assertTrue("Gender should be m or f", genderKey.equals("m") || genderKey.equals("f"));
	}

	Assert.assertEquals(2, gender.getBuckets().size());

	Terms state = result.get("state");
	for(Terms.Bucket stateBucket : state.getBuckets()) {
		if(stateBucket.getKey().toString().equalsIgnoreCase("ak")) {
			Assert.assertTrue("There are 22 entries for state ak", stateBucket.getDocCount() == 22);
		}
	}

	Assert.assertEquals(response.getHits().getTotalHits().value, 1000);
	Assert.assertEquals(response.getHits().getHits().length, 10);
}
 
Example 10
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubAggregations() throws  Exception {
	Set expectedAges = new HashSet<>(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers()));
	final String query = String.format("SELECT /*! DOCS_WITH_AGGREGATION(10) */" +
               " * FROM %s/account GROUP BY (gender, terms('field'='age','size'=200,'alias'='age')), (state) LIMIT 200,200", TEST_INDEX_ACCOUNT);

	Map<String, Set<Integer>> buckets = new HashMap<>();

       SqlElasticSearchRequestBuilder select = getSearchRequestBuilder(query);
	SearchResponse response = (SearchResponse) select.get();
	Aggregations result = response.getAggregations();

	Terms gender = result.get("gender");
	for(Terms.Bucket genderBucket : gender.getBuckets()) {
		String genderKey = genderBucket.getKey().toString();
		buckets.put(genderKey, new HashSet<Integer>());
		Terms ageBuckets = (Terms) genderBucket.getAggregations().get("age");
		for(Terms.Bucket ageBucket : ageBuckets.getBuckets()) {
			buckets.get(genderKey).add(Integer.parseInt(ageBucket.getKey().toString()));
		}
	}

	Assert.assertEquals(2, buckets.keySet().size());
	Assert.assertEquals(expectedAges, buckets.get("m"));
	Assert.assertEquals(expectedAges, buckets.get("f"));

	Terms state = result.get("state");
	for(Terms.Bucket stateBucket : state.getBuckets()) {
		if(stateBucket.getKey().toString().equalsIgnoreCase("ak")) {
			Assert.assertTrue("There are 22 entries for state ak", stateBucket.getDocCount() == 22);
		}
	}

	Assert.assertEquals(response.getHits().getTotalHits().value, 1000);
	Assert.assertEquals(response.getHits().getHits().length, 10);
}
 
Example 11
Source File: MaxMinAggregationMain.java    From elasticsearch-pool with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
        RestHighLevelClient client = HighLevelClient.getInstance();
        try{
//            MaxAggregationBuilder aggregationBuilder = AggregationBuilders.max("utm").field("utm");
            MinAggregationBuilder aggregationBuilder = AggregationBuilders.min("utm").field("utm");

            SearchRequest searchRequest = new SearchRequest("serverlog_20180715");//限定index
            searchRequest.types("log");//限定type

            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.aggregation(aggregationBuilder);
            searchSourceBuilder.size(0);//设置不需要文档数据,只需要返回聚合结果
            searchRequest.source(searchSourceBuilder);

            SearchResponse searchResponse = client.search(searchRequest);
            System.out.println(searchResponse);

            //统计结果
            Aggregations aggregations = searchResponse.getAggregations();
            Map<String, Aggregation> aggregationMap = aggregations.asMap();
            for(Map.Entry<String,Aggregation> each: aggregationMap.entrySet()){
//                System.out.println(((ParsedMax)(each.getValue())).getValue());
                System.out.println(((ParsedMin)(each.getValue())).getValue());
            }

        }finally{
            HighLevelClient.close();
        }
    }
 
Example 12
Source File: CountAggregationMain.java    From elasticsearch-pool with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    RestHighLevelClient client = HighLevelClient.getInstance();
    try{
        QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("cmd", "get_fee_transfer_content_data");
        ValueCountAggregationBuilder aggregationBuilder = AggregationBuilders.count("cmd_count").field("cmd");

        SearchRequest searchRequest = new SearchRequest("serverlog_20180715");//限定index
        searchRequest.types("log");//限定type

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(matchQueryBuilder);
        searchSourceBuilder.aggregation(aggregationBuilder);
        searchSourceBuilder.size(0);//不返回具体业务数据,只需要聚合结果
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest);
        System.out.println(searchResponse);

        //统计结果
        Aggregations aggregations = searchResponse.getAggregations();
        Map<String, Aggregation> aggregationMap = aggregations.asMap();
        for(Map.Entry<String,Aggregation> each: aggregationMap.entrySet()){
            System.out.println(((ParsedValueCount)(each.getValue())).getValue());
        }

    }finally{
        HighLevelClient.close();
    }
}
 
Example 13
Source File: SumAggregationMain.java    From elasticsearch-pool with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
        RestHighLevelClient client = HighLevelClient.getInstance();
        try{
//            QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("cmd", "weather_hourforcast");
            SumAggregationBuilder aggregationBuilder = AggregationBuilders.sum("utm").field("utm");

            SearchRequest searchRequest = new SearchRequest("serverlog_20180715");//限定index
            searchRequest.types("log");//限定type

            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//            searchSourceBuilder.query(matchQueryBuilder);
            searchSourceBuilder.aggregation(aggregationBuilder);
            searchSourceBuilder.size(0);
            searchRequest.source(searchSourceBuilder);

            SearchResponse searchResponse = client.search(searchRequest);
            System.out.println(searchResponse);

            //统计结果
            Aggregations aggregations = searchResponse.getAggregations();
            Map<String, Aggregation> aggregationMap = aggregations.asMap();
            for(Map.Entry<String,Aggregation> each: aggregationMap.entrySet()){
                System.out.println(((ParsedSum)(each.getValue())).getValue());
            }

        }finally{
            HighLevelClient.close();
        }
    }
 
Example 14
Source File: AvgAggregationMain.java    From elasticsearch-pool with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    RestHighLevelClient client = HighLevelClient.getInstance();
    try{
        AvgAggregationBuilder aggregationBuilder = AggregationBuilders.avg("utm").field("utm").missing(0);

        SearchRequest searchRequest = new SearchRequest("serverlog_20180715");//限定index
        searchRequest.types("log");//限定type

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.aggregation(aggregationBuilder);
        searchSourceBuilder.size(0);
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest);
        System.out.println(searchResponse);

        //统计结果
        Aggregations aggregations = searchResponse.getAggregations();
        Map<String, Aggregation> aggregationMap = aggregations.asMap();
        for(Map.Entry<String,Aggregation> each: aggregationMap.entrySet()){
            System.out.println(((ParsedAvg)(each.getValue())).getValue());
        }

    }finally{
        HighLevelClient.close();
    }
}
 
Example 15
Source File: DistributedTableMetadataManager.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
private SearchResponse validateAndGetSearchResponse(MultiSearchResponse.Item item, String table) {
    if(item.isFailure()) {
        logger.info("FailureInDeducingCardinality table:{} failureMessage:{}", table, item.getFailureMessage());
        return null;
    }
    SearchResponse response = item.getResponse();
    if(null == response.getAggregations()) {
        return null;
    }
    return response;
}
 
Example 16
Source File: ElasticSearchServiceMapper.java    From vertx-elasticsearch-service with Apache License 2.0 5 votes vote down vote up
public static com.hubrick.vertx.elasticsearch.model.SearchResponse mapToSearchResponse(SearchResponse esSearchResponse) {
    final com.hubrick.vertx.elasticsearch.model.SearchResponse searchResponse = new com.hubrick.vertx.elasticsearch.model.SearchResponse();

    searchResponse.setRawResponse(readResponse(esSearchResponse));
    searchResponse.setTook(esSearchResponse.getTook().getMillis());
    searchResponse.setTimedOut(esSearchResponse.isTimedOut());
    searchResponse.setShards(mapToShards(esSearchResponse));
    searchResponse.setHits(mapToHits(esSearchResponse.getHits()));
    searchResponse.setScrollId(esSearchResponse.getScrollId());

    if (esSearchResponse.getSuggest() != null) {
        for (Suggest.Suggestion<?> entries : esSearchResponse.getSuggest()) {
            searchResponse.addSuggestion(entries.getName(), mapToSuggestion(entries));
        }
    }

    if (esSearchResponse.getAggregations() != null) {
        searchResponse.setAggregations(
                esSearchResponse.getAggregations().asMap()
                        .entrySet()
                        .stream()
                        .collect(Collectors.toMap(e -> e.getKey(), e -> readResponse((InternalAggregation) e.getValue())))
        );
    }

    return searchResponse;
}
 
Example 17
Source File: NpmSearchFacetHosted.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
public Content searchV1(final Parameters parameters) throws IOException {
  String text = npmSearchParameterExtractor.extractText(parameters);
  int size = npmSearchParameterExtractor.extractSize(parameters);
  int from = npmSearchParameterExtractor.extractFrom(parameters);

  // npm search V1 endpoint currently returns an empty result set if no text is provided in the request
  NpmSearchResponse response;
  if (text.isEmpty()) {
    response = npmSearchResponseFactory.buildEmptyResponse();
  }
  else {
    QueryStringQueryBuilder query = QueryBuilders.queryStringQuery(text)
        .allowLeadingWildcard(true)
        .analyzeWildcard(true);
    TermsBuilder terms = AggregationBuilders.terms("name")
        .field("assets.attributes.npm.name")
        .size(v1SearchMaxResults)
        .subAggregation(AggregationBuilders.topHits("versions")
            .addSort(SortBuilders.fieldSort("assets.attributes.npm.search_normalized_version")
                .order(SortOrder.DESC))
            .setTrackScores(true)
            .setSize(1));

    SearchResponse searchResponse = searchQueryService.search(
        repositoryQuery(query).inRepositories(getRepository()), singletonList(terms));
    Aggregations aggregations = searchResponse.getAggregations();
    Terms nameTerms = aggregations.get("name");
    response = npmSearchResponseFactory.buildResponseForResults(nameTerms.getBuckets(), size, from);
  }

  String content = npmSearchResponseMapper.writeString(response);
  return new Content(new StringPayload(content, ContentTypes.APPLICATION_JSON));
}
 
Example 18
Source File: QueryActionElasticExecutor.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
public static Aggregations executeAggregationAction(AggregationQueryAction aggregationQueryAction) throws SqlParseException {
    SqlElasticSearchRequestBuilder select =  aggregationQueryAction.explain();
    SearchResponse resp = (SearchResponse) select.get();

    //
    if (resp.getFailedShards() > 0) {
        if (resp.getSuccessfulShards() < 1) {
            throw new IllegalStateException("fail to aggregation[" + select + "], " + Arrays.toString(resp.getShardFailures()));
        }

        LOGGER.warn("The failures that occurred during the aggregation[{}]: {}", select, Arrays.toString(resp.getShardFailures()));
    }

    return resp.getAggregations();
}
 
Example 19
Source File: SearchResult.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
public SearchResult(SearchResponse resp, Select select) throws SqlParseException {
	Aggregations aggs = resp.getAggregations();
	if (aggs.get("filter") != null) {
		InternalFilter inf = aggs.get("filter");
		aggs = inf.getAggregations();
	}
	if (aggs.get("group by") != null) {
		InternalTerms terms = aggs.get("group by");
		Collection<Bucket> buckets = terms.getBuckets();
		this.total = buckets.size();
		results = new ArrayList<>(buckets.size());
		for (Bucket bucket : buckets) {
			Map<String, Object> aggsMap = toAggsMap(bucket.getAggregations().getAsMap());
			aggsMap.put("docCount", bucket.getDocCount());
			results.add(aggsMap);
		}
	} else {
		results = new ArrayList<>(1);
		this.total = 1;
		Map<String, Object> map = new HashMap<>();
		for (Aggregation aggregation : aggs) {
			map.put(aggregation.getName(), covenValue(aggregation));
		}
		results.add(map);
	}

}
 
Example 20
Source File: ResultHighlightMapper.java    From klask-io with GNU General Public License v3.0 4 votes vote down vote up
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
    List<File> result = new ArrayList<>();
    long totalHits = response.getHits().getTotalHits();
    for (SearchHit searchHit : response.getHits()) {
        if (response.getHits().getHits().length <= 0) {
            return null;
        }

        //System.out.println(response.toString());

        String summaryWithHighlight = null;
        String pathWithHighlight = null;
        HighlightField highlightFieldContent = searchHit.getHighlightFields().get("content");
        HighlightField highlightFieldPath = searchHit.getHighlightFields().get("path");
        if (highlightFieldContent != null) {
            summaryWithHighlight = Arrays.stream(highlightFieldContent.fragments())
                .map(text -> EncodingUtil.convertToUTF8(text.toString()))
                .collect(Collectors.joining("\n[...]\n"));
        }
        if (highlightFieldPath != null && highlightFieldPath.fragments() != null) {
            pathWithHighlight = EncodingUtil.unEscapeString(highlightFieldPath.fragments()[0].toString());
        }
        File oneFile = new File(
            (String) searchHit.getSource().get("id"),
            (String) searchHit.getSource().get("name"),
            (String) searchHit.getSource().get("extension"),
            pathWithHighlight != null ? pathWithHighlight : (String) searchHit.getSource().get("path"),
            (String) searchHit.getSource().get("project"),
            summaryWithHighlight,
            (String) searchHit.getSource().get("version"),
            //conversion en string puis en long, très bizarre, à l'origine, il était préférable de réaliser :
            //(Long) searchHit.getSource().get("size")
            //mais cela jette un classCastException Integer to Long
            Long.valueOf(searchHit.getSource().get("size").toString())
        );
        oneFile.setScore(searchHit.getScore());
        result.add(oneFile);
    }
    return new AggregatedPageImpl<>((List<T>) result, pageable, totalHits, response.getAggregations());
}