Search in sources :

Example 71 with TermQuery

use of org.apache.lucene.search.TermQuery in project neo4j-mobile-android by neo4j-contrib.

the class FullTxData method injectOrphans.

private Query injectOrphans(Query query) {
    if (query instanceof BooleanQuery) {
        BooleanQuery source = (BooleanQuery) query;
        BooleanQuery result = new BooleanQuery();
        for (BooleanClause clause : source.clauses()) {
            result.add(injectOrphans(clause.getQuery()), clause.getOccur());
        }
        return result;
    } else {
        BooleanQuery result = new BooleanQuery();
        result.add(query, Occur.SHOULD);
        result.add(new TermQuery(new Term(ORPHANS_KEY, extractTermField(query))), Occur.SHOULD);
        return result;
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term)

Example 72 with TermQuery

use of org.apache.lucene.search.TermQuery 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 73 with TermQuery

use of org.apache.lucene.search.TermQuery in project neo4j by neo4j.

the class PartitionedLuceneLabelScanWriter method readLabelBitMapsInRange.

private Map<Long, /*range*/
Bitmap> readLabelBitMapsInRange(IndexSearcher searcher, long range) throws IOException {
    Map<Long, Bitmap> /*label*/
    fields = new HashMap<>();
    Term documentTerm = format.rangeTerm(range);
    TermQuery query = new TermQuery(documentTerm);
    FirstHitCollector hitCollector = new FirstHitCollector();
    searcher.search(query, hitCollector);
    if (hitCollector.hasMatched()) {
        Document document = searcher.doc(hitCollector.getMatchedDoc());
        for (IndexableField field : document.getFields()) {
            if (!format.isRangeOrLabelField(field)) {
                Long label = Long.valueOf(field.name());
                fields.put(label, format.readBitmap(field));
            }
        }
    }
    return fields;
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) TermQuery(org.apache.lucene.search.TermQuery) Bitmap(org.neo4j.kernel.api.impl.labelscan.bitmaps.Bitmap) HashMap(java.util.HashMap) FirstHitCollector(org.neo4j.kernel.api.impl.index.collector.FirstHitCollector) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document)

Example 74 with TermQuery

use of org.apache.lucene.search.TermQuery in project neo4j by neo4j.

the class LuceneDocumentStructure method newRangeSeekByStringQuery.

public static Query newRangeSeekByStringQuery(String lower, boolean includeLower, String upper, boolean includeUpper) {
    boolean includeLowerBoundary = StringUtils.EMPTY.equals(lower) || includeLower;
    boolean includeUpperBoundary = StringUtils.EMPTY.equals(upper) || includeUpper;
    TermRangeQuery termRangeQuery = TermRangeQuery.newStringRange(ValueEncoding.String.key(0), lower, upper, includeLowerBoundary, includeUpperBoundary);
    if ((includeLowerBoundary != includeLower) || (includeUpperBoundary != includeUpper)) {
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        builder.setDisableCoord(true);
        if (includeLowerBoundary != includeLower) {
            builder.add(new TermQuery(new Term(ValueEncoding.String.key(0), lower)), BooleanClause.Occur.MUST_NOT);
        }
        if (includeUpperBoundary != includeUpper) {
            builder.add(new TermQuery(new Term(ValueEncoding.String.key(0), upper)), BooleanClause.Occur.MUST_NOT);
        }
        builder.add(termRangeQuery, BooleanClause.Occur.FILTER);
        return new ConstantScoreQuery(builder.build());
    }
    return termRangeQuery;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) TermQuery(org.apache.lucene.search.TermQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Term(org.apache.lucene.index.Term)

Example 75 with TermQuery

use of org.apache.lucene.search.TermQuery in project neo4j by neo4j.

the class SimpleIndexReader method countIndexedNodes.

@Override
public long countIndexedNodes(long nodeId, Object... propertyValues) {
    Query nodeIdQuery = new TermQuery(LuceneDocumentStructure.newTermForChangeOrRemove(nodeId));
    Query valueQuery = LuceneDocumentStructure.newSeekQuery(propertyValues);
    BooleanQuery.Builder nodeIdAndValueQuery = new BooleanQuery.Builder().setDisableCoord(true);
    nodeIdAndValueQuery.add(nodeIdQuery, BooleanClause.Occur.MUST);
    nodeIdAndValueQuery.add(valueQuery, BooleanClause.Occur.MUST);
    try {
        TotalHitCountCollector collector = new TotalHitCountCollector();
        getIndexSearcher().search(nodeIdAndValueQuery.build(), collector);
        // A <label,propertyKeyId,nodeId> tuple should only match at most a single propertyValue
        return collector.getTotalHits();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) IndexQuery(org.neo4j.kernel.api.schema_new.IndexQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) IOException(java.io.IOException)

Aggregations

TermQuery (org.apache.lucene.search.TermQuery)673 Term (org.apache.lucene.index.Term)560 BooleanQuery (org.apache.lucene.search.BooleanQuery)343 Query (org.apache.lucene.search.Query)275 IndexSearcher (org.apache.lucene.search.IndexSearcher)252 Document (org.apache.lucene.document.Document)210 TopDocs (org.apache.lucene.search.TopDocs)164 Directory (org.apache.lucene.store.Directory)164 IndexReader (org.apache.lucene.index.IndexReader)125 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)125 PhraseQuery (org.apache.lucene.search.PhraseQuery)122 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)116 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)114 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)97 BoostQuery (org.apache.lucene.search.BoostQuery)85 Field (org.apache.lucene.document.Field)81 Test (org.junit.Test)75 PrefixQuery (org.apache.lucene.search.PrefixQuery)74 ArrayList (java.util.ArrayList)72 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)62