Search in sources :

Example 1 with SpanMatchNoDocsQuery

use of org.apache.lucene.queries.SpanMatchNoDocsQuery in project OpenSearch by opensearch-project.

the class SpanMultiTermQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    // We do the rewrite in toQuery to not have to deal with the case when a multi-term builder rewrites to a non-multi-term
    // builder.
    QueryBuilder multiTermQueryBuilder = Rewriteable.rewrite(this.multiTermQueryBuilder, context);
    if (multiTermQueryBuilder instanceof MatchNoneQueryBuilder) {
        return new SpanMatchNoDocsQuery(this.multiTermQueryBuilder.fieldName(), "Inner query rewrote to match_none");
    } else if (multiTermQueryBuilder instanceof PrefixQueryBuilder) {
        PrefixQueryBuilder prefixBuilder = (PrefixQueryBuilder) multiTermQueryBuilder;
        MappedFieldType fieldType = context.fieldMapper(prefixBuilder.fieldName());
        if (fieldType == null) {
            throw new IllegalStateException("Rewrite first");
        }
        final SpanMultiTermQueryWrapper.SpanRewriteMethod spanRewriteMethod;
        if (prefixBuilder.rewrite() != null) {
            MultiTermQuery.RewriteMethod rewriteMethod = QueryParsers.parseRewriteMethod(prefixBuilder.rewrite(), null, LoggingDeprecationHandler.INSTANCE);
            if (rewriteMethod instanceof TopTermsRewrite) {
                TopTermsRewrite<?> innerRewrite = (TopTermsRewrite<?>) rewriteMethod;
                spanRewriteMethod = new SpanMultiTermQueryWrapper.TopTermsSpanBooleanQueryRewrite(innerRewrite.getSize());
            } else {
                spanRewriteMethod = new SpanBooleanQueryRewriteWithMaxClause();
            }
        } else {
            spanRewriteMethod = new SpanBooleanQueryRewriteWithMaxClause();
        }
        return fieldType.spanPrefixQuery(prefixBuilder.value(), spanRewriteMethod, context);
    } else {
        Query subQuery = multiTermQueryBuilder.toQuery(context);
        while (true) {
            if (subQuery instanceof ConstantScoreQuery) {
                subQuery = ((ConstantScoreQuery) subQuery).getQuery();
            } else if (subQuery instanceof BoostQuery) {
                BoostQuery boostQuery = (BoostQuery) subQuery;
                subQuery = boostQuery.getQuery();
            } else {
                break;
            }
        }
        if (subQuery instanceof MatchNoDocsQuery) {
            return new SpanMatchNoDocsQuery(this.multiTermQueryBuilder.fieldName(), subQuery.toString());
        } else if (subQuery instanceof MultiTermQuery == false) {
            throw new UnsupportedOperationException("unsupported inner query, should be " + MultiTermQuery.class.getName() + " but was " + subQuery.getClass().getName());
        }
        MultiTermQuery multiTermQuery = (MultiTermQuery) subQuery;
        SpanMultiTermQueryWrapper<?> wrapper = new SpanMultiTermQueryWrapper<>(multiTermQuery);
        if (multiTermQuery.getRewriteMethod() instanceof TopTermsRewrite == false) {
            wrapper.setRewriteMethod(new SpanBooleanQueryRewriteWithMaxClause());
        }
        return wrapper;
    }
}
Also used : TopTermsRewrite(org.apache.lucene.search.TopTermsRewrite) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) SpanMatchNoDocsQuery(org.apache.lucene.queries.SpanMatchNoDocsQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanMultiTermQueryWrapper(org.apache.lucene.queries.spans.SpanMultiTermQueryWrapper) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) SpanMatchNoDocsQuery(org.apache.lucene.queries.SpanMatchNoDocsQuery) SpanBooleanQueryRewriteWithMaxClause(org.opensearch.common.lucene.search.SpanBooleanQueryRewriteWithMaxClause) BoostQuery(org.apache.lucene.search.BoostQuery) SpanMatchNoDocsQuery(org.apache.lucene.queries.SpanMatchNoDocsQuery) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery)

Example 2 with SpanMatchNoDocsQuery

use of org.apache.lucene.queries.SpanMatchNoDocsQuery in project OpenSearch by opensearch-project.

