Search in sources :

Example 1 with IndexHits

use of org.neo4j.graphdb.index.IndexHits in project neo4j-mobile-android by neo4j-contrib.

the class LuceneBatchInserterIndex method query.

private IndexHits<Long> query(Query query, final String key, final Object value) {
    try {
        Hits hits = new Hits(searcher(), query, null);
        HitsIterator result = new HitsIterator(hits);
        if (key == null || this.cache == null || !this.cache.containsKey(key)) {
            return new DocToIdIterator(result, Collections.<Long>emptyList(), null);
        } else {
            return new DocToIdIterator(result, Collections.<Long>emptyList(), null) {

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

                @Override
                protected Long fetchNextOrNull() {
                    Long result = super.fetchNextOrNull();
                    if (result != null) {
                        ids.add(result);
                    }
                    return result;
                }

                @Override
                protected void endReached() {
                    super.endReached();
                    addToCache(ids, key, value);
                }
            };
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : IndexHits(org.neo4j.graphdb.index.IndexHits) Collection(java.util.Collection) IOException(java.io.IOException)

Example 2 with IndexHits

use of org.neo4j.graphdb.index.IndexHits in project neo4j-mobile-android by neo4j-contrib.

the class LuceneIndex method query.

protected IndexHits<T> query(Query query, String keyForDirectLookup, Object valueForDirectLookup, QueryContext additionalParametersOrNull) {
    List<Long> ids = new ArrayList<Long>();
    LuceneXaConnection con = getReadOnlyConnection();
    LuceneTransaction luceneTx = con != null ? con.getLuceneTx() : null;
    Collection<Long> removedIds = Collections.emptySet();
    IndexSearcher additionsSearcher = null;
    if (luceneTx != null) {
        if (keyForDirectLookup != null) {
            ids.addAll(luceneTx.getAddedIds(this, keyForDirectLookup, valueForDirectLookup));
        } else {
            additionsSearcher = luceneTx.getAdditionsAsSearcher(this, additionalParametersOrNull);
        }
        removedIds = keyForDirectLookup != null ? luceneTx.getRemovedIds(this, keyForDirectLookup, valueForDirectLookup) : luceneTx.getRemovedIds(this, query);
    }
    service.dataSource().getReadLock();
    IndexHits<Long> idIterator = null;
    IndexSearcherRef searcher = null;
    try {
        searcher = service.dataSource().getIndexSearcher(identifier, true);
        if (searcher != null) {
            boolean foundInCache = false;
            LruCache<String, Collection<Long>> cachedIdsMap = null;
            if (keyForDirectLookup != null) {
                cachedIdsMap = service.dataSource().getFromCache(identifier, keyForDirectLookup);
                foundInCache = fillFromCache(cachedIdsMap, ids, keyForDirectLookup, valueForDirectLookup.toString(), removedIds);
            }
            if (!foundInCache) {
                DocToIdIterator searchedIds = new DocToIdIterator(search(searcher, query, additionalParametersOrNull, additionsSearcher, removedIds), removedIds, searcher);
                if (ids.isEmpty()) {
                    idIterator = searchedIds;
                } else {
                    Collection<IndexHits<Long>> iterators = new ArrayList<IndexHits<Long>>();
                    iterators.add(searchedIds);
                    iterators.add(new ConstantScoreIterator<Long>(ids, Float.NaN));
                    idIterator = new CombinedIndexHits<Long>(iterators);
                }
            }
        }
    } finally {
        // The DocToIdIterator closes the IndexSearchRef instance anyways,
        // or the LazyIterator if it's a lazy one. So no need here.
        service.dataSource().releaseReadLock();
    }
    idIterator = idIterator == null ? new ConstantScoreIterator<Long>(ids, 0) : idIterator;
    return newEntityIterator(idIterator);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) ArrayList(java.util.ArrayList) IndexHits(org.neo4j.graphdb.index.IndexHits) Collection(java.util.Collection)

Example 3 with IndexHits

use of org.neo4j.graphdb.index.IndexHits in project graphdb by neo4j-attic.

the class LuceneIndex method query.

protected IndexHits<T> query(Query query, String keyForDirectLookup, Object valueForDirectLookup, QueryContext additionalParametersOrNull) {
    List<Long> ids = new ArrayList<Long>();
    LuceneXaConnection con = getReadOnlyConnection();
    LuceneTransaction luceneTx = con != null ? con.getLuceneTx() : null;
    Collection<Long> removedIds = Collections.emptySet();
    Searcher additionsSearcher = null;
    if (luceneTx != null) {
        if (keyForDirectLookup != null) {
            ids.addAll(luceneTx.getAddedIds(this, keyForDirectLookup, valueForDirectLookup));
        } else {
            additionsSearcher = luceneTx.getAdditionsAsSearcher(this, additionalParametersOrNull);
        }
        removedIds = keyForDirectLookup != null ? luceneTx.getRemovedIds(this, keyForDirectLookup, valueForDirectLookup) : luceneTx.getRemovedIds(this, query);
    }
    service.dataSource().getReadLock();
    IndexHits<Long> idIterator = null;
    IndexSearcherRef searcher = null;
    try {
        searcher = service.dataSource().getIndexSearcher(identifier, true);
        if (searcher != null) {
            boolean foundInCache = false;
            LruCache<String, Collection<Long>> cachedIdsMap = null;
            if (keyForDirectLookup != null) {
                cachedIdsMap = service.dataSource().getFromCache(identifier, keyForDirectLookup);
                foundInCache = fillFromCache(cachedIdsMap, ids, keyForDirectLookup, valueForDirectLookup.toString(), removedIds);
            }
            if (!foundInCache) {
                DocToIdIterator searchedIds = new DocToIdIterator(search(searcher, query, additionalParametersOrNull, additionsSearcher, removedIds), removedIds, searcher);
                if (ids.isEmpty()) {
                    idIterator = searchedIds;
                } else {
                    Collection<IndexHits<Long>> iterators = new ArrayList<IndexHits<Long>>();
                    iterators.add(searchedIds);
                    iterators.add(new ConstantScoreIterator<Long>(ids, Float.NaN));
                    idIterator = new CombinedIndexHits<Long>(iterators);
                }
            }
        }
    } finally {
        // The DocToIdIterator closes the IndexSearchRef instance anyways,
        // or the LazyIterator if it's a lazy one. So no need here.
        service.dataSource().releaseReadLock();
    }
    idIterator = idIterator == null ? new ConstantScoreIterator<Long>(ids, 0) : idIterator;
    return new IdToEntityIterator<T>(idIterator) {

        @Override
        protected T underlyingObjectToObject(Long id) {
            return getById(id);
        }

        protected void itemDodged(Long item) {
            abandonedIds.add(item);
        }
    };
}
Also used : MultiSearcher(org.apache.lucene.search.MultiSearcher) Searcher(org.apache.lucene.search.Searcher) ArrayList(java.util.ArrayList) IndexHits(org.neo4j.graphdb.index.IndexHits) Collection(java.util.Collection)

Example 4 with IndexHits

use of org.neo4j.graphdb.index.IndexHits in project graphdb by neo4j-attic.

the class LuceneBatchInserterIndex method query.

private IndexHits<Long> query(Query query, final String key, final Object value) {
    try {
        Hits hits = new Hits(searcher(), query, null);
        HitsIterator result = new HitsIterator(hits);
        if (key == null || this.cache == null || !this.cache.containsKey(key)) {
            return new DocToIdIterator(result, null, null);
        } else {
            return new DocToIdIterator(result, null, null) {

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

                @Override
                protected Long fetchNextOrNull() {
                    Long result = super.fetchNextOrNull();
                    if (result != null) {
                        ids.add(result);
                    }
                    return result;
                }

                @Override
                protected void endReached() {
                    super.endReached();
                    addToCache(ids, key, value);
                }
            };
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : IndexHits(org.neo4j.graphdb.index.IndexHits) Collection(java.util.Collection) IOException(java.io.IOException)

Example 5 with IndexHits

use of org.neo4j.graphdb.index.IndexHits in project graphdb by neo4j-attic.

the class LuceneIndex method search.

private IndexHits<Document> search(IndexSearcherRef searcherRef, Query query, QueryContext additionalParametersOrNull, Searcher additionsSearcher, Collection<Long> removed) {
    try {
        if (additionsSearcher != null && !removed.isEmpty()) {
            letThroughAdditions(additionsSearcher, query, removed);
        }
        Searcher searcher = additionsSearcher == null ? searcherRef.getSearcher() : new MultiSearcher(searcherRef.getSearcher(), additionsSearcher);
        IndexHits<Document> result = null;
        if (additionalParametersOrNull != null && additionalParametersOrNull.getTop() > 0) {
            result = new TopDocsIterator(query, additionalParametersOrNull, searcher);
        } else {
            Sort sorting = additionalParametersOrNull != null ? additionalParametersOrNull.getSorting() : null;
            boolean forceScore = additionalParametersOrNull == null || !additionalParametersOrNull.getTradeCorrectnessForSpeed();
            Hits hits = new Hits(searcher, query, null, sorting, forceScore);
            result = new HitsIterator(hits);
        }
        return result;
    } catch (IOException e) {
        throw new RuntimeException("Unable to query " + this + " with " + query, e);
    }
}
Also used : MultiSearcher(org.apache.lucene.search.MultiSearcher) IndexHits(org.neo4j.graphdb.index.IndexHits) MultiSearcher(org.apache.lucene.search.MultiSearcher) Searcher(org.apache.lucene.search.Searcher) Sort(org.apache.lucene.search.Sort) IOException(java.io.IOException) Document(org.apache.lucene.document.Document)

Aggregations

IndexHits (org.neo4j.graphdb.index.IndexHits)8 IOException (java.io.IOException)4 Collection (java.util.Collection)4 ArrayList (java.util.ArrayList)2 Document (org.apache.lucene.document.Document)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 MultiSearcher (org.apache.lucene.search.MultiSearcher)2 Searcher (org.apache.lucene.search.Searcher)2 Sort (org.apache.lucene.search.Sort)2 Iterator (java.util.Iterator)1 NoSuchElementException (java.util.NoSuchElementException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 MultiReader (org.apache.lucene.index.MultiReader)1 Test (org.junit.Test)1 Node (org.neo4j.graphdb.Node)1 Transaction (org.neo4j.graphdb.Transaction)1