|
| 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.index.query.MatchPhraseQueryBuilder; |
| 10 | +import org.elasticsearch.index.query.ZeroTermsQueryOption; |
| 11 | +import org.elasticsearch.test.ESTestCase; |
| 12 | +import org.elasticsearch.xpack.esql.core.tree.Source; |
| 13 | +import org.elasticsearch.xpack.esql.core.tree.SourceTests; |
| 14 | +import org.elasticsearch.xpack.esql.core.util.StringUtils; |
| 15 | + |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.function.Function; |
| 20 | + |
| 21 | +import static org.elasticsearch.test.EqualsHashCodeTestUtils.checkEqualsAndHashCode; |
| 22 | +import static org.hamcrest.Matchers.equalTo; |
| 23 | + |
| 24 | +public class MatchPhraseQueryTests extends ESTestCase { |
| 25 | + static MatchPhraseQuery randomMatchPhraseQuery() { |
| 26 | + return new MatchPhraseQuery(SourceTests.randomSource(), randomAlphaOfLength(5), randomAlphaOfLength(5)); |
| 27 | + } |
| 28 | + |
| 29 | + public void testEqualsAndHashCode() { |
| 30 | + checkEqualsAndHashCode(randomMatchPhraseQuery(), MatchPhraseQueryTests::copy, MatchPhraseQueryTests::mutate); |
| 31 | + } |
| 32 | + |
| 33 | + private static MatchPhraseQuery copy(MatchPhraseQuery query) { |
| 34 | + return new MatchPhraseQuery(query.source(), query.name(), query.text(), query.options()); |
| 35 | + } |
| 36 | + |
| 37 | + private static MatchPhraseQuery mutate(MatchPhraseQuery query) { |
| 38 | + List<Function<MatchPhraseQuery, MatchPhraseQuery>> options = Arrays.asList( |
| 39 | + q -> new MatchPhraseQuery(SourceTests.mutate(q.source()), q.name(), q.text(), q.options()), |
| 40 | + q -> new MatchPhraseQuery(q.source(), randomValueOtherThan(q.name(), () -> randomAlphaOfLength(5)), q.text(), q.options()), |
| 41 | + q -> new MatchPhraseQuery(q.source(), q.name(), randomValueOtherThan(q.text(), () -> randomAlphaOfLength(5)), q.options()) |
| 42 | + ); |
| 43 | + return randomFrom(options).apply(query); |
| 44 | + } |
| 45 | + |
| 46 | + public void testQueryBuilding() { |
| 47 | + |
| 48 | + MatchPhraseQueryBuilder qb = getBuilder(Map.of("slop", 2, "zero_terms_query", "none")); |
| 49 | + assertThat(qb.slop(), equalTo(2)); |
| 50 | + assertThat(qb.zeroTermsQuery(), equalTo(ZeroTermsQueryOption.NONE)); |
| 51 | + |
| 52 | + Exception e = expectThrows(IllegalArgumentException.class, () -> getBuilder(Map.of("pizza", "yummy"))); |
| 53 | + assertThat(e.getMessage(), equalTo("illegal match option [pizza]")); |
| 54 | + |
| 55 | + e = expectThrows(NumberFormatException.class, () -> getBuilder(Map.of("slop", "mushrooms"))); |
| 56 | + assertThat(e.getMessage(), equalTo("For input string: \"mushrooms\"")); |
| 57 | + |
| 58 | + e = expectThrows(IllegalArgumentException.class, () -> getBuilder(Map.of("zero_terms_query", "pepperoni"))); |
| 59 | + assertThat(e.getMessage(), equalTo("unknown serialized type [pepperoni]")); |
| 60 | + } |
| 61 | + |
| 62 | + private static MatchPhraseQueryBuilder getBuilder(Map<String, Object> options) { |
| 63 | + final Source source = new Source(1, 1, StringUtils.EMPTY); |
| 64 | + // FieldAttribute fa = new FieldAttribute(EMPTY, "a", new EsField("af", KEYWORD, emptyMap(), true)); |
| 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 | + // FieldAttribute fa = new FieldAttribute(EMPTY, "a", new EsField("af", KEYWORD, emptyMap(), true)); |
| 72 | + final MatchPhraseQuery mpq = new MatchPhraseQuery(source, "eggplant", "foo bar"); |
| 73 | + assertEquals("MatchPhraseQuery@1:2[eggplant:foo bar]", mpq.toString()); |
| 74 | + } |
| 75 | +} |
0 commit comments