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));
}
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"));
}
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));
}
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));
}
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);
}
}
}
Aggregations