Search in sources :

Example 16 with Sort

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);
    }
}
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 17 with Sort

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());
}
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 18 with Sort

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

Example 19 with Sort

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);
    }
}
Also used : ArrayList(java.util.ArrayList) Collection(java.util.Collection) Sort(org.apache.lucene.search.Sort) IOException(java.io.IOException)

Example 20 with Sort

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

Aggregations

Sort (org.apache.lucene.search.Sort)244 SortField (org.apache.lucene.search.SortField)181 Document (org.apache.lucene.document.Document)139 Directory (org.apache.lucene.store.Directory)129 IndexSearcher (org.apache.lucene.search.IndexSearcher)108 TopDocs (org.apache.lucene.search.TopDocs)92 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)86 IndexReader (org.apache.lucene.index.IndexReader)72 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)72 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)61 SortedNumericSortField (org.apache.lucene.search.SortedNumericSortField)56 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)51 TermQuery (org.apache.lucene.search.TermQuery)50 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)42 Query (org.apache.lucene.search.Query)41 ArrayList (java.util.ArrayList)37 Term (org.apache.lucene.index.Term)36 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)35 BytesRef (org.apache.lucene.util.BytesRef)32 TopFieldDocs (org.apache.lucene.search.TopFieldDocs)30