Search in sources :

Example 1 with SpanBooleanQueryRewriteWithMaxClause

use of org.opensearch.common.lucene.search.SpanBooleanQueryRewriteWithMaxClause 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 SpanBooleanQueryRewriteWithMaxClause

use of org.opensearch.common.lucene.search.SpanBooleanQueryRewriteWithMaxClause in project OpenSearch by opensearch-project.

the class MatchQuery method setMaxExpansions.

public void setMaxExpansions(int maxExpansions) {
    this.maxExpansions = maxExpansions;
    this.spanRewriteMethod = new SpanBooleanQueryRewriteWithMaxClause(maxExpansions, false);
}
Also used : SpanBooleanQueryRewriteWithMaxClause(org.opensearch.common.lucene.search.SpanBooleanQueryRewriteWithMaxClause)

Example 3 with SpanBooleanQueryRewriteWithMaxClause

use of org.opensearch.common.lucene.search.SpanBooleanQueryRewriteWithMaxClause in project OpenSearch by opensearch-project.

the class SpanMultiTermQueryBuilderTests method testToQueryInnerTermQuery.

public void testToQueryInnerTermQuery() throws IOException {
    String fieldName = randomFrom("prefix_field", "prefix_field_alias");
    final QueryShardContext context = createShardContext();
    {
        Query query = new SpanMultiTermQueryBuilder(new PrefixQueryBuilder(fieldName, "foo")).toQuery(context);
        assertThat(query, instanceOf(FieldMaskingSpanQuery.class));
        FieldMaskingSpanQuery fieldQuery = (FieldMaskingSpanQuery) query;
        assertThat(fieldQuery.getMaskedQuery(), instanceOf(SpanTermQuery.class));
        assertThat(fieldQuery.getField(), equalTo("prefix_field"));
        SpanTermQuery termQuery = (SpanTermQuery) fieldQuery.getMaskedQuery();
        assertThat(termQuery.getTerm().field(), equalTo("prefix_field._index_prefix"));
        assertThat(termQuery.getTerm().text(), equalTo("foo"));
    }
    {
        Query query = new SpanMultiTermQueryBuilder(new PrefixQueryBuilder(fieldName, "f")).toQuery(context);
        assertThat(query, instanceOf(SpanMultiTermQueryWrapper.class));
        SpanMultiTermQueryWrapper wrapper = (SpanMultiTermQueryWrapper) query;
        assertThat(wrapper.getWrappedQuery(), instanceOf(PrefixQuery.class));
        assertThat(wrapper.getField(), equalTo("prefix_field"));
        PrefixQuery prefixQuery = (PrefixQuery) wrapper.getWrappedQuery();
        assertThat(prefixQuery.getField(), equalTo("prefix_field"));
        assertThat(prefixQuery.getPrefix().text(), equalTo("f"));
        assertThat(wrapper.getRewriteMethod(), instanceOf(SpanBooleanQueryRewriteWithMaxClause.class));
        SpanBooleanQueryRewriteWithMaxClause rewrite = (SpanBooleanQueryRewriteWithMaxClause) wrapper.getRewriteMethod();
        assertThat(rewrite.getMaxExpansions(), equalTo(BooleanQuery.getMaxClauseCount()));
        assertTrue(rewrite.isHardLimit());
    }
}
Also used : 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) SpanMultiTermQueryWrapper(org.apache.lucene.queries.spans.SpanMultiTermQueryWrapper) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FieldMaskingSpanQuery(org.apache.lucene.queries.spans.FieldMaskingSpanQuery) SpanBooleanQueryRewriteWithMaxClause(org.opensearch.common.lucene.search.SpanBooleanQueryRewriteWithMaxClause) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 4 with SpanBooleanQueryRewriteWithMaxClause

use of org.opensearch.common.lucene.search.SpanBooleanQueryRewriteWithMaxClause in project OpenSearch by opensearch-project.

the class SpanMultiTermQueryBuilderTests method testTopNMultiTermsRewriteInsideSpan.

public void testTopNMultiTermsRewriteInsideSpan() throws Exception {
    Query query = QueryBuilders.spanMultiTermQueryBuilder(QueryBuilders.prefixQuery("body", "b").rewrite("top_terms_boost_2000")).toQuery(createShardContext());
    assertTrue(query instanceof SpanMultiTermQueryWrapper);
    if (query instanceof SpanMultiTermQueryWrapper) {
        MultiTermQuery.RewriteMethod rewriteMethod = ((SpanMultiTermQueryWrapper) query).getRewriteMethod();
        assertFalse(rewriteMethod instanceof SpanBooleanQueryRewriteWithMaxClause);
    }
}
Also used : 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) SpanMultiTermQueryWrapper(org.apache.lucene.queries.spans.SpanMultiTermQueryWrapper) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanBooleanQueryRewriteWithMaxClause(org.opensearch.common.lucene.search.SpanBooleanQueryRewriteWithMaxClause)

Example 5 with SpanBooleanQueryRewriteWithMaxClause

use of org.opensearch.common.lucene.search.SpanBooleanQueryRewriteWithMaxClause in project OpenSearch by opensearch-project.

the class SpanMultiTermQueryBuilderTests method testDefaultMaxRewriteBuilder.

public void testDefaultMaxRewriteBuilder() throws Exception {
    Query query = QueryBuilders.spanMultiTermQueryBuilder(QueryBuilders.prefixQuery("body", "b")).toQuery(createShardContext());
    assertTrue(query instanceof SpanMultiTermQueryWrapper);
    if (query instanceof SpanMultiTermQueryWrapper) {
        MultiTermQuery.RewriteMethod rewriteMethod = ((SpanMultiTermQueryWrapper) query).getRewriteMethod();
        assertTrue(rewriteMethod instanceof SpanBooleanQueryRewriteWithMaxClause);
    }
}
Also used : 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) SpanMultiTermQueryWrapper(org.apache.lucene.queries.spans.SpanMultiTermQueryWrapper) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanBooleanQueryRewriteWithMaxClause(org.opensearch.common.lucene.search.SpanBooleanQueryRewriteWithMaxClause)

Aggregations

SpanBooleanQueryRewriteWithMaxClause (org.opensearch.common.lucene.search.SpanBooleanQueryRewriteWithMaxClause)5 SpanMatchNoDocsQuery (org.apache.lucene.queries.SpanMatchNoDocsQuery)4 SpanMultiTermQueryWrapper (org.apache.lucene.queries.spans.SpanMultiTermQueryWrapper)4 BoostQuery (org.apache.lucene.search.BoostQuery)4 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)4 Query (org.apache.lucene.search.Query)4 FieldMaskingSpanQuery (org.apache.lucene.queries.spans.FieldMaskingSpanQuery)3 SpanQuery (org.apache.lucene.queries.spans.SpanQuery)3 SpanTermQuery (org.apache.lucene.queries.spans.SpanTermQuery)3 BooleanQuery (org.apache.lucene.search.BooleanQuery)3 PrefixQuery (org.apache.lucene.search.PrefixQuery)3 TermQuery (org.apache.lucene.search.TermQuery)3 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)1 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)1 TopTermsRewrite (org.apache.lucene.search.TopTermsRewrite)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)1