use of org.opensearch.index.search.MatchQuery in project OpenSearch by opensearch-project.
the class MatchPhrasePrefixQueryBuilder method doToQuery.
@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
// validate context specific fields
if (analyzer != null && context.getIndexAnalyzers().get(analyzer) == null) {
throw new QueryShardException(context, "[" + NAME + "] analyzer [" + analyzer + "] not found");
}
MatchQuery matchQuery = new MatchQuery(context);
if (analyzer != null) {
matchQuery.setAnalyzer(analyzer);
}
matchQuery.setPhraseSlop(slop);
matchQuery.setMaxExpansions(maxExpansions);
matchQuery.setZeroTermsQuery(zeroTermsQuery);
return matchQuery.parse(MatchQuery.Type.PHRASE_PREFIX, fieldName, value);
}
use of org.opensearch.index.search.MatchQuery in project OpenSearch by opensearch-project.
the class MatchBoolPrefixQueryBuilder method doToQuery.
@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
if (analyzer != null && context.getIndexAnalyzers().get(analyzer) == null) {
throw new QueryShardException(context, "[" + NAME + "] analyzer [" + analyzer + "] not found");
}
final MatchQuery matchQuery = new MatchQuery(context);
if (analyzer != null) {
matchQuery.setAnalyzer(analyzer);
}
matchQuery.setOccur(operator.toBooleanClauseOccur());
matchQuery.setFuzziness(fuzziness);
matchQuery.setFuzzyPrefixLength(prefixLength);
matchQuery.setMaxExpansions(maxExpansions);
matchQuery.setTranspositions(fuzzyTranspositions);
matchQuery.setFuzzyRewriteMethod(QueryParsers.parseRewriteMethod(fuzzyRewrite, null, LoggingDeprecationHandler.INSTANCE));
final Query query = matchQuery.parse(MatchQuery.Type.BOOLEAN_PREFIX, fieldName, value);
return Queries.maybeApplyMinimumShouldMatch(query, minimumShouldMatch);
}
use of org.opensearch.index.search.MatchQuery in project OpenSearch by opensearch-project.
the class MatchQueryBuilder method doToQuery.
@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
// validate context specific fields
if (analyzer != null && context.getIndexAnalyzers().get(analyzer) == null) {
throw new QueryShardException(context, "[" + NAME + "] analyzer [" + analyzer + "] not found");
}
MatchQuery matchQuery = new MatchQuery(context);
matchQuery.setOccur(operator.toBooleanClauseOccur());
if (analyzer != null) {
matchQuery.setAnalyzer(analyzer);
}
matchQuery.setFuzziness(fuzziness);
matchQuery.setFuzzyPrefixLength(prefixLength);
matchQuery.setMaxExpansions(maxExpansions);
matchQuery.setTranspositions(fuzzyTranspositions);
matchQuery.setFuzzyRewriteMethod(QueryParsers.parseRewriteMethod(fuzzyRewrite, null, LoggingDeprecationHandler.INSTANCE));
matchQuery.setLenient(lenient);
matchQuery.setCommonTermsCutoff(cutoffFrequency);
matchQuery.setZeroTermsQuery(zeroTermsQuery);
matchQuery.setAutoGenerateSynonymsPhraseQuery(autoGenerateSynonymsPhraseQuery);
Query query = matchQuery.parse(MatchQuery.Type.BOOLEAN, fieldName, value);
return Queries.maybeApplyMinimumShouldMatch(query, minimumShouldMatch);
}
use of org.opensearch.index.search.MatchQuery in project OpenSearch by opensearch-project.
the class TextFieldMapperTests method testFastPhrasePrefixes.
public void testFastPhrasePrefixes() throws IOException {
MapperService mapperService = createMapperService(mapping(b -> {
b.startObject("field");
{
b.field("type", "text");
b.field("analyzer", "my_stop_analyzer");
b.startObject("index_prefixes").field("min_chars", 2).field("max_chars", 10).endObject();
}
b.endObject();
b.startObject("synfield");
{
b.field("type", "text");
// "standard" will be replaced with MockSynonymAnalyzer
b.field("analyzer", "standard");
b.field("index_phrases", true);
b.startObject("index_prefixes").field("min_chars", 2).field("max_chars", 10).endObject();
}
b.endObject();
}));
QueryShardContext queryShardContext = createQueryShardContext(mapperService);
{
Query q = new MatchPhrasePrefixQueryBuilder("field", "two words").toQuery(queryShardContext);
Query expected = new SpanNearQuery.Builder("field", true).addClause(new SpanTermQuery(new Term("field", "two"))).addClause(new FieldMaskingSpanQuery(new SpanTermQuery(new Term("field._index_prefix", "words")), "field")).build();
assertThat(q, equalTo(expected));
}
{
Query q = new MatchPhrasePrefixQueryBuilder("field", "three words here").toQuery(queryShardContext);
Query expected = new SpanNearQuery.Builder("field", true).addClause(new SpanTermQuery(new Term("field", "three"))).addClause(new SpanTermQuery(new Term("field", "words"))).addClause(new FieldMaskingSpanQuery(new SpanTermQuery(new Term("field._index_prefix", "here")), "field")).build();
assertThat(q, equalTo(expected));
}
{
Query q = new MatchPhrasePrefixQueryBuilder("field", "two words").slop(1).toQuery(queryShardContext);
MultiPhrasePrefixQuery mpq = new MultiPhrasePrefixQuery("field");
mpq.setSlop(1);
mpq.add(new Term("field", "two"));
mpq.add(new Term("field", "words"));
assertThat(q, equalTo(mpq));
}
{
Query q = new MatchPhrasePrefixQueryBuilder("field", "singleton").toQuery(queryShardContext);
assertThat(q, is(new SynonymQuery.Builder("field._index_prefix").addTerm(new Term("field._index_prefix", "singleton")).build()));
}
{
Query q = new MatchPhrasePrefixQueryBuilder("field", "sparkle a stopword").toQuery(queryShardContext);
Query expected = new SpanNearQuery.Builder("field", true).addClause(new SpanTermQuery(new Term("field", "sparkle"))).addGap(1).addClause(new FieldMaskingSpanQuery(new SpanTermQuery(new Term("field._index_prefix", "stopword")), "field")).build();
assertThat(q, equalTo(expected));
}
{
MatchQuery matchQuery = new MatchQuery(queryShardContext);
matchQuery.setAnalyzer(new MockSynonymAnalyzer());
Query q = matchQuery.parse(MatchQuery.Type.PHRASE_PREFIX, "synfield", "motor dogs");
Query expected = new SpanNearQuery.Builder("synfield", true).addClause(new SpanTermQuery(new Term("synfield", "motor"))).addClause(new SpanOrQuery(new FieldMaskingSpanQuery(new SpanTermQuery(new Term("synfield._index_prefix", "dogs")), "synfield"), new FieldMaskingSpanQuery(new SpanTermQuery(new Term("synfield._index_prefix", "dog")), "synfield"))).build();
assertThat(q, equalTo(expected));
}
{
MatchQuery matchQuery = new MatchQuery(queryShardContext);
matchQuery.setPhraseSlop(1);
matchQuery.setAnalyzer(new MockSynonymAnalyzer());
Query q = matchQuery.parse(MatchQuery.Type.PHRASE_PREFIX, "synfield", "two dogs");
MultiPhrasePrefixQuery mpq = new MultiPhrasePrefixQuery("synfield");
mpq.setSlop(1);
mpq.add(new Term("synfield", "two"));
mpq.add(new Term[] { new Term("synfield", "dogs"), new Term("synfield", "dog") });
assertThat(q, equalTo(mpq));
}
{
Query q = new MatchPhrasePrefixQueryBuilder("field", "motor d").toQuery(queryShardContext);
MultiPhrasePrefixQuery mpq = new MultiPhrasePrefixQuery("field");
mpq.add(new Term("field", "motor"));
mpq.add(new Term("field", "d"));
assertThat(q, equalTo(mpq));
}
}
use of org.opensearch.index.search.MatchQuery in project OpenSearch by opensearch-project.
the class MatchQueryBuilderTests method testAutoGenerateSynonymsPhraseQuery.
public void testAutoGenerateSynonymsPhraseQuery() throws Exception {
final MatchQuery matchQuery = new MatchQuery(createShardContext());
matchQuery.setAnalyzer(new MockSynonymAnalyzer());
{
matchQuery.setAutoGenerateSynonymsPhraseQuery(false);
final Query query = matchQuery.parse(Type.BOOLEAN, TEXT_FIELD_NAME, "guinea pig");
final Query expectedQuery = new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new TermQuery(new Term(TEXT_FIELD_NAME, "guinea")), BooleanClause.Occur.MUST).add(new TermQuery(new Term(TEXT_FIELD_NAME, "pig")), BooleanClause.Occur.MUST).build(), BooleanClause.Occur.SHOULD).add(new TermQuery(new Term(TEXT_FIELD_NAME, "cavy")), BooleanClause.Occur.SHOULD).build(), BooleanClause.Occur.SHOULD).build();
assertThat(query, equalTo(expectedQuery));
}
{
matchQuery.setAutoGenerateSynonymsPhraseQuery(true);
final Query query = matchQuery.parse(Type.BOOLEAN, TEXT_FIELD_NAME, "guinea pig");
final Query expectedQuery = new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new PhraseQuery.Builder().add(new Term(TEXT_FIELD_NAME, "guinea")).add(new Term(TEXT_FIELD_NAME, "pig")).build(), BooleanClause.Occur.SHOULD).add(new TermQuery(new Term(TEXT_FIELD_NAME, "cavy")), BooleanClause.Occur.SHOULD).build(), BooleanClause.Occur.SHOULD).build();
assertThat(query, equalTo(expectedQuery));
}
{
matchQuery.setAutoGenerateSynonymsPhraseQuery(false);
final Query query = matchQuery.parse(Type.BOOLEAN_PREFIX, TEXT_FIELD_NAME, "guinea pig");
final Query expectedQuery = new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new TermQuery(new Term(TEXT_FIELD_NAME, "guinea")), BooleanClause.Occur.MUST).add(new TermQuery(new Term(TEXT_FIELD_NAME, "pig")), BooleanClause.Occur.MUST).build(), BooleanClause.Occur.SHOULD).add(new TermQuery(new Term(TEXT_FIELD_NAME, "cavy")), BooleanClause.Occur.SHOULD).build(), BooleanClause.Occur.SHOULD).build();
assertThat(query, equalTo(expectedQuery));
}
{
matchQuery.setAutoGenerateSynonymsPhraseQuery(true);
final Query query = matchQuery.parse(Type.BOOLEAN_PREFIX, TEXT_FIELD_NAME, "guinea pig");
final MultiPhrasePrefixQuery guineaPig = new MultiPhrasePrefixQuery(TEXT_FIELD_NAME);
guineaPig.add(new Term(TEXT_FIELD_NAME, "guinea"));
guineaPig.add(new Term(TEXT_FIELD_NAME, "pig"));
final MultiPhrasePrefixQuery cavy = new MultiPhrasePrefixQuery(TEXT_FIELD_NAME);
cavy.add(new Term(TEXT_FIELD_NAME, "cavy"));
final Query expectedQuery = new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(guineaPig, BooleanClause.Occur.SHOULD).add(cavy, BooleanClause.Occur.SHOULD).build(), BooleanClause.Occur.SHOULD).build();
assertThat(query, equalTo(expectedQuery));
}
}
Aggregations