Example usage for org.apache.lucene.search Sort INDEXORDER

List of usage examples for org.apache.lucene.search Sort INDEXORDER

Introduction

In this page you can find the example usage for org.apache.lucene.search Sort INDEXORDER.

Prototype

Sort INDEXORDER

To view the source code for org.apache.lucene.search Sort INDEXORDER.

Click Source Link

Document

Represents sorting by index order.

Usage

From source file:org.neo4j.kernel.api.impl.index.collector.DocValuesCollector.java

License:Open Source License

/**
 * Replay the search and collect every hit into TopDocs. One {@code ScoreDoc} is allocated
 * for every hit and the {@code Document} instance is loaded lazily with on every iteration step.
 *
 * @param sort how to sort the iterator. If this is null, results will be in index-order.
 * @return an indexhits iterator over all matches
 * @throws IOException//from   w  w  w.  ja v a2s.c o  m
 */
public IndexHits<Document> getIndexHits(Sort sort) throws IOException {
    List<MatchingDocs> matchingDocs = getMatchingDocs();
    int size = getTotalHits();
    if (size == 0) {
        return EMPTY_INDEX_HITS;
    }

    if (sort == null || sort == Sort.INDEXORDER) {
        return new DocsInIndexOrderIterator(matchingDocs, size, isKeepScores());
    }

    TopDocs topDocs = getTopDocs(sort, size);
    LeafReaderContext[] contexts = getLeafReaderContexts(matchingDocs);
    return new TopDocsIterator(topDocs, contexts);
}

From source file:org.neo4j.kernel.api.impl.index.DocValuesCollector.java

License:Open Source License

/**
 * Replay the search and collect every hit into TopDocs. One {@code ScoreDoc} is allocated
 * for every hit and the {@code Document} instance is loaded lazily with on every iteration step.
 *
 * @param sort how to sort the iterator. If this is null, results will be in index-order.
 * @return an indexhits iterator over all matches
 * @throws IOException//w ww.java2  s  .  c o m
 */
public IndexHits<Document> getIndexHits(Sort sort) throws IOException {
    List<MatchingDocs> matchingDocs = getMatchingDocs();
    int size = getTotalHits();
    if (size == 0) {
        return new AbstractIndexHits<Document>() {
            @Override
            public int size() {
                return 0;
            }

            @Override
            public float currentScore() {
                return Float.NaN;
            }

            @Override
            protected Document fetchNextOrNull() {
                return null;
            }
        };
    }

    if (sort == null || sort == Sort.INDEXORDER) {
        return new DocsInIndexOrderIterator(matchingDocs, size, isKeepScores());
    }

    TopDocs topDocs = getTopDocs(sort, size);
    LeafReaderContext[] contexts = getLeafReaderContexts(matchingDocs);
    return new TopDocsIterator(topDocs, contexts);
}

From source file:org.pageseeder.flint.lucene.LuceneIndexQueries.java

License:Apache License

/**
 * Run a search on the given Index.//from w w  w.j  av a  2s. c om
 *
 * @param index  the Index to run the search on
 * @param query  the query to run
 * @param paging paging details (can be <code>null</code>)
 *
 * @return the search results
 *
 * @throws IndexException if any error occurred while performing the search
 */
public static SearchResults query(Index index, SearchQuery query, SearchPaging paging) throws IndexException {
    LuceneIndexIO io = getIndexIO(index);
    IndexSearcher searcher = io.bookSearcher();
    if (searcher != null) {
        try {
            Query lquery = query.toQuery();
            if (lquery == null) {
                io.releaseSearcher(searcher);
                throw new IndexException("Failed performing a query on the Index because the query is null",
                        new NullPointerException("Null query"));
            }
            LOGGER.debug("Performing search [{}] on index {}", query, index);
            Sort sort = query.getSort();
            if (sort == null) {
                sort = Sort.INDEXORDER;
            }
            // load the scores
            TopFieldCollector tfc = TopFieldCollector.create(sort, paging.getHitsPerPage() * paging.getPage(),
                    true, true, false);
            searcher.search(lquery, tfc);
            return new SearchResults(query, tfc.topDocs().scoreDocs, tfc.getTotalHits(), paging, io, searcher);
        } catch (IOException e) {
            io.releaseSearcher(searcher);
            throw new IndexException("Failed performing a query on the Index because of an I/O problem", e);
        }
    }
    return null;
}

