use of org.apache.lucene.search.Sort in project neo4j by neo4j.
the class PageOfRangesIterator method getRanges.
private ValuesIterator getRanges() {
if (rangesIterator != null) {
return rangesIterator;
}
try {
DocValuesCollector docValuesCollector = new DocValuesCollector();
searcher.search(query, docValuesCollector);
rangesIterator = docValuesCollector.getSortedValuesIterator(BitmapDocumentFormat.RANGE, new Sort(new SortField(BitmapDocumentFormat.RANGE, SortField.Type.LONG)));
return rangesIterator;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of org.apache.lucene.search.Sort in project neo4j by neo4j.
the class DocValuesCollectorTest method shouldReturnIndexHitsInGivenSortOrder.
@Test
public void shouldReturnIndexHitsInGivenSortOrder() throws Exception {
// given
DocValuesCollector collector = new DocValuesCollector(false);
IndexReaderStub readerStub = indexReaderWithMaxDocs(43);
// when
collector.doSetNextReader(readerStub.getContext());
collector.collect(1);
collector.collect(3);
collector.collect(37);
collector.collect(42);
// then
Sort byIdDescending = new Sort(new SortField("id", SortField.Type.LONG, true));
IndexHits<Document> indexHits = collector.getIndexHits(byIdDescending);
assertEquals(4, indexHits.size());
assertEquals("42", indexHits.next().get("id"));
assertEquals("37", indexHits.next().get("id"));
assertEquals("3", indexHits.next().get("id"));
assertEquals("1", indexHits.next().get("id"));
assertFalse(indexHits.hasNext());
}
use of org.apache.lucene.search.Sort 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.Sort in project graphdb by neo4j-attic.
the class FullTxData method internalQuery.
private Pair<Collection<Long>, TxData> internalQuery(Query query, QueryContext contextOrNull) {
if (this.directory == null) {
return Pair.<Collection<Long>, TxData>of(Collections.<Long>emptySet(), this);
}
try {
Sort sorting = contextOrNull != null ? contextOrNull.getSorting() : null;
boolean prioritizeCorrectness = contextOrNull == null || !contextOrNull.getTradeCorrectnessForSpeed();
query = includeOrphans(query);
Hits hits = new Hits(searcher(prioritizeCorrectness), query, null, sorting, prioritizeCorrectness);
Collection<Long> result = new ArrayList<Long>();
for (int i = 0; i < hits.length(); i++) {
result.add(Long.parseLong(hits.doc(i).getField(LuceneIndex.KEY_DOC_ID).stringValue()));
}
return Pair.<Collection<Long>, TxData>of(result, this);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of org.apache.lucene.search.Sort in project neo4j by neo4j.
the class FullTxData method internalQuery.
private Collection<EntityId> internalQuery(Query query, QueryContext contextOrNull) {
if (this.directory == null) {
return Collections.emptySet();
}
try {
Sort sorting = contextOrNull != null ? contextOrNull.getSorting() : null;
boolean prioritizeCorrectness = contextOrNull == null || !contextOrNull.getTradeCorrectnessForSpeed();
IndexSearcher theSearcher = searcher(prioritizeCorrectness);
query = includeOrphans(query);
DocValuesCollector docValuesCollector = new DocValuesCollector(prioritizeCorrectness);
theSearcher.search(query, docValuesCollector);
Collection<EntityId> result = new ArrayList<>();
PrimitiveLongIterator valuesIterator = docValuesCollector.getSortedValuesIterator(KEY_DOC_ID, sorting);
while (valuesIterator.hasNext()) {
result.add(new EntityId.IdData(valuesIterator.next()));
}
return result;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Aggregations