Search in sources :

Example 21 with SortField

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;
}
Also used : NumericRangeQuery(org.apache.lucene.search.NumericRangeQuery) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField)

Example 22 with SortField

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);
    }
}
Also used : DocValuesCollector(org.neo4j.kernel.api.impl.index.collector.DocValuesCollector) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) IOException(java.io.IOException)

Example 23 with SortField

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());
}
Also used : IndexReaderStub(org.neo4j.kernel.api.impl.index.IndexReaderStub) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) Document(org.apache.lucene.document.Document) Test(org.junit.Test)

Example 24 with SortField

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);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) Term(org.apache.lucene.index.Term) IOException(java.io.IOException)

Example 25 with SortField

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);
    }
}
Also used : DynamicOperandImpl(org.apache.jackrabbit.spi.commons.query.qom.DynamicOperandImpl) DefaultTraversingQOMTreeVisitor(org.apache.jackrabbit.spi.commons.query.qom.DefaultTraversingQOMTreeVisitor) NodeLocalNameImpl(org.apache.jackrabbit.spi.commons.query.qom.NodeLocalNameImpl) OrderingImpl(org.apache.jackrabbit.spi.commons.query.qom.OrderingImpl) UpperCaseImpl(org.apache.jackrabbit.spi.commons.query.qom.UpperCaseImpl) FullTextSearchScoreImpl(org.apache.jackrabbit.spi.commons.query.qom.FullTextSearchScoreImpl) NodeNameImpl(org.apache.jackrabbit.spi.commons.query.qom.NodeNameImpl) SortField(org.apache.lucene.search.SortField) RepositoryException(javax.jcr.RepositoryException) QOMTreeVisitor(org.apache.jackrabbit.spi.commons.query.qom.QOMTreeVisitor) DefaultTraversingQOMTreeVisitor(org.apache.jackrabbit.spi.commons.query.qom.DefaultTraversingQOMTreeVisitor) RepositoryException(javax.jcr.RepositoryException) Name(org.apache.jackrabbit.spi.Name) LowerCaseImpl(org.apache.jackrabbit.spi.commons.query.qom.LowerCaseImpl) LengthImpl(org.apache.jackrabbit.spi.commons.query.qom.LengthImpl) PropertyValueImpl(org.apache.jackrabbit.spi.commons.query.qom.PropertyValueImpl)

Aggregations

SortField (org.apache.lucene.search.SortField)230 Sort (org.apache.lucene.search.Sort)174 Document (org.apache.lucene.document.Document)116 Directory (org.apache.lucene.store.Directory)110 IndexSearcher (org.apache.lucene.search.IndexSearcher)90 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)75 TopDocs (org.apache.lucene.search.TopDocs)74 IndexReader (org.apache.lucene.index.IndexReader)65 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)62 SortedNumericSortField (org.apache.lucene.search.SortedNumericSortField)56 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)56 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)49 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)37 TermQuery (org.apache.lucene.search.TermQuery)36 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)32 Query (org.apache.lucene.search.Query)29 Term (org.apache.lucene.index.Term)25 BytesRef (org.apache.lucene.util.BytesRef)25 TopFieldDocs (org.apache.lucene.search.TopFieldDocs)24 StoredField (org.apache.lucene.document.StoredField)23