Search in sources :

Example 46 with IndexSearcher

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;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher)

Example 47 with IndexSearcher

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);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) IndexableField(org.apache.lucene.index.IndexableField) Query(org.apache.lucene.search.Query) Document(org.apache.lucene.document.Document)

Example 48 with IndexSearcher

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;
}
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 49 with IndexSearcher

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);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) WritableDatabaseLabelScanIndex(org.neo4j.kernel.api.impl.labelscan.WritableDatabaseLabelScanIndex) PartitionSearcher(org.neo4j.kernel.api.impl.index.partition.PartitionSearcher) IndexWriter(org.apache.lucene.index.IndexWriter) PartitionedLuceneLabelScanWriter(org.neo4j.kernel.api.impl.labelscan.writer.PartitionedLuceneLabelScanWriter) WritableIndexPartition(org.neo4j.kernel.api.impl.index.partition.WritableIndexPartition) PartitionedLuceneLabelScanWriter(org.neo4j.kernel.api.impl.labelscan.writer.PartitionedLuceneLabelScanWriter) LabelScanWriter(org.neo4j.kernel.api.labelscan.LabelScanWriter) Test(org.junit.Test)

Example 50 with IndexSearcher

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);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) NumericDocValues(org.apache.lucene.index.NumericDocValues) Query(org.apache.lucene.search.Query) IndexReaderStub(org.neo4j.kernel.api.impl.index.IndexReaderStub) DocValuesCollector(org.neo4j.kernel.api.impl.index.collector.DocValuesCollector) PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Test(org.junit.Test)

Aggregations

IndexSearcher (org.apache.lucene.search.IndexSearcher)927 Document (org.apache.lucene.document.Document)528 IndexReader (org.apache.lucene.index.IndexReader)430 Directory (org.apache.lucene.store.Directory)407 TopDocs (org.apache.lucene.search.TopDocs)382 TermQuery (org.apache.lucene.search.TermQuery)332 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)311 Query (org.apache.lucene.search.Query)299 Term (org.apache.lucene.index.Term)287 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)229 BooleanQuery (org.apache.lucene.search.BooleanQuery)176 IOException (java.io.IOException)155 Field (org.apache.lucene.document.Field)140 IndexWriter (org.apache.lucene.index.IndexWriter)139 ScoreDoc (org.apache.lucene.search.ScoreDoc)135 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)122 Sort (org.apache.lucene.search.Sort)114 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)111 DirectoryReader (org.apache.lucene.index.DirectoryReader)110 ArrayList (java.util.ArrayList)109