the class SpanMatchNoDocsQueryTests method testSimple.

public void testSimple() throws Exception {
    SpanMatchNoDocsQuery query = new SpanMatchNoDocsQuery("field", "a good reason");
    assertEquals(query.toString(), "SpanMatchNoDocsQuery(\"a good reason\")");
    Query rewrite = query.rewrite(null);
    assertTrue(rewrite instanceof SpanMatchNoDocsQuery);
    assertEquals(rewrite.toString(), "SpanMatchNoDocsQuery(\"a good reason\")");
}
Also used : Query(org.apache.lucene.search.Query) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) SpanMatchNoDocsQuery(org.apache.lucene.queries.SpanMatchNoDocsQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) SpanMatchNoDocsQuery(org.apache.lucene.queries.SpanMatchNoDocsQuery)

Example 3 with SpanMatchNoDocsQuery

use of org.apache.lucene.queries.SpanMatchNoDocsQuery in project OpenSearch by opensearch-project.

the class SpanMatchNoDocsQueryTests method testQuery.

public void testQuery() throws Exception {
    Directory dir = newDirectory();
    Analyzer analyzer = new MockAnalyzer(random());
    IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(analyzer).setMaxBufferedDocs(2).setMergePolicy(newLogMergePolicy()));
    addDoc("one", iw);
    addDoc("two", iw);
    addDoc("three", iw);
    IndexReader ir = DirectoryReader.open(iw);
    IndexSearcher searcher = new IndexSearcher(ir);
    Query query = new SpanMatchNoDocsQuery("unknown", "field not found");
    assertEquals(searcher.count(query), 0);
    ScoreDoc[] hits;
    hits = searcher.search(query, 1000).scoreDocs;
    assertEquals(0, hits.length);
    assertEquals(query.toString(), "SpanMatchNoDocsQuery(\"field not found\")");
    SpanOrQuery orQuery = new SpanOrQuery(new SpanMatchNoDocsQuery("unknown", "field not found"), new SpanTermQuery(new Term("unknown", "one")));
    assertEquals(searcher.count(orQuery), 0);
    hits = searcher.search(orQuery, 1000).scoreDocs;
    assertEquals(0, hits.length);
    orQuery = new SpanOrQuery(new SpanMatchNoDocsQuery("key", "a good reason"), new SpanTermQuery(new Term("key", "one")));
    assertEquals(searcher.count(orQuery), 1);
    hits = searcher.search(orQuery, 1000).scoreDocs;
    assertEquals(1, hits.length);
    Query rewrite = orQuery.rewrite(ir);
    assertEquals(rewrite, orQuery);
    SpanNearQuery nearQuery = new SpanNearQuery(new SpanQuery[] { new SpanMatchNoDocsQuery("same", ""), new SpanMatchNoDocsQuery("same", "") }, 0, true);
    assertEquals(searcher.count(nearQuery), 0);
    hits = searcher.search(nearQuery, 1000).scoreDocs;
    assertEquals(0, hits.length);
    rewrite = nearQuery.rewrite(ir);
    assertEquals(rewrite, nearQuery);
    iw.close();
    ir.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) SpanMatchNoDocsQuery(org.apache.lucene.queries.SpanMatchNoDocsQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) Term(org.apache.lucene.index.Term) MockAnalyzer(org.apache.lucene.tests.analysis.MockAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) ScoreDoc(org.apache.lucene.search.ScoreDoc) MockAnalyzer(org.apache.lucene.tests.analysis.MockAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) IndexReader(org.apache.lucene.index.IndexReader) SpanMatchNoDocsQuery(org.apache.lucene.queries.SpanMatchNoDocsQuery) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) Directory(org.apache.lucene.store.Directory)

Example 4 with SpanMatchNoDocsQuery

use of org.apache.lucene.queries.SpanMatchNoDocsQuery in project OpenSearch by opensearch-project.

the class SpanMatchNoDocsQueryTests method testEquals.

