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);
}
}
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);
}
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);
}
};
}
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);
}
}
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);
}
}
Aggregations