Search in sources :

Example 1 with LegacyIndexHits

use of org.neo4j.kernel.api.LegacyIndexHits in project neo4j by neo4j.

the class LuceneLegacyIndex method query.

protected LegacyIndexHits query(Query query, String keyForDirectLookup, Object valueForDirectLookup, QueryContext additionalParametersOrNull) {
    List<EntityId> simpleTransactionStateIds = new ArrayList<>();
    Collection<EntityId> removedIdsFromTransactionState = Collections.emptySet();
    IndexSearcher fulltextTransactionStateSearcher = null;
    if (transaction != null) {
        if (keyForDirectLookup != null) {
            simpleTransactionStateIds.addAll(transaction.getAddedIds(this, keyForDirectLookup, valueForDirectLookup));
        } else {
            fulltextTransactionStateSearcher = transaction.getAdditionsAsSearcher(this, additionalParametersOrNull);
        }
        removedIdsFromTransactionState = keyForDirectLookup != null ? transaction.getRemovedIds(this, keyForDirectLookup, valueForDirectLookup) : transaction.getRemovedIds(this, query);
    }
    LegacyIndexHits idIterator = null;
    IndexReference searcher = null;
    dataSource.getReadLock();
    try {
        searcher = dataSource.getIndexSearcher(identifier);
    } finally {
        dataSource.releaseReadLock();
    }
    if (searcher != null) {
        try {
            // Gather all added ids from fulltextTransactionStateSearcher and simpleTransactionStateIds.
            PrimitiveLongSet idsModifiedInTransactionState = gatherIdsModifiedInTransactionState(simpleTransactionStateIds, fulltextTransactionStateSearcher, query);
            // Do the combined search from store and fulltext tx state
            DocToIdIterator hits = new DocToIdIterator(search(searcher, fulltextTransactionStateSearcher, query, additionalParametersOrNull, removedIdsFromTransactionState), removedIdsFromTransactionState, searcher, idsModifiedInTransactionState);
            idIterator = simpleTransactionStateIds.isEmpty() ? hits : new CombinedIndexHits(Arrays.<LegacyIndexHits>asList(hits, new ConstantScoreIterator(simpleTransactionStateIds, Float.NaN)));
        } catch (IOException e) {
            throw new RuntimeException("Unable to query " + this + " with " + query, e);
        }
    }
    // We've only got transaction state
    idIterator = idIterator == null ? new ConstantScoreIterator(simpleTransactionStateIds, 0) : idIterator;
    return idIterator;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) ArrayList(java.util.ArrayList) IOException(java.io.IOException) PrimitiveLongSet(org.neo4j.collection.primitive.PrimitiveLongSet) LegacyIndexHits(org.neo4j.kernel.api.LegacyIndexHits)

Example 2 with LegacyIndexHits

use of org.neo4j.kernel.api.LegacyIndexHits in project neo4j by neo4j.

the class LuceneBatchInserterIndex method query.

private IndexHits<Long> query(Query query, final String key, final Object value) {
    IndexSearcher searcher;
    try {
        searcher = searcherManager.acquire();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    try {
        DocValuesCollector collector = new DocValuesCollector(true);
        searcher.search(query, collector);
        IndexHits<Document> result = collector.getIndexHits(Sort.RELEVANCE);
        LegacyIndexHits primitiveHits = null;
        if (key == null || this.cache == null || !this.cache.containsKey(key)) {
            primitiveHits = new DocToIdIterator(result, Collections.<EntityId>emptyList(), null, PrimitiveLongCollections.emptySet());
        } else {
            primitiveHits = new DocToIdIterator(result, Collections.<EntityId>emptyList(), null, PrimitiveLongCollections.emptySet()) {

                private final Collection<EntityId> ids = new ArrayList<>();

                @Override
                protected boolean fetchNext() {
                    if (super.fetchNext()) {
                        ids.add(new EntityId.IdData(next));
                        return true;
                    }
                    addToCache(ids, key, value);
                    return false;
                }
            };
        }
        return wrapIndexHits(primitiveHits);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            searcherManager.release(searcher);
        } catch (IOException ignore) {
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) DocValuesCollector(org.neo4j.kernel.api.impl.index.collector.DocValuesCollector) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) LegacyIndexHits(org.neo4j.kernel.api.LegacyIndexHits)

Aggregations

IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 LegacyIndexHits (org.neo4j.kernel.api.LegacyIndexHits)2 Document (org.apache.lucene.document.Document)1 PrimitiveLongSet (org.neo4j.collection.primitive.PrimitiveLongSet)1 DocValuesCollector (org.neo4j.kernel.api.impl.index.collector.DocValuesCollector)1