public void testEquals() {
    Query q1 = new SpanMatchNoDocsQuery("key1", "one");
    Query q2 = new SpanMatchNoDocsQuery("key2", "two");
    assertTrue(q1.equals(q2));
    QueryUtils.check(q1);
}
Also used : Query(org.apache.lucene.search.Query) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) SpanMatchNoDocsQuery(org.apache.lucene.queries.SpanMatchNoDocsQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) SpanMatchNoDocsQuery(org.apache.lucene.queries.SpanMatchNoDocsQuery)

Example 5 with SpanMatchNoDocsQuery

use of org.apache.lucene.queries.SpanMatchNoDocsQuery in project OpenSearch by opensearch-project.

the class SpanMultiTermQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(SpanMultiTermQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
    if (query instanceof SpanMatchNoDocsQuery) {
        return;
    }
    assertThat(query, either(instanceOf(SpanMultiTermQueryWrapper.class)).or(instanceOf(FieldMaskingSpanQuery.class)));
    if (query instanceof SpanMultiTermQueryWrapper) {
        SpanMultiTermQueryWrapper wrapper = (SpanMultiTermQueryWrapper) query;
        Query innerQuery = queryBuilder.innerQuery().toQuery(context);
        if (queryBuilder.innerQuery().boost() != AbstractQueryBuilder.DEFAULT_BOOST) {
            assertThat(innerQuery, instanceOf(BoostQuery.class));
            BoostQuery boostQuery = (BoostQuery) innerQuery;
            innerQuery = boostQuery.getQuery();
        }
        assertThat(innerQuery, instanceOf(MultiTermQuery.class));
        MultiTermQuery multiQuery = (MultiTermQuery) innerQuery;
        if (multiQuery.getRewriteMethod() instanceof TopTermsRewrite) {
            assertThat(wrapper.getRewriteMethod(), instanceOf(SpanMultiTermQueryWrapper.TopTermsSpanBooleanQueryRewrite.class));
        } else {
            assertThat(wrapper.getRewriteMethod(), instanceOf(SpanBooleanQueryRewriteWithMaxClause.class));
        }
    } else if (query instanceof FieldMaskingSpanQuery) {
        FieldMaskingSpanQuery mask = (FieldMaskingSpanQuery) query;
        assertThat(mask.getMaskedQuery(), instanceOf(TermQuery.class));
    }
}
Also used : TopTermsRewrite(org.apache.lucene.search.TopTermsRewrite) SpanMultiTermQueryWrapper(org.apache.lucene.queries.spans.SpanMultiTermQueryWrapper) Query(org.apache.lucene.search.Query) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) SpanMatchNoDocsQuery(org.apache.lucene.queries.SpanMatchNoDocsQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) FieldMaskingSpanQuery(org.apache.lucene.queries.spans.FieldMaskingSpanQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanMatchNoDocsQuery(org.apache.lucene.queries.SpanMatchNoDocsQuery) SpanBooleanQueryRewriteWithMaxClause(org.opensearch.common.lucene.search.SpanBooleanQueryRewriteWithMaxClause) FieldMaskingSpanQuery(org.apache.lucene.queries.spans.FieldMaskingSpanQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Aggregations

SpanMatchNoDocsQuery (org.apache.lucene.queries.SpanMatchNoDocsQuery)6 SpanQuery (org.apache.lucene.queries.spans.SpanQuery)5 SpanTermQuery (org.apache.lucene.queries.spans.SpanTermQuery)5 Query (org.apache.lucene.search.Query)5 SpanOrQuery (org.apache.lucene.queries.spans.SpanOrQuery)4 SpanNearQuery (org.apache.lucene.queries.spans.SpanNearQuery)3 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)3 IndexReader (org.apache.lucene.index.IndexReader)2 Term (org.apache.lucene.index.Term)2 SpanMultiTermQueryWrapper (org.apache.lucene.queries.spans.SpanMultiTermQueryWrapper)2 BoostQuery (org.apache.lucene.search.BoostQuery)2 TopTermsRewrite (org.apache.lucene.search.TopTermsRewrite)2 SpanBooleanQueryRewriteWithMaxClause (org.opensearch.common.lucene.search.SpanBooleanQueryRewriteWithMaxClause)2 HashSet (java.util.HashSet)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 IndexReaderContext (org.apache.lucene.index.IndexReaderContext)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 Terms (org.apache.lucene.index.Terms)1 TermsEnum (org.apache.lucene.index.TermsEnum)1