Search in sources :

Example 1 with Aggregations

use of org.elasticsearch.search.aggregations.Aggregations in project elasticsearch by elastic.

the class SignificantTermsSignificanceScoreIT method testBackgroundVsSeparateSet.

// compute significance score by
// 1. terms agg on class and significant terms
// 2. filter buckets and set the background to the other class and set is_background false
// both should yield exact same result
public void testBackgroundVsSeparateSet(SignificanceHeuristic significanceHeuristicExpectingSuperset, SignificanceHeuristic significanceHeuristicExpectingSeparateSets) throws Exception {
    SearchResponse response1 = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE).addAggregation(terms("class").field(CLASS_FIELD).subAggregation(significantTerms("sig_terms").field(TEXT_FIELD).minDocCount(1).significanceHeuristic(significanceHeuristicExpectingSuperset))).execute().actionGet();
    assertSearchResponse(response1);
    SearchResponse response2 = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE).addAggregation(filter("0", QueryBuilders.termQuery(CLASS_FIELD, "0")).subAggregation(significantTerms("sig_terms").field(TEXT_FIELD).minDocCount(1).backgroundFilter(QueryBuilders.termQuery(CLASS_FIELD, "1")).significanceHeuristic(significanceHeuristicExpectingSeparateSets))).addAggregation(filter("1", QueryBuilders.termQuery(CLASS_FIELD, "1")).subAggregation(significantTerms("sig_terms").field(TEXT_FIELD).minDocCount(1).backgroundFilter(QueryBuilders.termQuery(CLASS_FIELD, "0")).significanceHeuristic(significanceHeuristicExpectingSeparateSets))).execute().actionGet();
    StringTerms classes = response1.getAggregations().get("class");
    SignificantTerms sigTerms0 = ((SignificantTerms) (classes.getBucketByKey("0").getAggregations().asMap().get("sig_terms")));
    assertThat(sigTerms0.getBuckets().size(), equalTo(2));
    double score00Background = sigTerms0.getBucketByKey("0").getSignificanceScore();
    double score01Background = sigTerms0.getBucketByKey("1").getSignificanceScore();
    SignificantTerms sigTerms1 = ((SignificantTerms) (classes.getBucketByKey("1").getAggregations().asMap().get("sig_terms")));
    double score10Background = sigTerms1.getBucketByKey("0").getSignificanceScore();
    double score11Background = sigTerms1.getBucketByKey("1").getSignificanceScore();
    Aggregations aggs = response2.getAggregations();
    sigTerms0 = (SignificantTerms) ((InternalFilter) aggs.get("0")).getAggregations().getAsMap().get("sig_terms");
    double score00SeparateSets = sigTerms0.getBucketByKey("0").getSignificanceScore();
    double score01SeparateSets = sigTerms0.getBucketByKey("1").getSignificanceScore();
    sigTerms1 = (SignificantTerms) ((InternalFilter) aggs.get("1")).getAggregations().getAsMap().get("sig_terms");
    double score10SeparateSets = sigTerms1.getBucketByKey("0").getSignificanceScore();
    double score11SeparateSets = sigTerms1.getBucketByKey("1").getSignificanceScore();
    assertThat(score00Background, equalTo(score00SeparateSets));
    assertThat(score01Background, equalTo(score01SeparateSets));
    assertThat(score10Background, equalTo(score10SeparateSets));
    assertThat(score11Background, equalTo(score11SeparateSets));
}
Also used : SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) InternalFilter(org.elasticsearch.search.aggregations.bucket.filter.InternalFilter) Aggregations(org.elasticsearch.search.aggregations.Aggregations) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 2 with Aggregations

use of org.elasticsearch.search.aggregations.Aggregations in project elasticsearch by elastic.

the class SignificantTermsSignificanceScoreIT method testScoresEqualForPositiveAndNegative.