From source file:org.pageseeder.flint.lucene.LuceneIndexQueries.java

License:Apache License

/**
 * Run a search on the given Indexes./*from   w w w .  j a v a  2s  . c o m*/
 *
 * @param indexes  the Indexes to run the search on
 * @param query    the query to run
 * @param paging   paging details (can be <code>null</code>)
 *
 * @return the search results
 *
 * @throws IndexException if any error occurred while performing the search
 */
public static SearchResults query(List<Index> indexes, SearchQuery query, SearchPaging paging)
        throws IndexException {
    Query lquery = query.toQuery();
    if (lquery == null)
        throw new IndexException("Failed performing a query because the query is null",
                new NullPointerException("Null query"));
    // find all readers
    Map<LuceneIndexIO, IndexReader> readersMap = new HashMap<>();
    IndexReader[] readers = new IndexReader[indexes.size()];
    // grab a reader for each indexes
    for (int i = 0; i < indexes.size(); i++) {
        LuceneIndexIO io = getIndexIO(indexes.get(i));
        // make sure index has been setup
        if (io != null) {
            // grab what we need
            IndexReader reader = io.bookReader();
            readers[i] = reader;
            readersMap.put(io, reader);
        }
    }
    try {
        MultiReader reader = new MultiReader(readers);
        IndexSearcher searcher = new IndexSearcher(reader);
        LOGGER.debug("Performing search [{}] on {} indexes", query, readers.length);
        Sort sort = query.getSort();
        if (sort == null)
            sort = Sort.INDEXORDER;
        // load the scores
        TopFieldDocs results = searcher.search(lquery, paging.getHitsPerPage() * paging.getPage(), sort);
        return new SearchResults(query, results, paging, readersMap, searcher);
    } catch (IOException e) {
        for (LuceneIndexIO io : readersMap.keySet())
            io.releaseReader(readersMap.get(io));
        throw new IndexException("Failed performing a query on the Index because of an I/O problem", e);
    }
}

From source file:org.pageseeder.flint.lucene.query.PredicateSearchQuery.java

License:Apache License

/**
 * Creates new predicate search query.//ww  w. jav a  2  s . com
 *
 * @param predicate The predicate for this query.
 * @param sortField The field name to use to order the results.
 *
 * @throws IllegalArgumentException If the predicate is <code>null</code>.
 */
public PredicateSearchQuery(String predicate, String sortField) throws IllegalArgumentException {
    this(predicate, sortField == null ? Sort.INDEXORDER : new Sort(new SortField(sortField, Type.STRING)));
}

From source file:org.pageseeder.flint.lucene.query.PredicateSearchQuery.java

License:Apache License

/**
 * Creates new predicate search query./*  w w w  .  jav  a  2  s.  c  om*/
 *
 * @param predicate The predicate for this query.
 * @param analyzer  The analyzer to use for the query, should be the same as the one used to write the Index.
 * @param sortField The field name to use to order the results.
 *
 * @throws IllegalArgumentException If the predicate is <code>null</code>.
 */
public PredicateSearchQuery(String predicate, Analyzer analyzer, String sortField)
        throws IllegalArgumentException {
    this(predicate, analyzer,
            sortField == null ? Sort.INDEXORDER : new Sort(new SortField(sortField, Type.STRING)));
}

From source file:org.pageseeder.flint.lucene.query.PredicateSearchQuery.java

License:Apache License

