use of org.apache.lucene.search.SortedNumericSortField in project elasticsearch by elastic.
the class CollapsingTopDocsCollectorTests method testCollapseDouble.
public void testCollapseDouble() throws Exception {
CollapsingDocValuesProducer producer = new CollapsingDocValuesProducer<Double>() {
@Override
public Double randomGroup(int maxGroup) {
return new Double(randomIntBetween(0, maxGroup - 1));
}
@Override
public void add(Document doc, Double value, boolean multivalued) {
if (multivalued) {
doc.add(new SortedNumericDocValuesField("field", NumericUtils.doubleToSortableLong(value)));
} else {
doc.add(new NumericDocValuesField("field", Double.doubleToLongBits(value)));
}
}
@Override
public SortField sortField(boolean multivalued) {
if (multivalued) {
return new SortedNumericSortField("field", SortField.Type.DOUBLE);
} else {
return new SortField("field", SortField.Type.DOUBLE);
}
}
};
assertSearchCollapse(producer, true);
}
use of org.apache.lucene.search.SortedNumericSortField in project elasticsearch by elastic.
the class CollapsingTopDocsCollectorTests method testCollapseInt.
public void testCollapseInt() throws Exception {
CollapsingDocValuesProducer producer = new CollapsingDocValuesProducer<Integer>() {
@Override
public Integer randomGroup(int maxGroup) {
return randomIntBetween(0, maxGroup - 1);
}
@Override
public void add(Document doc, Integer 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.INT);
} else {
return new SortField("field", SortField.Type.INT);
}
}
};
assertSearchCollapse(producer, true);
}
use of org.apache.lucene.search.SortedNumericSortField in project neo4j by neo4j.
the class QueryContext method sortNumeric.
/**
* Sort the results of a numeric range query if the query in this context
* is a {@link NumericRangeQuery}, see {@link #numericRange(String, Number, Number)},
* Otherwise an {@link IllegalStateException} will be thrown.
*
* @param key the key to sort on.
* @param reversed if the sort order should be reversed or not. {@code true}
* for lowest first (ascending), {@code false} for highest first (descending)
* @return a QueryContext with sorting by numeric value.
*/
public QueryContext sortNumeric(String key, boolean reversed) {
if (!(queryOrQueryObject instanceof NumericRangeQuery)) {
throw new IllegalStateException("Not a numeric range query");
}
Number number = ((NumericRangeQuery) queryOrQueryObject).getMin();
number = number != null ? number : ((NumericRangeQuery) queryOrQueryObject).getMax();
SortField.Type fieldType = SortField.Type.INT;
if (number instanceof Long) {
fieldType = SortField.Type.LONG;
} else if (number instanceof Float) {
fieldType = SortField.Type.FLOAT;
} else if (number instanceof Double) {
fieldType = SortField.Type.DOUBLE;
}
sort(new Sort(new SortedNumericSortField(key, fieldType, reversed)));
return this;
}
use of org.apache.lucene.search.SortedNumericSortField in project lucene-solr by apache.
the class BaseSegmentInfoFormatTestCase method randomIndexSortField.
private SortField randomIndexSortField() {
boolean reversed = random().nextBoolean();
SortField sortField;
switch(random().nextInt(10)) {
case 0:
sortField = new SortField(TestUtil.randomSimpleString(random()), SortField.Type.INT, reversed);
if (random().nextBoolean()) {
sortField.setMissingValue(random().nextInt());
}
break;
case 1:
sortField = new SortedNumericSortField(TestUtil.randomSimpleString(random()), SortField.Type.INT, reversed);
if (random().nextBoolean()) {
sortField.setMissingValue(random().nextInt());
}
break;
case 2:
sortField = new SortField(TestUtil.randomSimpleString(random()), SortField.Type.LONG, reversed);
if (random().nextBoolean()) {
sortField.setMissingValue(random().nextLong());
}
break;
case 3:
sortField = new SortedNumericSortField(TestUtil.randomSimpleString(random()), SortField.Type.LONG, reversed);
if (random().nextBoolean()) {
sortField.setMissingValue(random().nextLong());
}
break;
case 4:
sortField = new SortField(TestUtil.randomSimpleString(random()), SortField.Type.FLOAT, reversed);
if (random().nextBoolean()) {
sortField.setMissingValue(random().nextFloat());
}
break;
case 5:
sortField = new SortedNumericSortField(TestUtil.randomSimpleString(random()), SortField.Type.FLOAT, reversed);
if (random().nextBoolean()) {
sortField.setMissingValue(random().nextFloat());
}
break;
case 6:
sortField = new SortField(TestUtil.randomSimpleString(random()), SortField.Type.DOUBLE, reversed);
if (random().nextBoolean()) {
sortField.setMissingValue(random().nextDouble());
}
break;
case 7:
sortField = new SortedNumericSortField(TestUtil.randomSimpleString(random()), SortField.Type.DOUBLE, reversed);
if (random().nextBoolean()) {
sortField.setMissingValue(random().nextDouble());
}
break;
case 8:
sortField = new SortField(TestUtil.randomSimpleString(random()), SortField.Type.STRING, reversed);
if (random().nextBoolean()) {
sortField.setMissingValue(SortField.STRING_LAST);
}
break;
case 9:
sortField = new SortedSetSortField(TestUtil.randomSimpleString(random()), reversed);
if (random().nextBoolean()) {
sortField.setMissingValue(SortField.STRING_LAST);
}
break;
default:
sortField = null;
fail();
}
return sortField;
}
use of org.apache.lucene.search.SortedNumericSortField in project lucene-solr by apache.
the class TestIndexSorting method testMissingMultiValuedIntFirst.
public void testMissingMultiValuedIntFirst() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
SortField sortField = new SortedNumericSortField("foo", SortField.Type.INT);
sortField.setMissingValue(Integer.valueOf(Integer.MIN_VALUE));
Sort indexSort = new Sort(sortField);
iwc.setIndexSort(indexSort);
IndexWriter w = new IndexWriter(dir, iwc);
Document doc = new Document();
doc.add(new NumericDocValuesField("id", 3));
doc.add(new SortedNumericDocValuesField("foo", 18));
doc.add(new SortedNumericDocValuesField("foo", 187667));
w.addDocument(doc);
// so we get more than one segment, so that forceMerge actually does merge, since we only get a sorted segment by merging:
w.commit();
// missing
doc = new Document();
doc.add(new NumericDocValuesField("id", 1));
w.addDocument(doc);
w.commit();
doc = new Document();
doc.add(new NumericDocValuesField("id", 2));
doc.add(new SortedNumericDocValuesField("foo", 7));
doc.add(new SortedNumericDocValuesField("foo", 34));
w.addDocument(doc);
w.forceMerge(1);
DirectoryReader r = DirectoryReader.open(w);
LeafReader leaf = getOnlyLeafReader(r);
assertEquals(3, leaf.maxDoc());
NumericDocValues values = leaf.getNumericDocValues("id");
assertEquals(0, values.nextDoc());
assertEquals(1, values.longValue());
assertEquals(1, values.nextDoc());
assertEquals(2, values.longValue());
assertEquals(2, values.nextDoc());
assertEquals(3, values.longValue());
r.close();
w.close();
dir.close();
}
Aggregations