public void testScoresEqualForPositiveAndNegative(SignificanceHeuristic heuristic) throws Exception {
    //check that results for both classes are the same with exclude negatives = false and classes are routing ids
    SearchResponse response = client().prepareSearch("test").addAggregation(terms("class").field("class").subAggregation(significantTerms("mySignificantTerms").field("text").executionHint(randomExecutionHint()).significanceHeuristic(heuristic).minDocCount(1).shardSize(1000).size(1000))).execute().actionGet();
    assertSearchResponse(response);
    StringTerms classes = response.getAggregations().get("class");
    assertThat(classes.getBuckets().size(), equalTo(2));
    Iterator<Terms.Bucket> classBuckets = classes.getBuckets().iterator();
    Aggregations aggregations = classBuckets.next().getAggregations();
    SignificantTerms sigTerms = aggregations.get("mySignificantTerms");
    Collection<SignificantTerms.Bucket> classA = sigTerms.getBuckets();
    Iterator<SignificantTerms.Bucket> classBBucketIterator = sigTerms.getBuckets().iterator();
    assertThat(classA.size(), greaterThan(0));
    for (SignificantTerms.Bucket classABucket : classA) {
        SignificantTerms.Bucket classBBucket = classBBucketIterator.next();
        assertThat(classABucket.getKey(), equalTo(classBBucket.getKey()));
        assertThat(classABucket.getSignificanceScore(), closeTo(classBBucket.getSignificanceScore(), 1.e-5));
    }
}
Also used : SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) Aggregations(org.elasticsearch.search.aggregations.Aggregations) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 3 with Aggregations

use of org.elasticsearch.search.aggregations.Aggregations in project elasticsearch by elastic.

the class ScriptedMetricIT method testInitMapCombineReduceWithParamsAsSubAgg.

public void testInitMapCombineReduceWithParamsAsSubAgg() {
    Map<String, Object> varsMap = new HashMap<>();
    varsMap.put("multiplier", 1);
    Map<String, Object> params = new HashMap<>();
    params.put("_agg", new ArrayList<>());
    params.put("vars", varsMap);
    Script initScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "vars.multiplier = 3", Collections.emptyMap());
    Script mapScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_agg.add(vars.multiplier)", Collections.emptyMap());
    Script combineScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "sum agg values as a new aggregation", Collections.emptyMap());
    Script reduceScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "sum aggs of agg values as a new aggregation", Collections.emptyMap());
    SearchResponse response = client().prepareSearch("idx").setQuery(matchAllQuery()).setSize(1000).addAggregation(histogram("histo").field("l_value").interval(1).subAggregation(scriptedMetric("scripted").params(params).initScript(initScript).mapScript(mapScript).combineScript(combineScript).reduceScript(reduceScript))).get();
    assertSearchResponse(response);
    assertThat(response.getHits().getTotalHits(), equalTo(numDocs));
    Aggregation aggregation = response.getAggregations().get("histo");
    assertThat(aggregation, notNullValue());
    assertThat(aggregation, instanceOf(Histogram.class));
    Histogram histoAgg = (Histogram) aggregation;
    assertThat(histoAgg.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = histoAgg.getBuckets();
    assertThat(buckets, notNullValue());
    for (Bucket b : buckets) {
        assertThat(b, notNullValue());
        assertThat(b.getDocCount(), equalTo(1L));
        Aggregations subAggs = b.getAggregations();
        assertThat(subAggs, notNullValue());
        assertThat(subAggs.asList().size(), equalTo(1));
        Aggregation subAgg = subAggs.get("scripted");
        assertThat(subAgg, notNullValue());
        assertThat(subAgg, instanceOf(ScriptedMetric.class));
        ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) subAgg;
        assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
        assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
        assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
        List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
        assertThat(aggregationList.size(), equalTo(1));
        Object object = aggregationList.get(0);
        assertThat(object, notNullValue());
        assertThat(object, instanceOf(Number.class));
        assertThat(((Number) object).longValue(), equalTo(3L));
    }
}
Also used : Script(org.elasticsearch.script.Script) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) HashMap(java.util.HashMap) Aggregations(org.elasticsearch.search.aggregations.Aggregations) ArrayList(java.util.ArrayList) ScriptedMetric(org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetric) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) Aggregation(org.elasticsearch.search.aggregations.Aggregation) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with Aggregations

use of org.elasticsearch.search.aggregations.Aggregations in project pancm_project by xuwujing.

the class EsHighLevelRestSearchTest method search.

/**
 * search查询使用示例
 *
 * @throws IOException
 */
