Search in sources :

Example 61 with Terms

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

the class DiversifiedSamplerTests method testDiversifiedSampler.

public void testDiversifiedSampler() throws Exception {
    String[] data = { // "id,cat,name,price,inStock,author_t,series_t,sequence_i,genre_s,genre_id",
    "0553573403,book,A Game of Thrones,7.99,true,George R.R. Martin,A Song of Ice and Fire,1,fantasy,0", "0553579908,book,A Clash of Kings,7.99,true,George R.R. Martin,A Song of Ice and Fire,2,fantasy,0", "055357342X,book,A Storm of Swords,7.99,true,George R.R. Martin,A Song of Ice and Fire,3,fantasy,0", "0553293354,book,Foundation,17.99,true,Isaac Asimov,Foundation Novels,1,scifi,1", "0812521390,book,The Black Company,6.99,false,Glen Cook,The Chronicles of The Black Company,1,fantasy,0", "0812550706,book,Ender's Game,6.99,true,Orson Scott Card,Ender,1,scifi,1", "0441385532,book,Jhereg,7.95,false,Steven Brust,Vlad Taltos,1,fantasy,0", "0380014300,book,Nine Princes In Amber,6.99,true,Roger Zelazny,the Chronicles of Amber,1,fantasy,0", "0805080481,book,The Book of Three,5.99,true,Lloyd Alexander,The Chronicles of Prydain,1,fantasy,0", "080508049X,book,The Black Cauldron,5.99,true,Lloyd Alexander,The Chronicles of Prydain,2,fantasy,0" };
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    for (String entry : data) {
        String[] parts = entry.split(",");
        Document document = new Document();
        document.add(new SortedDocValuesField("id", new BytesRef(parts[0])));
        document.add(new StringField("cat", parts[1], Field.Store.NO));
        document.add(new TextField("name", parts[2], Field.Store.NO));
        document.add(new DoubleDocValuesField("price", Double.valueOf(parts[3])));
        document.add(new StringField("inStock", parts[4], Field.Store.NO));
        document.add(new StringField("author", parts[5], Field.Store.NO));
        document.add(new StringField("series", parts[6], Field.Store.NO));
        document.add(new StringField("sequence", parts[7], Field.Store.NO));
        document.add(new SortedDocValuesField("genre", new BytesRef(parts[8])));
        document.add(new NumericDocValuesField("genre_id", Long.valueOf(parts[9])));
        indexWriter.addDocument(document);
    }
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    MappedFieldType genreFieldType = new KeywordFieldMapper.KeywordFieldType();
    genreFieldType.setName("genre");
    genreFieldType.setHasDocValues(true);
    Consumer<InternalSampler> verify = result -> {
        Terms terms = result.getAggregations().get("terms");
        assertEquals(2, terms.getBuckets().size());
        assertEquals("0805080481", terms.getBuckets().get(0).getKeyAsString());
        assertEquals("0812550706", terms.getBuckets().get(1).getKeyAsString());
    };
    testCase(indexSearcher, genreFieldType, "map", verify);
    testCase(indexSearcher, genreFieldType, "global_ordinals", verify);
    testCase(indexSearcher, genreFieldType, "bytes_hash", verify);
    genreFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
    genreFieldType.setName("genre_id");
    testCase(indexSearcher, genreFieldType, null, verify);
    // wrong field:
    genreFieldType = new KeywordFieldMapper.KeywordFieldType();
    genreFieldType.setName("wrong_field");
    genreFieldType.setHasDocValues(true);
    testCase(indexSearcher, genreFieldType, null, result -> {
        Terms terms = result.getAggregations().get("terms");
        assertEquals(1, terms.getBuckets().size());
        assertEquals("0805080481", terms.getBuckets().get(0).getKeyAsString());
    });
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) StringField(org.apache.lucene.document.StringField) Index(org.elasticsearch.index.Index) Document(org.apache.lucene.document.Document) SortedNumericDVIndexFieldData(org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Directory(org.apache.lucene.store.Directory) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) DoubleDocValuesField(org.apache.lucene.document.DoubleDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) BytesRef(org.apache.lucene.util.BytesRef) FieldValueFactorFunction(org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) DirectoryReader(org.apache.lucene.index.DirectoryReader) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Consumer(java.util.function.Consumer) AggregatorTestCase(org.elasticsearch.search.aggregations.AggregatorTestCase) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) KeywordFieldMapper(org.elasticsearch.index.mapper.KeywordFieldMapper) Field(org.apache.lucene.document.Field) IndexNumericFieldData(org.elasticsearch.index.fielddata.IndexNumericFieldData) NumberFieldMapper(org.elasticsearch.index.mapper.NumberFieldMapper) TextField(org.apache.lucene.document.TextField) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) NumberFieldMapper(org.elasticsearch.index.mapper.NumberFieldMapper) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Document(org.apache.lucene.document.Document) KeywordFieldMapper(org.elasticsearch.index.mapper.KeywordFieldMapper) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) StringField(org.apache.lucene.document.StringField) DoubleDocValuesField(org.apache.lucene.document.DoubleDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) TextField(org.apache.lucene.document.TextField) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 62 with Terms

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

the class StringTermsIT method testSingleValuedFieldOrderedByMultiValueSubAggregationAsc.

public void testSingleValuedFieldOrderedByMultiValueSubAggregationAsc() throws Exception {
    boolean asc = true;
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.aggregation("stats.avg", asc)).subAggregation(stats("stats").field("i"))).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));
    int i = 0;
    for (Terms.Bucket bucket : terms.getBuckets()) {
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo("val" + i));
        assertThat(bucket.getDocCount(), equalTo(1L));
        Stats stats = bucket.getAggregations().get("stats");
        assertThat(stats, notNullValue());
        assertThat(stats.getMax(), equalTo((double) i));
        i++;
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) AggregationBuilders.extendedStats(org.elasticsearch.search.aggregations.AggregationBuilders.extendedStats) ExtendedStats(org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats) Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 63 with Terms

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

