use of org.apache.lucene.search.TopTermsRewrite 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;
}
}
use of org.apache.lucene.search.TopTermsRewrite 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));
}
}
Aggregations