Search in sources :

Example 16 with Min

use of org.elasticsearch.search.aggregations.metrics.min.Min in project elasticsearch by elastic.

the class MinIT method testScriptSingleValuedWithParams.

@Override
public void testScriptSingleValuedWithParams() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("dec", 1);
    Script script = new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "doc['value'].value - dec", params);
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(min("min").script(script)).get();
    assertHitCount(searchResponse, 10);
    Min min = searchResponse.getAggregations().get("min");
    assertThat(min, notNullValue());
    assertThat(min.getName(), equalTo("min"));
    assertThat(min.getValue(), equalTo(0.0));
}
Also used : Script(org.elasticsearch.script.Script) Min(org.elasticsearch.search.aggregations.metrics.min.Min) HashMap(java.util.HashMap) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 17 with Min

use of org.elasticsearch.search.aggregations.metrics.min.Min in project elasticsearch by elastic.

the class MinIT method testSingleValuedFieldWithFormatter.

public void testSingleValuedFieldWithFormatter() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(min("min").format("0000.0").field("value")).execute().actionGet();
    assertHitCount(searchResponse, 10);
    Min min = searchResponse.getAggregations().get("min");
    assertThat(min, notNullValue());
    assertThat(min.getName(), equalTo("min"));
    assertThat(min.getValue(), equalTo(1.0));
    assertThat(min.getValueAsString(), equalTo("0001.0"));
}
Also used : Min(org.elasticsearch.search.aggregations.metrics.min.Min) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 18 with Min

use of org.elasticsearch.search.aggregations.metrics.min.Min in project elasticsearch by elastic.

the class MinIT method testSingleValuedFieldWithValueScriptWithParams.

@Override
public void testSingleValuedFieldWithValueScriptWithParams() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("dec", 1);
    Script script = new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "_value - dec", params);
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(min("min").field("value").script(script)).get();
    assertHitCount(searchResponse, 10);
    Min min = searchResponse.getAggregations().get("min");
    assertThat(min, notNullValue());
    assertThat(min.getName(), equalTo("min"));
    assertThat(min.getValue(), equalTo(0.0));
}
Also used : Script(org.elasticsearch.script.Script) Min(org.elasticsearch.search.aggregations.metrics.min.Min) HashMap(java.util.HashMap) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 19 with Min

use of org.elasticsearch.search.aggregations.metrics.min.Min in project elasticsearch by elastic.

the class MinIT method testMultiValuedFieldWithValueScriptWithParams.

@Override
public void testMultiValuedFieldWithValueScriptWithParams() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("dec", 1);
    Script script = new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "_value - dec", params);
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(min("min").field("values").script(script)).get();
    assertHitCount(searchResponse, 10);
    Min min = searchResponse.getAggregations().get("min");
    assertThat(min, notNullValue());
    assertThat(min.getName(), equalTo("min"));
    assertThat(min.getValue(), equalTo(1.0));
}
Also used : Script(org.elasticsearch.script.Script) Min(org.elasticsearch.search.aggregations.metrics.min.Min) HashMap(java.util.HashMap) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 20 with Min

use of org.elasticsearch.search.aggregations.metrics.min.Min in project elasticsearch by elastic.

the class SamplerAggregatorTests method testSampler.

/**
     * Uses the sampler aggregation to find the minimum value of a field out of the top 3 scoring documents in a search.
     */
public void testSampler() throws IOException {
    TextFieldType textFieldType = new TextFieldType();
    textFieldType.setIndexAnalyzer(new NamedAnalyzer("foo", AnalyzerScope.GLOBAL, new StandardAnalyzer()));
    MappedFieldType numericFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
    numericFieldType.setName("int");
    IndexWriterConfig indexWriterConfig = newIndexWriterConfig();
    indexWriterConfig.setMaxBufferedDocs(100);
    // flush on open to have a single segment with predictable docIds
    indexWriterConfig.setRAMBufferSizeMB(100);
    try (Directory dir = newDirectory();
        IndexWriter w = new IndexWriter(dir, indexWriterConfig)) {
        for (long value : new long[] { 7, 3, -10, -6, 5, 50 }) {
            Document doc = new Document();
            StringBuilder text = new StringBuilder();
            for (int i = 0; i < value; i++) {
                text.append("good ");
            }
            doc.add(new Field("text", text.toString(), textFieldType));
            doc.add(new SortedNumericDocValuesField("int", value));
            w.addDocument(doc);
        }
        SamplerAggregationBuilder aggBuilder = new SamplerAggregationBuilder("sampler").shardSize(3).subAggregation(new MinAggregationBuilder("min").field("int"));
        try (IndexReader reader = DirectoryReader.open(w)) {
            assertEquals("test expects a single segment", 1, reader.leaves().size());
            IndexSearcher searcher = new IndexSearcher(reader);
            Sampler sampler = searchAndReduce(searcher, new TermQuery(new Term("text", "good")), aggBuilder, textFieldType, numericFieldType);
            Min min = sampler.getAggregations().get("min");
            assertEquals(5.0, min.getValue(), 0);
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) NamedAnalyzer(org.elasticsearch.index.analysis.NamedAnalyzer) Term(org.apache.lucene.index.Term) TextFieldType(org.elasticsearch.index.mapper.TextFieldMapper.TextFieldType) Document(org.apache.lucene.document.Document) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Field(org.apache.lucene.document.Field) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Min(org.elasticsearch.search.aggregations.metrics.min.Min) IndexWriter(org.apache.lucene.index.IndexWriter) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) MinAggregationBuilder(org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder) IndexReader(org.apache.lucene.index.IndexReader) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Directory(org.apache.lucene.store.Directory)

Aggregations

Min (org.elasticsearch.search.aggregations.metrics.min.Min)20 SearchResponse (org.elasticsearch.action.search.SearchResponse)18 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)17 Script (org.elasticsearch.script.Script)8 HashMap (java.util.HashMap)4 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)2 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)2 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)2 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)1 Document (org.apache.lucene.document.Document)1 Field (org.apache.lucene.document.Field)1 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)1 Term (org.apache.lucene.index.Term)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1