the class StringTermsIT method testSingleValuedFieldOrderedBySubAggregationAscMultiHierarchyLevelsSpecialCharsNoDotNotation.

public void testSingleValuedFieldOrderedBySubAggregationAscMultiHierarchyLevelsSpecialCharsNoDotNotation() throws Exception {
    StringBuilder filter2NameBuilder = new StringBuilder("filt.er2");
    filter2NameBuilder.append(randomAsciiOfLengthBetween(3, 10).replace("[", "").replace("]", "").replace(">", ""));
    String filter2Name = filter2NameBuilder.toString();
    StringBuilder statsNameBuilder = new StringBuilder("st.ats");
    statsNameBuilder.append(randomAsciiOfLengthBetween(3, 10).replace("[", "").replace("]", "").replace(">", ""));
    String statsName = statsNameBuilder.toString();
    boolean asc = randomBoolean();
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("tags").executionHint(randomExecutionHint()).field("tag").collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.aggregation("filter1>" + filter2Name + ">" + statsName + "[max]", asc)).subAggregation(filter("filter1", QueryBuilders.matchAllQuery()).subAggregation(filter(filter2Name, QueryBuilders.matchAllQuery()).subAggregation(stats(statsName).field("i"))))).execute().actionGet();
    assertSearchResponse(response);
    Terms tags = response.getAggregations().get("tags");
    assertThat(tags, notNullValue());
    assertThat(tags.getName(), equalTo("tags"));
    assertThat(tags.getBuckets().size(), equalTo(2));
    Iterator<Terms.Bucket> iters = tags.getBuckets().iterator();
    // the max for "more" is 2
    // the max for "less" is 4
    Terms.Bucket tag = iters.next();
    assertThat(tag, notNullValue());
    assertThat(key(tag), equalTo(asc ? "more" : "less"));
    assertThat(tag.getDocCount(), equalTo(asc ? 3L : 2L));
    Filter filter1 = tag.getAggregations().get("filter1");
    assertThat(filter1, notNullValue());
    assertThat(filter1.getDocCount(), equalTo(asc ? 3L : 2L));
    Filter filter2 = filter1.getAggregations().get(filter2Name);
    assertThat(filter2, notNullValue());
    assertThat(filter2.getDocCount(), equalTo(asc ? 3L : 2L));
    Stats stats = filter2.getAggregations().get(statsName);
    assertThat(stats, notNullValue());
    assertThat(stats.getMax(), equalTo(asc ? 2.0 : 4.0));
    tag = iters.next();
    assertThat(tag, notNullValue());
    assertThat(key(tag), equalTo(asc ? "less" : "more"));
    assertThat(tag.getDocCount(), equalTo(asc ? 2L : 3L));
    filter1 = tag.getAggregations().get("filter1");
    assertThat(filter1, notNullValue());
    assertThat(filter1.getDocCount(), equalTo(asc ? 2L : 3L));
    filter2 = filter1.getAggregations().get(filter2Name);
    assertThat(filter2, notNullValue());
    assertThat(filter2.getDocCount(), equalTo(asc ? 2L : 3L));
    stats = filter2.getAggregations().get(statsName);
    assertThat(stats, notNullValue());
    assertThat(stats.getMax(), equalTo(asc ? 4.0 : 2.0));
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) AggregationBuilders.extendedStats(org.elasticsearch.search.aggregations.AggregationBuilders.extendedStats) ExtendedStats(org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats) Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 64 with Terms

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

the class StringTermsIT method testScriptMultiValued.

public void testScriptMultiValued() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).executionHint(randomExecutionHint()).script(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['" + MULTI_VALUED_FIELD_NAME + "']", Collections.emptyMap()))).get();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(6));
    for (int i = 0; i < 6; i++) {
        Terms.Bucket bucket = terms.getBucketByKey("val" + i);
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo("val" + i));
        if (i == 0 || i == 5) {
            assertThat(bucket.getDocCount(), equalTo(1L));
        } else {
            assertThat(bucket.getDocCount(), equalTo(2L));
        }
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Script(org.elasticsearch.script.Script) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 65 with Terms

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

the class StringTermsIT method testMultiValuedFieldWithValueScriptNotUnique.

public void testMultiValuedFieldWithValueScriptNotUnique() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(MULTI_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).script(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_value.substring(0,3)", Collections.emptyMap()))).get();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(1));
    Terms.Bucket bucket = terms.getBucketByKey("val");
    assertThat(bucket, notNullValue());
    assertThat(key(bucket), equalTo("val"));
    assertThat(bucket.getDocCount(), equalTo(5L));
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Script(org.elasticsearch.script.Script) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)252 SearchResponse (org.elasticsearch.action.search.SearchResponse)238 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)209 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)97 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)37 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)36 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)30 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)26 Script (org.elasticsearch.script.Script)22 ArrayList (java.util.ArrayList)21 HashMap (java.util.HashMap)21 IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)16 ExtendedStats (org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats)16 SearchHits (org.elasticsearch.search.SearchHits)13 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)13 InternalBucketMetricValue (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue)13 Avg (org.elasticsearch.search.aggregations.metrics.avg.Avg)12 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)11 Sampler (org.elasticsearch.search.aggregations.bucket.sampler.Sampler)10 PipelineAggregatorBuilders.percentilesBucket (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.percentilesBucket)9