use of org.apache.lucene.search.IndexSearcher in project Openfire by igniterealtime.
the class ChatSearchManager method getSearcher.
/**
* Returns a Lucene Searcher that can be used to execute queries. Lucene
* can handle index reading even while updates occur. However, in order
* for index changes to be reflected in search results, the reader must
* be re-opened whenever the modificationDate changes.<p>
* <p/>
* The location of the index is the "index" subdirectory in [jiveHome].
*
* @return a Searcher that can be used to execute queries.
*/
public Searcher getSearcher() throws IOException {
synchronized (indexerAnalyzer) {
if (searcherReader == null) {
if (searchDirectory != null && IndexReader.indexExists(searchDirectory)) {
searcherReader = IndexReader.open(searchDirectory);
searcher = new IndexSearcher(searcherReader);
} else {
// Log warnings.
if (searchDirectory == null) {
Log.warn("Search " + "directory not set, you must rebuild the index.");
} else if (!IndexReader.indexExists(searchDirectory)) {
Log.warn("Search " + "directory " + searchDirectory + " does not appear to " + "be a valid search index. You must rebuild the index.");
}
return null;
}
}
}
return searcher;
}
use of org.apache.lucene.search.IndexSearcher in project neo4j by neo4j.
the class LuceneBatchInserterIndex method removeFromCache.
private void removeFromCache(long entityId) throws IOException, CorruptIndexException {
IndexSearcher searcher = searcherManager.acquire();
try {
Query query = type.idTermQuery(entityId);
TopDocs docs = searcher.search(query, 1);
if (docs.totalHits > 0) {
Document document = searcher.doc(docs.scoreDocs[0].doc);
for (IndexableField field : document.getFields()) {
String key = field.name();
Object value = field.stringValue();
removeFromCache(entityId, key, value);
}
}
} finally {
searcherManager.release(searcher);
}
}
use of org.apache.lucene.search.IndexSearcher 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;
}
use of org.apache.lucene.search.IndexSearcher in project neo4j by neo4j.
the class NodeRangeDocumentLabelScanStorageStrategyTest method shouldCreateNewDocumentsForNewlyLabeledNodes.
@Test
public void shouldCreateNewDocumentsForNewlyLabeledNodes() throws Exception {
// given
WritableIndexPartition partition = mock(WritableIndexPartition.class);
WritableDatabaseLabelScanIndex index = buildLuceneIndex(partition);
PartitionSearcher partitionSearcher = mock(PartitionSearcher.class);
when(partition.acquireSearcher()).thenReturn(partitionSearcher);
IndexWriter indexWriter = mock(IndexWriter.class);
when(partition.getIndexWriter()).thenReturn(indexWriter);
IndexSearcher searcher = mock(IndexSearcher.class);
when(partitionSearcher.getIndexSearcher()).thenReturn(searcher);
when(searcher.search(new TermQuery(format.rangeTerm(0)), 1)).thenReturn(emptyTopDocs());
when(searcher.search(new TermQuery(format.rangeTerm(1)), 1)).thenReturn(null);
LabelScanWriter writer = new PartitionedLuceneLabelScanWriter(index, format);
// when
writer.write(labelChanges(0, labels(), labels(6, 7)));
writer.write(labelChanges(1, labels(), labels(6, 8)));
writer.write(labelChanges(1 << format.bitmapFormat().shift, labels(), labels(7)));
writer.close();
// then
verify(partition, times(2)).acquireSearcher();
verify(partitionSearcher, times(2)).getIndexSearcher();
verify(partition, times(2)).getIndexWriter();
verify(partitionSearcher, times(2)).close();
verify(indexWriter).updateDocument(eq(format.rangeTerm(0)), match(document(format.rangeField(0), format.labelField(6, 0x3), format.labelField(7, 0x1), format.labelField(8, 0x2), format.labelSearchField(8))));
verify(indexWriter).updateDocument(eq(format.rangeTerm(1)), match(document(format.rangeField(1), format.labelField(7, 0x1), format.labelSearchField(7))));
verify(index).maybeRefreshBlocking();
verifyNoMoreInteractions(partition);
}
use of org.apache.lucene.search.IndexSearcher in project neo4j by neo4j.
the class PageOfRangesIteratorTest method shouldReadPagesOfDocumentsFromSearcher.
@Test
public void shouldReadPagesOfDocumentsFromSearcher() throws Exception {
final int labelId = 7;
final int pageSize = 2;
// given
Query query = mock(Query.class);
IndexSearcher searcher = mock(IndexSearcher.class);
NumericDocValues rangeNDV = mock(NumericDocValues.class);
when(rangeNDV.get(11)).thenReturn(0x1L);
when(rangeNDV.get(16)).thenReturn(0x2L);
when(rangeNDV.get(37)).thenReturn(0x3L);
NumericDocValues labelNDV = mock(NumericDocValues.class);
when(labelNDV.get(11)).thenReturn(0x01L);
when(labelNDV.get(16)).thenReturn(0x03L);
when(labelNDV.get(37)).thenReturn(0x30L);
Map<String, NumericDocValues> docValues = MapUtil.genericMap("range", rangeNDV, "7", labelNDV);
IndexReaderStub reader = new IndexReaderStub(docValues);
reader.setElements(new String[] { "11", "16", "37" });
final LeafReaderContext context = reader.getContext();
doAnswer(invocation -> {
DocValuesCollector collector = (DocValuesCollector) invocation.getArguments()[1];
collector.doSetNextReader(context);
collector.collect(11);
collector.collect(16);
collector.collect(37);
return null;
}).when(searcher).search(same(query), any(DocValuesCollector.class));
PrimitiveLongIterator iterator = concat(new PageOfRangesIterator(format, searcher, pageSize, query, Occur.MUST, labelId));
// when
List<Long> longs = PrimitiveLongCollections.asList(iterator);
// then
assertEquals(asList(/*doc1:*/
1L << format.bitmapFormat().shift, /*doc2:*/
2L << format.bitmapFormat().shift, (2L << format.bitmapFormat().shift) + 1, /*doc3:*/
(3L << format.bitmapFormat().shift) + 4, (3L << format.bitmapFormat().shift) + 5), longs);
verify(searcher, times(1)).search(same(query), any(DocValuesCollector.class));
verify(rangeNDV, times(6)).get(anyInt());
verify(labelNDV, times(3)).get(anyInt());
verifyNoMoreInteractions(searcher);
verifyNoMoreInteractions(labelNDV);
verifyNoMoreInteractions(rangeNDV);
}
Aggregations