Search in sources :

Example 1 with CoveringQuery

use of org.apache.lucene.sandbox.search.CoveringQuery in project OpenSearch by opensearch-project.

the class TermsSetQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) {
    if (values.isEmpty()) {
        return Queries.newMatchNoDocsQuery("No terms supplied for \"" + getName() + "\" query.");
    }
    // Fail before we attempt to create the term queries:
    if (values.size() > BooleanQuery.getMaxClauseCount()) {
        throw new BooleanQuery.TooManyClauses();
    }
    List<Query> queries = createTermQueries(context);
    LongValuesSource longValuesSource = createValuesSource(context);
    return new CoveringQuery(queries, longValuesSource);
}
Also used : Query(org.apache.lucene.search.Query) CoveringQuery(org.apache.lucene.sandbox.search.CoveringQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) CoveringQuery(org.apache.lucene.sandbox.search.CoveringQuery) LongValuesSource(org.apache.lucene.search.LongValuesSource)

Example 2 with CoveringQuery

use of org.apache.lucene.sandbox.search.CoveringQuery in project OpenSearch by opensearch-project.

the class CandidateQueryTests method testPercolateSmallAndLargeDocument.

public void testPercolateSmallAndLargeDocument() throws Exception {
    List<ParseContext.Document> docs = new ArrayList<>();
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(new TermQuery(new Term("field", "value1")), Occur.MUST);
    builder.add(new TermQuery(new Term("field", "value2")), Occur.MUST);
    addQuery(builder.build(), docs);
    builder = new BooleanQuery.Builder();
    builder.add(new TermQuery(new Term("field", "value2")), Occur.MUST);
    builder.add(new TermQuery(new Term("field", "value3")), Occur.MUST);
    addQuery(builder.build(), docs);
    builder = new BooleanQuery.Builder();
    builder.add(new TermQuery(new Term("field", "value3")), Occur.MUST);
    builder.add(new TermQuery(new Term("field", "value4")), Occur.MUST);
    addQuery(builder.build(), docs);
    indexWriter.addDocuments(docs);
    indexWriter.close();
    directoryReader = DirectoryReader.open(directory);
    IndexSearcher shardSearcher = newSearcher(directoryReader);
    shardSearcher.setQueryCache(null);
    Version v = Version.CURRENT;
    try (Directory directory = new ByteBuffersDirectory()) {
        try (IndexWriter iw = new IndexWriter(directory, newIndexWriterConfig())) {
            List<Document> documents = new ArrayList<>();
            Document document = new Document();
            document.add(new StringField("field", "value1", Field.Store.NO));
            document.add(new StringField("field", "value2", Field.Store.NO));
            documents.add(document);
            document = new Document();
            document.add(new StringField("field", "value5", Field.Store.NO));
            document.add(new StringField("field", "value6", Field.Store.NO));
            documents.add(document);
            document = new Document();
            document.add(new StringField("field", "value3", Field.Store.NO));
            document.add(new StringField("field", "value4", Field.Store.NO));
            documents.add(document);
            // IW#addDocuments(...) ensures we end up with a single segment
            iw.addDocuments(documents);
        }
        try (IndexReader ir = DirectoryReader.open(directory)) {
            IndexSearcher percolateSearcher = new IndexSearcher(ir);
            PercolateQuery query = (PercolateQuery) fieldType.percolateQuery("_name", queryStore, Collections.singletonList(new BytesArray("{}")), percolateSearcher, false, v);
            BooleanQuery candidateQuery = (BooleanQuery) query.getCandidateMatchesQuery();
            assertThat(candidateQuery.clauses().get(0).getQuery(), instanceOf(CoveringQuery.class));
            TopDocs topDocs = shardSearcher.search(query, 10);
            assertEquals(2L, topDocs.totalHits.value);
            assertEquals(2, topDocs.scoreDocs.length);
            assertEquals(0, topDocs.scoreDocs[0].doc);
            assertEquals(2, topDocs.scoreDocs[1].doc);
            topDocs = shardSearcher.search(new ConstantScoreQuery(query), 10);
            assertEquals(2L, topDocs.totalHits.value);
            assertEquals(2, topDocs.scoreDocs.length);
            assertEquals(0, topDocs.scoreDocs[0].doc);
            assertEquals(2, topDocs.scoreDocs[1].doc);
        }
    }
    // This will trigger using the TermsQuery instead of individual term query clauses in the CoveringQuery:
    try (Directory directory = new ByteBuffersDirectory()) {
        try (IndexWriter iw = new IndexWriter(directory, newIndexWriterConfig())) {
            Document document = new Document();
            for (int i = 0; i < 1024; i++) {
                int fieldNumber = 2 + i;
                document.add(new StringField("field", "value" + fieldNumber, Field.Store.NO));
            }
            iw.addDocument(document);
        }
        try (IndexReader ir = DirectoryReader.open(directory)) {
            IndexSearcher percolateSearcher = new IndexSearcher(ir);
            PercolateQuery query = (PercolateQuery) fieldType.percolateQuery("_name", queryStore, Collections.singletonList(new BytesArray("{}")), percolateSearcher, false, v);
            BooleanQuery candidateQuery = (BooleanQuery) query.getCandidateMatchesQuery();
            assertThat(candidateQuery.clauses().get(0).getQuery(), instanceOf(TermInSetQuery.class));
            TopDocs topDocs = shardSearcher.search(query, 10);
            assertEquals(2L, topDocs.totalHits.value);
            assertEquals(2, topDocs.scoreDocs.length);
            assertEquals(1, topDocs.scoreDocs[0].doc);
            assertEquals(2, topDocs.scoreDocs[1].doc);
            topDocs = shardSearcher.search(new ConstantScoreQuery(query), 10);
            assertEquals(2L, topDocs.totalHits.value);
            assertEquals(2, topDocs.scoreDocs.length);
            assertEquals(1, topDocs.scoreDocs[0].doc);
            assertEquals(2, topDocs.scoreDocs[1].doc);
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) BytesArray(org.opensearch.common.bytes.BytesArray) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) FloatPoint(org.apache.lucene.document.FloatPoint) DoublePoint(org.apache.lucene.document.DoublePoint) HalfFloatPoint(org.apache.lucene.sandbox.document.HalfFloatPoint) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) TopDocs(org.apache.lucene.search.TopDocs) Version(org.opensearch.Version) IndexWriter(org.apache.lucene.index.IndexWriter) ByteBuffersDirectory(org.apache.lucene.store.ByteBuffersDirectory) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) CoveringQuery(org.apache.lucene.sandbox.search.CoveringQuery) ByteBuffersDirectory(org.apache.lucene.store.ByteBuffersDirectory) Directory(org.apache.lucene.store.Directory)

Aggregations

CoveringQuery (org.apache.lucene.sandbox.search.CoveringQuery)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 TermQuery (org.apache.lucene.search.TermQuery)2 ArrayList (java.util.ArrayList)1 Document (org.apache.lucene.document.Document)1 DoublePoint (org.apache.lucene.document.DoublePoint)1 FloatPoint (org.apache.lucene.document.FloatPoint)1 InetAddressPoint (org.apache.lucene.document.InetAddressPoint)1 IntPoint (org.apache.lucene.document.IntPoint)1 LongPoint (org.apache.lucene.document.LongPoint)1 StringField (org.apache.lucene.document.StringField)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 Term (org.apache.lucene.index.Term)1 BlendedTermQuery (org.apache.lucene.queries.BlendedTermQuery)1 SpanTermQuery (org.apache.lucene.queries.spans.SpanTermQuery)1 HalfFloatPoint (org.apache.lucene.sandbox.document.HalfFloatPoint)1 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 LongValuesSource (org.apache.lucene.search.LongValuesSource)1