use of org.apache.lucene.search.SortedNumericSortField in project crate by crate.
the class SortSymbolVisitor method mappedSortField.
private static SortField mappedSortField(Reference symbol, MappedFieldType fieldType, boolean reverse, NullValueOrder nullValueOrder) {
String fieldName = symbol.column().fqn();
MultiValueMode sortMode = reverse ? MultiValueMode.MAX : MultiValueMode.MIN;
switch(symbol.valueType().id()) {
case StringType.ID:
{
SortField sortField = new SortedSetSortField(fieldName, reverse, sortMode == MultiValueMode.MAX ? SortedSetSelector.Type.MAX : SortedSetSelector.Type.MIN);
sortField.setMissingValue(nullValueOrder == NullValueOrder.LAST ^ reverse ? SortedSetSortField.STRING_LAST : SortedSetSortField.STRING_FIRST);
return sortField;
}
case BooleanType.ID:
case ByteType.ID:
case ShortType.ID:
case IntegerType.ID:
case LongType.ID:
case TimestampType.ID_WITHOUT_TZ:
case TimestampType.ID_WITH_TZ:
{
SortedNumericSelector.Type selectorType = sortMode == MultiValueMode.MAX ? SortedNumericSelector.Type.MAX : SortedNumericSelector.Type.MIN;
var reducedType = SortField.Type.LONG;
var sortField = new SortedNumericSortField(fieldName, SortField.Type.LONG, reverse, selectorType);
sortField.setMissingValue(NullSentinelValues.nullSentinelForReducedType(reducedType, nullValueOrder, reverse));
return sortField;
}
case FloatType.ID:
{
SortedNumericSelector.Type selectorType = sortMode == MultiValueMode.MAX ? SortedNumericSelector.Type.MAX : SortedNumericSelector.Type.MIN;
var reducedType = SortField.Type.FLOAT;
var sortField = new SortedNumericSortField(fieldName, SortField.Type.FLOAT, reverse, selectorType);
sortField.setMissingValue(NullSentinelValues.nullSentinelForReducedType(reducedType, nullValueOrder, reverse));
return sortField;
}
case DoubleType.ID:
{
SortedNumericSelector.Type selectorType = sortMode == MultiValueMode.MAX ? SortedNumericSelector.Type.MAX : SortedNumericSelector.Type.MIN;
var reducedType = SortField.Type.DOUBLE;
var sortField = new SortedNumericSortField(fieldName, SortField.Type.DOUBLE, reverse, selectorType);
sortField.setMissingValue(NullSentinelValues.nullSentinelForReducedType(reducedType, nullValueOrder, reverse));
return sortField;
}
case GeoPointType.ID:
throw new IllegalArgumentException("can't sort on geo_point field without using specific sorting feature, like geo_distance");
default:
throw new UnsupportedOperationException("Cannot order on " + symbol + "::" + symbol.valueType());
}
}
use of org.apache.lucene.search.SortedNumericSortField in project crate by crate.
the class OrderedLuceneBatchIteratorFactoryTest method createOrderedCollector.
private LuceneOrderedDocCollector createOrderedCollector(IndexSearcher searcher, int shardId) {
CollectorContext collectorContext = new CollectorContext();
List<LuceneCollectorExpression<?>> expressions = Collections.singletonList(new OrderByCollectorExpression(reference, orderBy, o -> o));
return new LuceneOrderedDocCollector(new ShardId("dummy", UUIDs.randomBase64UUID(), shardId), searcher, new MatchAllDocsQuery(), null, false, // batchSize < 10 to have at least one searchMore call.
5, RamAccounting.NO_ACCOUNTING, collectorContext, f -> null, new Sort(new SortedNumericSortField(columnName, SortField.Type.LONG, reverseFlags[0])), expressions, expressions);
}
use of org.apache.lucene.search.SortedNumericSortField 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.search.SortedNumericSortField 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);
}
use of org.apache.lucene.search.SortedNumericSortField in project neo4j by neo4j.
the class TestLuceneIndex method queryAndSortNodesByNumericProperty.
private void queryAndSortNodesByNumericProperty(Index<Node> index, String numericProperty, Function<Integer, ? extends Number> expectedValueProvider) {
try (Transaction transaction = graphDb.beginTx()) {
QueryContext queryContext = new QueryContext(numericProperty + ":**");
queryContext.sort(new Sort(new SortedNumericSortField(numericProperty, SortField.Type.INT, false)));
IndexHits<Node> nodes = index.query(queryContext);
int nodeIndex = 0;
for (Node node : nodes) {
assertEquals("Nodes should be sorted by numeric property", expectedValueProvider.apply(nodeIndex++), node.getProperty(numericProperty));
}
transaction.success();
}
}
Aggregations