Skip to content

Commit baee81b

Browse files
committed
Add MatchPhraseQueryTests
1 parent 3bcc654 commit baee81b

File tree

4 files changed

+231
-155
lines changed

4 files changed

+231
-155
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/MatchPhrase.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,12 @@ public class MatchPhrase extends FullTextFunction implements OptionalArgument, P
106106
MatchPhrase can be used on <<text, text>> fields, as well as other field types like keyword, boolean, or date types.
107107
MatchPhrase is not supported for <<semantic-text, semantic_text>> or numeric types.
108108
109-
MatchPhrase can use <<esql-function-named-params,function named parameters>> to specify additional options for the match_phrase query.
109+
MatchPhrase can use <<esql-function-named-params,function named parameters>> to specify additional options for the
110+
match_phrase query.
110111
All <<match-phrase-field-params,match_phrase query parameters>> are supported.
111112
112-
For a simplified syntax, you can use the <<esql-match-phrase-operator,match_phrase operator>> `:` operator instead of `MATCH_PHRASE`.
113+
For a simplified syntax, you can use the <<esql-match-phrase-operator,match_phrase operator>> `:` operator instead
114+
of `MATCH_PHRASE`.
113115
114116
`MATCH_PHRASE` returns true if the provided query matches the row.""",
115117
examples = {

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/querydsl/query/MatchPhraseQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class MatchPhraseQuery extends Query {
2828
static {
2929
BUILDER_APPLIERS = Map.ofEntries(
3030
entry(ANALYZER_FIELD.getPreferredName(), (qb, s) -> qb.analyzer(s.toString())),
31-
entry(SLOP_FIELD.getPreferredName(), (qb, i) -> qb.slop((Integer) i)),
31+
entry(SLOP_FIELD.getPreferredName(), (qb, s) -> qb.slop(Integer.parseInt(s.toString()))),
3232
entry(ZERO_TERMS_QUERY_FIELD.getPreferredName(), (qb, s) -> qb.zeroTermsQuery((String) s))
3333
);
3434
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
package org.elasticsearch.xpack.esql.querydsl.query;
8+
9+
import org.elasticsearch.ElasticsearchException;
10+
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
11+
import org.elasticsearch.index.query.ZeroTermsQueryOption;
12+
import org.elasticsearch.test.ESTestCase;
13+
import org.elasticsearch.xpack.esql.core.tree.Source;
14+
import org.elasticsearch.xpack.esql.core.tree.SourceTests;
15+
import org.elasticsearch.xpack.esql.core.util.StringUtils;
16+
17+
import java.util.Arrays;
18+
import java.util.List;
19+
import java.util.Map;
20+
import java.util.function.Function;
21+
22+
import static org.elasticsearch.test.EqualsHashCodeTestUtils.checkEqualsAndHashCode;
23+
import static org.hamcrest.Matchers.equalTo;
24+
25+
public class MatchPhraseQueryTests extends ESTestCase {
26+
static MatchPhraseQuery randomMatchPhraseQuery() {
27+
return new MatchPhraseQuery(SourceTests.randomSource(), randomAlphaOfLength(5), randomAlphaOfLength(5));
28+
}
29+
30+
public void testEqualsAndHashCode() {
31+
checkEqualsAndHashCode(randomMatchPhraseQuery(), MatchPhraseQueryTests::copy, MatchPhraseQueryTests::mutate);
32+
}
33+
34+
private static MatchPhraseQuery copy(MatchPhraseQuery query) {
35+
return new MatchPhraseQuery(query.source(), query.name(), query.text(), query.options());
36+
}
37+
38+
private static MatchPhraseQuery mutate(MatchPhraseQuery query) {
39+
List<Function<MatchPhraseQuery, MatchPhraseQuery>> options = Arrays.asList(
40+
q -> new MatchPhraseQuery(SourceTests.mutate(q.source()), q.name(), q.text(), q.options()),
41+
q -> new MatchPhraseQuery(q.source(), randomValueOtherThan(q.name(), () -> randomAlphaOfLength(5)), q.text(), q.options()),
42+
q -> new MatchPhraseQuery(q.source(), q.name(), randomValueOtherThan(q.text(), () -> randomAlphaOfLength(5)), q.options())
43+
);
44+
return randomFrom(options).apply(query);
45+
}
46+
47+
public void testQueryBuilding() {
48+
49+
MatchPhraseQueryBuilder qb = getBuilder(Map.of("slop", 2, "zero_terms_query", "none"));
50+
assertThat(qb.slop(), equalTo(2));
51+
assertThat(qb.zeroTermsQuery(), equalTo(ZeroTermsQueryOption.NONE));
52+
53+
Exception e = expectThrows(IllegalArgumentException.class, () -> getBuilder(Map.of("pizza", "yummy")));
54+
assertThat(e.getMessage(), equalTo("illegal match_phrase option [pizza]"));
55+
56+
e = expectThrows(NumberFormatException.class, () -> getBuilder(Map.of("slop", "mushrooms")));
57+
assertThat(e.getMessage(), equalTo("For input string: \"mushrooms\""));
58+
59+
e = expectThrows(ElasticsearchException.class, () -> getBuilder(Map.of("zero_terms_query", "pepperoni")));
60+
assertThat(e.getMessage(), equalTo("unknown serialized type [pepperoni]"));
61+
}
62+
63+
private static MatchPhraseQueryBuilder getBuilder(Map<String, Object> options) {
64+
final Source source = new Source(1, 1, StringUtils.EMPTY);
65+
final MatchPhraseQuery mpq = new MatchPhraseQuery(source, "eggplant", "foo bar", options);
66+
return (MatchPhraseQueryBuilder) mpq.asBuilder();
67+
}
68+
69+
public void testToString() {
70+
final Source source = new Source(1, 1, StringUtils.EMPTY);
71+
final MatchPhraseQuery mpq = new MatchPhraseQuery(source, "eggplant", "foo bar");
72+
assertEquals("MatchPhraseQuery@1:2[eggplant:foo bar]", mpq.toString());
73+
}
74+
}

0 commit comments

Comments
 (0)