use of org.apache.lucene.document.SortedNumericDocValuesField in project elasticsearch by elastic.
the class HistogramAggregatorTests method testMissing.
public void testMissing() throws Exception {
try (Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
for (long value : new long[] { 7, 3, -10, -6, 5, 50 }) {
Document doc = new Document();
doc.add(new SortedNumericDocValuesField("field", value));
w.addDocument(doc);
w.addDocument(new Document());
}
HistogramAggregationBuilder aggBuilder = new HistogramAggregationBuilder("my_agg").field("field").interval(5).missing(2d);
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
fieldType.setName("field");
try (IndexReader reader = w.getReader()) {
IndexSearcher searcher = new IndexSearcher(reader);
Histogram histogram = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType);
assertEquals(4, histogram.getBuckets().size());
assertEquals(-10d, histogram.getBuckets().get(0).getKey());
assertEquals(2, histogram.getBuckets().get(0).getDocCount());
assertEquals(0d, histogram.getBuckets().get(1).getKey());
assertEquals(7, histogram.getBuckets().get(1).getDocCount());
assertEquals(5d, histogram.getBuckets().get(2).getKey());
assertEquals(2, histogram.getBuckets().get(2).getDocCount());
assertEquals(50d, histogram.getBuckets().get(3).getKey());
assertEquals(1, histogram.getBuckets().get(3).getDocCount());
}
}
}
use of org.apache.lucene.document.SortedNumericDocValuesField in project elasticsearch by elastic.
the class HistogramAggregatorTests method testExtendedBounds.
public void testExtendedBounds() throws Exception {
try (Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
for (double value : new double[] { 3.2, -5, -4.5, 4.3 }) {
Document doc = new Document();
doc.add(new SortedNumericDocValuesField("field", NumericUtils.doubleToSortableLong(value)));
w.addDocument(doc);
}
HistogramAggregationBuilder aggBuilder = new HistogramAggregationBuilder("my_agg").field("field").interval(5).extendedBounds(-12, 13);
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);
fieldType.setName("field");
try (IndexReader reader = w.getReader()) {
IndexSearcher searcher = new IndexSearcher(reader);
Histogram histogram = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType);
assertEquals(6, histogram.getBuckets().size());
assertEquals(-15d, histogram.getBuckets().get(0).getKey());
assertEquals(0, histogram.getBuckets().get(0).getDocCount());
assertEquals(-10d, histogram.getBuckets().get(1).getKey());
assertEquals(0, histogram.getBuckets().get(1).getDocCount());
assertEquals(-5d, histogram.getBuckets().get(2).getKey());
assertEquals(2, histogram.getBuckets().get(2).getDocCount());
assertEquals(0d, histogram.getBuckets().get(3).getKey());
assertEquals(2, histogram.getBuckets().get(3).getDocCount());
assertEquals(5d, histogram.getBuckets().get(4).getKey());
assertEquals(0, histogram.getBuckets().get(4).getDocCount());
assertEquals(10d, histogram.getBuckets().get(5).getKey());
assertEquals(0, histogram.getBuckets().get(5).getDocCount());
}
}
}
use of org.apache.lucene.document.SortedNumericDocValuesField in project elasticsearch by elastic.
the class ScriptedMetricAggregatorTests method testScriptedMetricWithCombine.
/**
* test that combine script sums the list produced by the "mapScript"
*/
public void testScriptedMetricWithCombine() throws IOException {
try (Directory directory = newDirectory()) {
Integer numDocs = randomInt(100);
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
for (int i = 0; i < numDocs; i++) {
indexWriter.addDocument(singleton(new SortedNumericDocValuesField("number", i)));
}
}
try (IndexReader indexReader = DirectoryReader.open(directory)) {
ScriptedMetricAggregationBuilder aggregationBuilder = new ScriptedMetricAggregationBuilder(AGG_NAME);
aggregationBuilder.initScript(INIT_SCRIPT).mapScript(MAP_SCRIPT).combineScript(COMBINE_SCRIPT);
ScriptedMetric scriptedMetric = search(newSearcher(indexReader, true, true), new MatchAllDocsQuery(), aggregationBuilder);
assertEquals(AGG_NAME, scriptedMetric.getName());
assertNotNull(scriptedMetric.aggregation());
assertEquals(numDocs, scriptedMetric.aggregation());
}
}
}
use of org.apache.lucene.document.SortedNumericDocValuesField in project elasticsearch by elastic.
the class CollapsingTopDocsCollectorTests method testCollapseLong.
public void testCollapseLong() throws Exception {
CollapsingDocValuesProducer producer = new CollapsingDocValuesProducer<Long>() {
@Override
public Long randomGroup(int maxGroup) {
return randomNonNegativeLong() % maxGroup;
}
@Override
public void add(Document doc, Long value, boolean multivalued) {
if (multivalued) {
doc.add(new SortedNumericDocValuesField("field", value));
} else {
doc.add(new NumericDocValuesField("field", value));
}
}
@Override
public SortField sortField(boolean multivalued) {
if (multivalued) {
return new SortedNumericSortField("field", SortField.Type.LONG);
} else {
return new SortField("field", SortField.Type.LONG);
}
}
};
assertSearchCollapse(producer, true);
}
use of org.apache.lucene.document.SortedNumericDocValuesField in project elasticsearch by elastic.
the class CollapsingTopDocsCollectorTests method testCollapseFloat.
public void testCollapseFloat() throws Exception {
CollapsingDocValuesProducer producer = new CollapsingDocValuesProducer<Float>() {
@Override
public Float randomGroup(int maxGroup) {
return new Float(randomIntBetween(0, maxGroup - 1));
}
@Override
public void add(Document doc, Float value, boolean multivalued) {
if (multivalued) {
doc.add(new SortedNumericDocValuesField("field", NumericUtils.floatToSortableInt(value)));
} else {
doc.add(new NumericDocValuesField("field", Float.floatToIntBits(value)));
}
}
@Override
public SortField sortField(boolean multivalued) {
if (multivalued) {
return new SortedNumericSortField("field", SortField.Type.FLOAT);
} else {
return new SortField("field", SortField.Type.FLOAT);
}
}
};
assertSearchCollapse(producer, true);
}
Aggregations