use of org.apache.lucene.queries.spans.FieldMaskingSpanQuery in project OpenSearch by opensearch-project.
the class FieldMaskingSpanQueryBuilder method doToQuery.
@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
String fieldInQuery = fieldName;
MappedFieldType fieldType = context.fieldMapper(fieldName);
if (fieldType != null) {
fieldInQuery = fieldType.name();
}
Query innerQuery = queryBuilder.toQuery(context);
assert innerQuery instanceof SpanQuery;
return new FieldMaskingSpanQuery((SpanQuery) innerQuery, fieldInQuery);
}
use of org.apache.lucene.queries.spans.FieldMaskingSpanQuery in project OpenSearch by opensearch-project.
the class FieldMaskingSpanQueryBuilderTests method doAssertLuceneQuery.
@Override
protected void doAssertLuceneQuery(FieldMaskingSpanQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
String fieldInQuery = expectedFieldName(queryBuilder.fieldName());
assertThat(query, instanceOf(FieldMaskingSpanQuery.class));
FieldMaskingSpanQuery fieldMaskingSpanQuery = (FieldMaskingSpanQuery) query;
assertThat(fieldMaskingSpanQuery.getField(), equalTo(fieldInQuery));
Query sub = queryBuilder.innerQuery().toQuery(context);
assertThat(fieldMaskingSpanQuery.getMaskedQuery(), equalTo(sub));
}
use of org.apache.lucene.queries.spans.FieldMaskingSpanQuery in project OpenSearch by opensearch-project.
the class FieldMaskingSpanQueryBuilderTests method testJsonSpanTermWithBoost.
public void testJsonSpanTermWithBoost() throws IOException {
String json = "{\n" + " \"span_field_masking\" : {\n" + " \"query\" : {\n" + " \"span_term\" : {\n" + " \"value\" : {\n" + " \"value\" : \"term\"\n" + " }\n" + " }\n" + " },\n" + " \"field\" : \"mapped_geo_shape\",\n" + " \"boost\" : 42.0,\n" + " \"_name\" : \"KPI\"\n" + " }\n" + "}";
Query query = parseQuery(json).toQuery(createShardContext());
assertEquals(new BoostQuery(new FieldMaskingSpanQuery(new SpanTermQuery(new Term("value", "term")), "mapped_geo_shape"), 42f), query);
}
use of org.apache.lucene.queries.spans.FieldMaskingSpanQuery 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());
}
}
use of org.apache.lucene.queries.spans.FieldMaskingSpanQuery 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