use of org.apache.lucene.search.SortField in project neo4j-mobile-android by neo4j-contrib.
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();
int fieldType = SortField.INT;
if (number instanceof Long) {
fieldType = SortField.LONG;
} else if (number instanceof Float) {
fieldType = SortField.FLOAT;
} else if (number instanceof Double) {
fieldType = SortField.DOUBLE;
}
sort(new Sort(new SortField(key, fieldType, reversed)));
return this;
}
use of org.apache.lucene.search.SortField 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.SortField 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.SortField in project zm-mailbox by Zimbra.
the class LuceneIndex method warmup.
/**
* Runs a common search query + common sort order (and throw away the result) to warm up the Lucene cache and OS
* file system cache.
*/
@Override
public synchronized void warmup() {
if (SEARCHER_CACHE.asMap().containsKey(mailbox.getId()) || GAL_SEARCHER_CACHE.containsKey(mailbox.getId())) {
// already warmed up
return;
}
long start = System.currentTimeMillis();
IndexSearcher searcher = null;
try {
searcher = (IndexSearcher) openSearcher();
searcher.search(new TermQuery(new Term(LuceneFields.L_CONTENT, "zimbra")), 1, new Sort(new SortField(LuceneFields.L_SORT_DATE, SortField.STRING, true)));
} catch (IOException e) {
ZimbraLog.search.warn("Failed to warm up", e);
} finally {
Closeables.closeQuietly(searcher);
}
ZimbraLog.search.debug("WarmUpLuceneSearcher elapsed=%d", System.currentTimeMillis() - start);
}
use of org.apache.lucene.search.SortField in project jackrabbit by apache.
the class Ordering method fromQOM.
/**
* Creates an ordering from a JCR QOM ordering.
*
* @param ordering the JCR QOM ordering specification.
* @param scs the sort comparator source from the search index.
* @param nsMappings the index internal namespace mappings.
* @return an ordering.
* @throws RepositoryException if an error occurs while translating the JCR
* QOM ordering.
*/
public static Ordering fromQOM(final OrderingImpl ordering, final SharedFieldComparatorSource scs, final NamespaceMappings nsMappings) throws RepositoryException {
final Name[] selectorName = new Name[1];
QOMTreeVisitor visitor = new DefaultTraversingQOMTreeVisitor() {
public Object visit(LengthImpl node, Object data) throws Exception {
PropertyValueImpl propValue = (PropertyValueImpl) node.getPropertyValue();
selectorName[0] = propValue.getSelectorQName();
return new SortField(propValue.getPropertyQName().toString(), new LengthSortComparator(nsMappings), !ordering.isAscending());
}
public Object visit(LowerCaseImpl node, Object data) throws Exception {
SortField sf = (SortField) ((DynamicOperandImpl) node.getOperand()).accept(this, data);
selectorName[0] = node.getSelectorQName();
return new SortField(sf.getField(), new LowerCaseSortComparator(sf.getComparatorSource()), !ordering.isAscending());
}
public Object visit(UpperCaseImpl node, Object data) throws Exception {
SortField sf = (SortField) ((DynamicOperandImpl) node.getOperand()).accept(this, data);
selectorName[0] = node.getSelectorQName();
return new SortField(sf.getField(), new UpperCaseSortComparator(sf.getComparatorSource()), !ordering.isAscending());
}
public Object visit(FullTextSearchScoreImpl node, Object data) throws Exception {
selectorName[0] = node.getSelectorQName();
return new SortField(null, SortField.SCORE, !ordering.isAscending());
}
public Object visit(NodeLocalNameImpl node, Object data) throws Exception {
selectorName[0] = node.getSelectorQName();
return new SortField(FieldNames.LOCAL_NAME, SortField.STRING, !ordering.isAscending());
}
public Object visit(NodeNameImpl node, Object data) throws Exception {
selectorName[0] = node.getSelectorQName();
return new SortField(FieldNames.LABEL, SortField.STRING, !ordering.isAscending());
}
public Object visit(PropertyValueImpl node, Object data) throws Exception {
selectorName[0] = node.getSelectorQName();
return new SortField(node.getPropertyQName().toString(), scs, !ordering.isAscending());
}
public Object visit(OrderingImpl node, Object data) throws Exception {
return ((DynamicOperandImpl) node.getOperand()).accept(this, data);
}
};
try {
SortField field = (SortField) ordering.accept(visitor, null);
return new Ordering(selectorName[0], field);
} catch (Exception e) {
throw new RepositoryException(e);
}
}
Aggregations