Search in sources :

Example 6 with StringTerms

use of org.elasticsearch.search.aggregations.bucket.terms.StringTerms 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 7 with StringTerms

use of org.elasticsearch.search.aggregations.bucket.terms.StringTerms in project tutorials by eugenp.

the class ElasticSearchQueryIntegrationTest method givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually.

@Test
public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() {
    final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags").order(Terms.Order.aggregation("_count", false));
    final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation).execute().actionGet();
    final Map<String, Aggregation> results = response.getAggregations().asMap();
    final StringTerms topTags = (StringTerms) results.get("top_tags");
    final List<String> keys = topTags.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString).collect(toList());
    assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys);
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) TermsBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test)

Example 8 with StringTerms

use of org.elasticsearch.search.aggregations.bucket.terms.StringTerms in project elasticsearch by elastic.

the class NestedIT method testNestNestedAggs.

public void testNestNestedAggs() throws Exception {
    SearchResponse response = client().prepareSearch("idx_nested_nested_aggs").addAggregation(nested("level1", "nested1").subAggregation(terms("a").field("nested1.a.keyword").collectMode(aggCollectionMode).subAggregation(nested("level2", "nested1.nested2").subAggregation(sum("sum").field("nested1.nested2.b"))))).get();
    assertSearchResponse(response);
    Nested level1 = response.getAggregations().get("level1");
    assertThat(level1, notNullValue());
    assertThat(level1.getName(), equalTo("level1"));
    assertThat(level1.getDocCount(), equalTo(2L));
    StringTerms a = level1.getAggregations().get("a");
    Terms.Bucket bBucket = a.getBucketByKey("a");
    assertThat(bBucket.getDocCount(), equalTo(1L));
    Nested level2 = bBucket.getAggregations().get("level2");
    assertThat(level2.getDocCount(), equalTo(1L));
    Sum sum = level2.getAggregations().get("sum");
    assertThat(sum.getValue(), equalTo(2d));
    a = level1.getAggregations().get("a");
    bBucket = a.getBucketByKey("b");
    assertThat(bBucket.getDocCount(), equalTo(1L));
    level2 = bBucket.getAggregations().get("level2");
    assertThat(level2.getDocCount(), equalTo(1L));
    sum = level2.getAggregations().get("sum");
    assertThat(sum.getValue(), equalTo(2d));
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) LongTerms(org.elasticsearch.search.aggregations.bucket.terms.LongTerms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 9 with StringTerms

use of org.elasticsearch.search.aggregations.bucket.terms.StringTerms in project elasticsearch by elastic.

the class SignificantTermsSignificanceScoreIT method testXContentResponse.

public void testXContentResponse() throws Exception {
    String type = randomBoolean() ? "text" : "long";
    String settings = "{\"index.number_of_shards\": 1, \"index.number_of_replicas\": 0}";
    SharedSignificantTermsTestMethods.index01Docs(type, settings, this);
    SearchResponse response = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE).addAggregation(terms("class").field(CLASS_FIELD).subAggregation(significantTerms("sig_terms").field(TEXT_FIELD))).execute().actionGet();
    assertSearchResponse(response);
    StringTerms classes = response.getAggregations().get("class");
    assertThat(classes.getBuckets().size(), equalTo(2));
    for (Terms.Bucket classBucket : classes.getBuckets()) {
        Map<String, Aggregation> aggs = classBucket.getAggregations().asMap();
        assertTrue(aggs.containsKey("sig_terms"));
        SignificantTerms agg = (SignificantTerms) aggs.get("sig_terms");
        assertThat(agg.getBuckets().size(), equalTo(1));
        String term = agg.iterator().next().getKeyAsString();
        String classTerm = classBucket.getKeyAsString();
        assertTrue(term.equals(classTerm));
    }
    XContentBuilder responseBuilder = XContentFactory.jsonBuilder();
    responseBuilder.startObject();
    classes.toXContent(responseBuilder, ToXContent.EMPTY_PARAMS);
    responseBuilder.endObject();
    String result = "{\"class\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0," + "\"buckets\":[" + "{" + "\"key\":\"0\"," + "\"doc_count\":4," + "\"sig_terms\":{" + "\"doc_count\":4," + "\"buckets\":[" + "{" + "\"key\":" + (type.equals("long") ? "0," : "\"0\",") + "\"doc_count\":4," + "\"score\":0.39999999999999997," + "\"bg_count\":5" + "}" + "]" + "}" + "}," + "{" + "\"key\":\"1\"," + "\"doc_count\":3," + "\"sig_terms\":{" + "\"doc_count\":3," + "\"buckets\":[" + "{" + "\"key\":" + (type.equals("long") ? "1," : "\"1\",") + "\"doc_count\":3," + "\"score\":0.75," + "\"bg_count\":4" + "}]}}]}}";
    assertThat(responseBuilder.string(), equalTo(result));
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) AggregationBuilders.significantTerms(org.elasticsearch.search.aggregations.AggregationBuilders.significantTerms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 10 with StringTerms

use of org.elasticsearch.search.aggregations.bucket.terms.StringTerms in project crate by crate.

the class FulltextIntegrationTest method testCopyValuesFromStringArrayToIndex.

@Test
public void testCopyValuesFromStringArrayToIndex() throws Exception {
    execute("CREATE TABLE t_string_array (" + "  id INTEGER PRIMARY KEY," + "  keywords ARRAY(STRING) INDEX USING FULLTEXT," + "  INDEX keywords_ft USING FULLTEXT(keywords)" + ")");
    execute("INSERT INTO t_string_array (id, keywords) VALUES (1, ['foo', 'bar', 'foo bar'])");
    ensureYellow();
    refresh();
    execute("SELECT id, keywords FROM t_string_array WHERE match(keywords_ft, 'foo')");
    assertThat(response.rows().length, is(1));
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("t_string_array");
    // we need to use ES search request to aggregate on terms
    String q = "{\n" + "    \"from\": 0,\n" + "    \"query\": {\n" + "      \"match_all\": {}\n" + "    },\n" + "    \"aggregations\": {\n" + "        \"kw\": {\n" + "            \"terms\": {\"field\": \"keywords_ft\"}\n" + "        }\n" + "    },\n" + "    \"size\": 0\n" + "}";
    BytesReference bytesRef = new BytesArray(q);
    searchRequest.source(bytesRef);
    SearchResponse searchResponse = client().search(searchRequest).get();
    StringTerms terms = searchResponse.getAggregations().get("kw");
    List<String> termValues = new ArrayList<>(terms.getBuckets().size());
    terms.getBuckets().forEach(b -> termValues.add(b.getKeyAsString()));
    assertThat(termValues, containsInAnyOrder("foo", "bar"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) SearchRequest(org.elasticsearch.action.search.SearchRequest) BytesArray(org.elasticsearch.common.bytes.BytesArray) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test)

Aggregations

StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)12 SearchResponse (org.elasticsearch.action.search.SearchResponse)10 Aggregation (org.elasticsearch.search.aggregations.Aggregation)6 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)6 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)6 SignificantTerms (org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms)5 AggregationBuilders.significantTerms (org.elasticsearch.search.aggregations.AggregationBuilders.significantTerms)3 Aggregations (org.elasticsearch.search.aggregations.Aggregations)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 MultiBucketsAggregation (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)2 LongTerms (org.elasticsearch.search.aggregations.bucket.terms.LongTerms)2 TermsBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder)2 SearchRequest (org.elasticsearch.action.search.SearchRequest)1 BytesArray (org.elasticsearch.common.bytes.BytesArray)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 SearchPlugin (org.elasticsearch.plugins.SearchPlugin)1 AggregationSpec (org.elasticsearch.plugins.SearchPlugin.AggregationSpec)1 PipelineAggregationSpec (org.elasticsearch.plugins.SearchPlugin.PipelineAggregationSpec)1