private static void search() throws IOException {
    /*
         * 全文查询使用示例
         */
    // 查询指定的索引库
    SearchRequest searchRequest = new SearchRequest("user");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    // 搜索字段user为pancm的数据
    MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("user", "pancm");
    // 设置模糊查询
    matchQueryBuilder.fuzziness(Fuzziness.AUTO);
    // 设置前缀长度
    matchQueryBuilder.prefixLength(3);
    // 设置最大扩展选项来控制查询的模糊过程
    matchQueryBuilder.maxExpansions(10);
    /*
         * QueryBuilder也可以
         */
    // QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("user", "kimchy")
    // .fuzziness(Fuzziness.AUTO)
    // .prefixLength(3)
    // .maxExpansions(10);
    SearchSourceBuilder searchSourceBuilder2 = new SearchSourceBuilder();
    searchSourceBuilder2.query(matchQueryBuilder);
    SearchRequest searchRequest2 = new SearchRequest();
    searchRequest2.source(searchSourceBuilder2);
    // 同步查询
    SearchResponse searchResponse2 = client.search(searchRequest, RequestOptions.DEFAULT);
    SearchHits hits = searchResponse2.getHits();
    // 总条数和分值
    long totalHits = hits.getTotalHits();
    float maxScore = hits.getMaxScore();
    hits.forEach(hit -> {
        String index = hit.getIndex();
        String type = hit.getType();
        String id = hit.getId();
        float score = hit.getScore();
        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
        String string = hit.getSourceAsString();
        System.out.println("Match查询的Map结果:" + sourceAsMap);
        System.out.println("Match查询的String结果:" + string);
        String documentTitle = (String) sourceAsMap.get("title");
        // List<Object> users = (List<Object>) sourceAsMap.get("user");
        Map<String, Object> innerObject = (Map<String, Object>) sourceAsMap.get("innerObject");
    });
    System.out.println("\n=================\n");
    /*
         * 高亮查询
         */
    SearchSourceBuilder searchSourceBuilder3 = new SearchSourceBuilder();
    HighlightBuilder highlightBuilder = new HighlightBuilder();
    HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("title");
    // 设置字段高亮字体
    highlightTitle.highlighterType("user");
    highlightBuilder.field(highlightTitle);
    HighlightBuilder.Field highlightUser = new HighlightBuilder.Field("user");
    highlightBuilder.field(highlightUser);
    searchSourceBuilder3.highlighter(highlightBuilder);
    SearchRequest searchRequest3 = new SearchRequest();
    searchRequest3.source(searchSourceBuilder3);
    // 同步查询
    SearchResponse searchResponse3 = client.search(searchRequest3, RequestOptions.DEFAULT);
    searchResponse3.getHits().forEach(hit -> {
        Map<String, Object> map = hit.getSourceAsMap();
        String string = hit.getSourceAsString();
        System.out.println("Highlight查询的Map结果:" + map);
        System.out.println("Highlight查询的String结果:" + string);
    });
    System.out.println("\n=================\n");
    /**
     * 聚合查询
     */
    SearchSourceBuilder searchSourceBuilder4 = new SearchSourceBuilder();
    // terms 就是分组统计 根据user进行分组并创建一个新的聚合user_
    TermsAggregationBuilder aggregation = AggregationBuilders.terms("user_").field("user");
    aggregation.subAggregation(AggregationBuilders.avg("average_age").field("age"));
    searchSourceBuilder4.aggregation(aggregation);
    SearchRequest searchRequest4 = new SearchRequest();
    searchRequest4.source(searchSourceBuilder4);
    // 同步查询
    SearchResponse searchResponse4 = client.search(searchRequest4, RequestOptions.DEFAULT);
    // 聚合查询返回条件
    Aggregations aggregations = searchResponse4.getAggregations();
    System.out.println("聚合查询");
    for (Aggregation agg : aggregations) {
        String type = agg.getType();
        String name = agg.getName();
        Terms terms = (Terms) aggregations.get(name);
        for (Terms.Bucket bucket : terms.getBuckets()) {
            System.out.println("条数:" + bucket.getDocCount());
            System.out.println("key:" + bucket.getKey());
            System.out.println("num:" + bucket.getKeyAsNumber());
            Avg avg = bucket.getAggregations().get("average_age");
            System.out.println("value:" + avg.getValue());
        }
        if (type.equals(TermsAggregationBuilder.NAME)) {
            Bucket elasticBucket = ((Terms) agg).getBucketByKey("average_age");
            long numberOfDocs = elasticBucket.getDocCount();
            System.out.println("条数:" + numberOfDocs);
        }
    }
    /*
         * 建议查询
         */
    SearchSourceBuilder searchSourceBuilder5 = new SearchSourceBuilder();
    SuggestionBuilder termSuggestionBuilder = SuggestBuilders.termSuggestion("user").text("pancm");
    SuggestBuilder suggestBuilder = new SuggestBuilder();
    suggestBuilder.addSuggestion("suggest_user", termSuggestionBuilder);
    searchSourceBuilder5.suggest(suggestBuilder);
    SearchRequest searchRequest5 = new SearchRequest();
    searchRequest5.source(searchSourceBuilder5);
    // 同步查询
    SearchResponse searchResponse5 = client.search(searchRequest5, RequestOptions.DEFAULT);
    Suggest suggest = searchResponse5.getSuggest();
    TermSuggestion termSuggestion = suggest.getSuggestion("suggest_user");
    for (TermSuggestion.Entry entry : termSuggestion.getEntries()) {
        for (TermSuggestion.Entry.Option option : entry) {
            String suggestText = option.getText().string();
            System.out.println("返回结果:" + suggestText);
        }
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Aggregations(org.elasticsearch.search.aggregations.Aggregations) Suggest(org.elasticsearch.search.suggest.Suggest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Aggregation(org.elasticsearch.search.aggregations.Aggregation) Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) TermSuggestion(org.elasticsearch.search.suggest.term.TermSuggestion) SearchHits(org.elasticsearch.search.SearchHits) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) Avg(org.elasticsearch.search.aggregations.metrics.avg.Avg) Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Map(java.util.Map) SuggestionBuilder(org.elasticsearch.search.suggest.SuggestionBuilder) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)

