Search in sources :

Example 1 with FieldMaskingSpanQuery

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);
}
Also used : Query(org.apache.lucene.search.Query) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) FieldMaskingSpanQuery(org.apache.lucene.queries.spans.FieldMaskingSpanQuery) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) FieldMaskingSpanQuery(org.apache.lucene.queries.spans.FieldMaskingSpanQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) FieldMaskingSpanQuery(org.apache.lucene.queries.spans.FieldMaskingSpanQuery)

Example 2 with FieldMaskingSpanQuery

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));
}
Also used : Query(org.apache.lucene.search.Query) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) BoostQuery(org.apache.lucene.search.BoostQuery) FieldMaskingSpanQuery(org.apache.lucene.queries.spans.FieldMaskingSpanQuery) FieldMaskingSpanQuery(org.apache.lucene.queries.spans.FieldMaskingSpanQuery)

Example 3 with FieldMaskingSpanQuery

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);
}
Also used : Query(org.apache.lucene.search.Query) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) BoostQuery(org.apache.lucene.search.BoostQuery) FieldMaskingSpanQuery(org.apache.lucene.queries.spans.FieldMaskingSpanQuery) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) FieldMaskingSpanQuery(org.apache.lucene.queries.spans.FieldMaskingSpanQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 4 with FieldMaskingSpanQuery

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());
    }
}
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 5 with FieldMaskingSpanQuery

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));
    }
}
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

FieldMaskingSpanQuery (org.apache.lucene.queries.spans.FieldMaskingSpanQuery)7 Query (org.apache.lucene.search.Query)7 SpanTermQuery (org.apache.lucene.queries.spans.SpanTermQuery)6 SpanQuery (org.apache.lucene.queries.spans.SpanQuery)4 BooleanQuery (org.apache.lucene.search.BooleanQuery)4 BoostQuery (org.apache.lucene.search.BoostQuery)4 TermQuery (org.apache.lucene.search.TermQuery)4 Term (org.apache.lucene.index.Term)3 SpanMultiTermQueryWrapper (org.apache.lucene.queries.spans.SpanMultiTermQueryWrapper)3 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)3 PrefixQuery (org.apache.lucene.search.PrefixQuery)3 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Map (java.util.Map)2 Analyzer (org.apache.lucene.analysis.Analyzer)2 TokenStream (org.apache.lucene.analysis.TokenStream)2 FieldType (org.apache.lucene.document.FieldType)2 IndexOptions (org.apache.lucene.index.IndexOptions)2 SpanMatchNoDocsQuery (org.apache.lucene.queries.SpanMatchNoDocsQuery)2