use of org.apache.lucene.document.SortedNumericDocValuesField in project elasticsearch by elastic.
the class MinAggregatorTests method testMinAggregator_noValue.
public void testMinAggregator_noValue() throws Exception {
Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
Document document = new Document();
document.add(new SortedNumericDocValuesField("number1", 7));
indexWriter.addDocument(document);
document = new Document();
document.add(new SortedNumericDocValuesField("number1", 3));
indexWriter.addDocument(document);
document = new Document();
document.add(new SortedNumericDocValuesField("number1", 1));
indexWriter.addDocument(document);
indexWriter.close();
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("_name").field("number2");
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
fieldType.setName("number2");
try (MinAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
aggregator.preCollection();
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
aggregator.postCollection();
InternalMin result = (InternalMin) aggregator.buildAggregation(0L);
assertEquals(Double.POSITIVE_INFINITY, result.getValue(), 0);
}
indexReader.close();
directory.close();
}
use of org.apache.lucene.document.SortedNumericDocValuesField in project elasticsearch by elastic.
the class DocValuesSliceQueryTests method testSearch.
public void testSearch() throws Exception {
final int numDocs = randomIntBetween(100, 200);
final Directory dir = newDirectory();
final RandomIndexWriter w = new RandomIndexWriter(random(), dir);
int max = randomIntBetween(2, 10);
int[] sliceCounters1 = new int[max];
int[] sliceCounters2 = new int[max];
Set<String> keys = new HashSet<>();
for (int i = 0; i < numDocs; ++i) {
Document doc = new Document();
String uuid = UUIDs.base64UUID();
int intValue = randomInt();
long doubleValue = NumericUtils.doubleToSortableLong(randomDouble());
doc.add(new StringField("uuid", uuid, Field.Store.YES));
doc.add(new SortedNumericDocValuesField("intField", intValue));
doc.add(new SortedNumericDocValuesField("doubleField", doubleValue));
w.addDocument(doc);
sliceCounters1[Math.floorMod(BitMixer.mix((long) intValue), max)]++;
sliceCounters2[Math.floorMod(BitMixer.mix(doubleValue), max)]++;
keys.add(uuid);
}
final IndexReader reader = w.getReader();
final IndexSearcher searcher = newSearcher(reader);
for (int id = 0; id < max; id++) {
DocValuesSliceQuery query1 = new DocValuesSliceQuery("intField", id, max);
assertThat(searcher.count(query1), equalTo(sliceCounters1[id]));
DocValuesSliceQuery query2 = new DocValuesSliceQuery("doubleField", id, max);
assertThat(searcher.count(query2), equalTo(sliceCounters2[id]));
searcher.search(query1, new Collector() {
@Override
public LeafCollector getLeafCollector(LeafReaderContext context) throws IOException {
return new LeafCollector() {
@Override
public void setScorer(Scorer scorer) throws IOException {
}
@Override
public void collect(int doc) throws IOException {
Document d = context.reader().document(doc, Collections.singleton("uuid"));
String uuid = d.get("uuid");
assertThat(keys.contains(uuid), equalTo(true));
keys.remove(uuid);
}
};
}
@Override
public boolean needsScores() {
return false;
}
});
}
assertThat(keys.size(), equalTo(0));
w.close();
reader.close();
dir.close();
}
use of org.apache.lucene.document.SortedNumericDocValuesField in project elasticsearch by elastic.
the class ExtendedStatsAggregatorTests method testRandomDoubles.
public void testRandomDoubles() throws IOException {
MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);
ft.setName("field");
final ExtendedSimpleStatsAggregator expected = new ExtendedSimpleStatsAggregator();
testCase(ft, iw -> {
int numDocs = randomIntBetween(10, 50);
for (int i = 0; i < numDocs; i++) {
Document doc = new Document();
int numValues = randomIntBetween(1, 5);
for (int j = 0; j < numValues; j++) {
double value = randomDoubleBetween(-100d, 100d, true);
long valueAsLong = NumericUtils.doubleToSortableLong(value);
doc.add(new SortedNumericDocValuesField("field", valueAsLong));
expected.add(value);
}
iw.addDocument(doc);
}
}, stats -> {
assertEquals(expected.count, stats.getCount(), 0);
assertEquals(expected.sum, stats.getSum(), TOLERANCE);
assertEquals(expected.min, stats.getMin(), 0);
assertEquals(expected.max, stats.getMax(), 0);
assertEquals(expected.sum / expected.count, stats.getAvg(), TOLERANCE);
assertEquals(expected.sumOfSqrs, stats.getSumOfSquares(), TOLERANCE);
assertEquals(expected.stdDev(), stats.getStdDeviation(), TOLERANCE);
assertEquals(expected.variance(), stats.getVariance(), TOLERANCE);
assertEquals(expected.stdDevBound(ExtendedStats.Bounds.LOWER, stats.getSigma()), stats.getStdDeviationBound(ExtendedStats.Bounds.LOWER), TOLERANCE);
assertEquals(expected.stdDevBound(ExtendedStats.Bounds.UPPER, stats.getSigma()), stats.getStdDeviationBound(ExtendedStats.Bounds.UPPER), TOLERANCE);
});
}
use of org.apache.lucene.document.SortedNumericDocValuesField in project elasticsearch by elastic.
the class ExtendedStatsAggregatorTests method testRandomLongs.
public void testRandomLongs() throws IOException {
MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
ft.setName("field");
final ExtendedSimpleStatsAggregator expected = new ExtendedSimpleStatsAggregator();
testCase(ft, iw -> {
int numDocs = randomIntBetween(10, 50);
for (int i = 0; i < numDocs; i++) {
Document doc = new Document();
int numValues = randomIntBetween(1, 5);
for (int j = 0; j < numValues; j++) {
long value = randomIntBetween(-100, 100);
doc.add(new SortedNumericDocValuesField("field", value));
expected.add(value);
}
iw.addDocument(doc);
}
}, stats -> {
assertEquals(expected.count, stats.getCount(), 0);
assertEquals(expected.sum, stats.getSum(), TOLERANCE);
assertEquals(expected.min, stats.getMin(), 0);
assertEquals(expected.max, stats.getMax(), 0);
assertEquals(expected.sum / expected.count, stats.getAvg(), TOLERANCE);
assertEquals(expected.sumOfSqrs, stats.getSumOfSquares(), TOLERANCE);
assertEquals(expected.stdDev(), stats.getStdDeviation(), TOLERANCE);
assertEquals(expected.variance(), stats.getVariance(), TOLERANCE);
assertEquals(expected.stdDevBound(ExtendedStats.Bounds.LOWER, stats.getSigma()), stats.getStdDeviationBound(ExtendedStats.Bounds.LOWER), TOLERANCE);
assertEquals(expected.stdDevBound(ExtendedStats.Bounds.UPPER, stats.getSigma()), stats.getStdDeviationBound(ExtendedStats.Bounds.UPPER), TOLERANCE);
});
}
use of org.apache.lucene.document.SortedNumericDocValuesField in project elasticsearch by elastic.
the class MaxAggregatorTests method testNoMatchingField.
public void testNoMatchingField() throws IOException {
testCase(new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 7)));
iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 1)));
}, max -> {
assertEquals(Double.NEGATIVE_INFINITY, max.getValue(), 0);
});
}
Aggregations