Example 5 with Aggregations

use of org.elasticsearch.search.aggregations.Aggregations in project pancm_project by xuwujing.

the class EsAggregationSearchTest method agg.

protected static void agg(AggregationBuilder aggregation, String buk) throws IOException {
    SearchResponse searchResponse = search(aggregation);
    if (RestStatus.OK.equals(searchResponse.status())) {
        // 获取聚合结果
        Aggregations aggregations = searchResponse.getAggregations();
        if (buk.contains("avg")) {
            // 取子聚合
            Avg ba = aggregations.get(buk);
            logger.info(buk + ":" + ba.getValue());
            logger.info("------------------------------------");
        } else if (buk.contains("max")) {
            // 取子聚合
            Max ba = aggregations.get(buk);
            logger.info(buk + ":" + ba.getValue());
            logger.info("------------------------------------");
        } else if (buk.contains("min")) {
            // 取子聚合
            Min ba = aggregations.get(buk);
            logger.info(buk + ":" + ba.getValue());
            logger.info("------------------------------------");
        } else if (buk.contains("sum")) {
            // 取子聚合
            Sum ba = aggregations.get(buk);
            logger.info(buk + ":" + ba.getValue());
            logger.info("------------------------------------");
        } else if (buk.contains("top")) {
            // 取子聚合TopHits
            TopHits ba = aggregations.get(buk);
            logger.info(buk + ":" + ba.getHits().totalHits);
            logger.info("------------------------------------");
        } else if (buk.contains("group")) {
            Map<String, Object> map = new HashMap<>();
            List<Map<String, Object>> list = new ArrayList<>();
            agg(map, list, aggregations);
            logger.info("聚合查询结果:" + list);
            logger.info("------------------------------------");
        }
    }
}
Also used : Max(org.elasticsearch.search.aggregations.metrics.max.Max) Aggregations(org.elasticsearch.search.aggregations.Aggregations) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) SearchResponse(org.elasticsearch.action.search.SearchResponse) Avg(org.elasticsearch.search.aggregations.metrics.avg.Avg) Min(org.elasticsearch.search.aggregations.metrics.min.Min) TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Aggregations (org.elasticsearch.search.aggregations.Aggregations)26 SearchResponse (org.elasticsearch.action.search.SearchResponse)19 Test (org.junit.Test)14 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)11 ArrayList (java.util.ArrayList)10 AbstractDaoTest (org.finra.herd.dao.AbstractDaoTest)8 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)7 Aggregations (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregations)7 Map (java.util.Map)6 HashMap (java.util.HashMap)5 Nested (org.elasticsearch.search.aggregations.bucket.nested.Nested)5 SearchType (org.graylog.plugins.views.search.SearchType)5 Date (java.util.Date)4 AggregationBuilder (org.elasticsearch.search.aggregations.AggregationBuilder)4 List (java.util.List)3 SearchRequest (org.elasticsearch.action.search.SearchRequest)3 Aggregation (org.elasticsearch.search.aggregations.Aggregation)3 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)3 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)3 PivotResult (org.graylog.plugins.views.search.searchtypes.pivot.PivotResult)3