/**
 * Creates new predicate search query./*from  w ww.j  a  v  a 2  s.  c o  m*/
 *
 * @param predicate The predicate for this query.
 *
 * @throws IllegalArgumentException If the predicate is <code>null</code>.
 */
public PredicateSearchQuery(String predicate) throws IllegalArgumentException {
    this(predicate, Sort.INDEXORDER);
}

From source file:org.pageseeder.flint.lucene.query.PredicateSearchQuery.java

License:Apache License

/**
 * Creates new predicate search query./*  w w  w  .ja v a  2  s  .com*/
 *
 * @param predicate The predicate for this query.
 * @param analyzer  The analyzer to use for the query, should be the same as the one used to write the Index.
 *
 * @throws IllegalArgumentException If the predicate is <code>null</code>.
 */
public PredicateSearchQuery(String predicate, Analyzer analyzer) throws IllegalArgumentException {
    this(predicate, analyzer, Sort.INDEXORDER);
}

From source file:org.sindice.siren.search.TestSirenNumericRangeQuery32.java

License:Open Source License

/** test for both constant score and boolean query, the other tests only use the constant score mode */
private void testRange(final int precisionStep) throws Exception {
    final String field = "field" + precisionStep;
    final int count = 3000;
    final int lower = (distance * 3 / 2) + startOffset, upper = lower + count * distance + (distance / 3);
    final SirenNumericRangeQuery<Integer> q = SirenNumericRangeQuery.newIntRange(field, precisionStep, lower,
            upper, true, true);//from   w ww . j av  a 2  s .  co  m
    final SirenCellQuery cq = new SirenCellQuery(q);
    cq.setConstraint(2);

    TopDocs topDocs;
    String type;
    q.clearTotalNumberOfTerms();

    type = " (constant score boolean rewrite)";
    q.setRewriteMethod(SirenMultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE);
    topDocs = searcher.search(cq, null, noDocs, Sort.INDEXORDER);

    final ScoreDoc[] sd = topDocs.scoreDocs;
    assertNotNull(sd);
    assertEquals("Score doc count" + type, count, sd.length);
    Document doc = searcher.doc(sd[0].doc);
    assertEquals("First doc" + type, 2 * distance + startOffset,
            Integer.parseInt(getLiteralValue(doc.get(field))));
    doc = searcher.doc(sd[sd.length - 1].doc);
    assertEquals("Last doc" + type, (1 + count) * distance + startOffset,
            Integer.parseInt(getLiteralValue(doc.get(field))));
}

From source file:org.sindice.siren.search.TestSirenNumericRangeQuery32.java

License:Open Source License

@Test
public void testInverseRange() throws Exception {
    SirenNumericRangeQuery<Integer> q = SirenNumericRangeQuery.newIntRange("field8", 8, 1000, -1000, true,
            true);//from  w ww  .  j a va  2 s.c o m
    q.clearTotalNumberOfTerms();
    q.setRewriteMethod(SirenMultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE);
    TopDocs topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER);
    assertEquals("A inverse range should return the EMPTY_DOCIDSET instance", 0, topDocs.totalHits);

    q = SirenNumericRangeQuery.newIntRange("field8", 8, Integer.MAX_VALUE, null, false, false);
    q.clearTotalNumberOfTerms();
    q.setRewriteMethod(SirenMultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE);
    topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER);
    assertEquals("A exclusive range starting with Integer.MAX_VALUE should return the EMPTY_DOCIDSET instance",
            0, topDocs.totalHits);

    q = SirenNumericRangeQuery.newIntRange("field8", 8, null, Integer.MIN_VALUE, false, false);
    q.clearTotalNumberOfTerms();
    q.setRewriteMethod(SirenMultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE);
    topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER);
    assertEquals("A exclusive range ending with Integer.MIN_VALUE should return the EMPTY_DOCIDSET instance", 0,
            topDocs.totalHits);
}