From ed457e4dace8f576059a6db3aa22368042e41749 Mon Sep 17 00:00:00 2001 From: Benjamin Trent Date: Tue, 8 Oct 2024 15:48:42 -0400 Subject: [PATCH] Fix analyzed wildcard query in simple_query_string when disjunctions is empty (#114264) (#114355) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change fixes analyzed wildcard query in simple_query_string when disjunctions is empty. Closes #114185 (cherry picked from commit 6955bc18a2e5e15f30b89191136596a605a2d808) Co-authored-by: Halil Bülent Orhon --- docs/changelog/114264.yaml | 5 ++++ .../search/query/SimpleQueryStringIT.java | 26 +++++++++++++++++++ .../search/SimpleQueryStringQueryParser.java | 3 +++ 3 files changed, 34 insertions(+) create mode 100644 docs/changelog/114264.yaml diff --git a/docs/changelog/114264.yaml b/docs/changelog/114264.yaml new file mode 100644 index 0000000000000..fe421f6422830 --- /dev/null +++ b/docs/changelog/114264.yaml @@ -0,0 +1,5 @@ +pr: 114264 +summary: "Fix analyzed wildcard query in simple_query_string when disjunctions is empty" +area: Search +type: bug +issues: [114185] diff --git a/server/src/internalClusterTest/java/org/elasticsearch/search/query/SimpleQueryStringIT.java b/server/src/internalClusterTest/java/org/elasticsearch/search/query/SimpleQueryStringIT.java index 7630ddb000140..7ff9304038cd9 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/search/query/SimpleQueryStringIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/search/query/SimpleQueryStringIT.java @@ -581,6 +581,32 @@ public void testFieldAliasOnDisallowedFieldType() throws Exception { }); } + public void testSimpleQueryStringWithAnalysisStopWords() throws Exception { + String mapping = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("properties") + .startObject("body") + .field("type", "text") + .field("analyzer", "stop") + .endObject() + .endObject() + .endObject() + ); + + CreateIndexRequestBuilder mappingRequest = indicesAdmin().prepareCreate("test1").setMapping(mapping); + mappingRequest.get(); + indexRandom(true, prepareIndex("test1").setId("1").setSource("body", "Some Text")); + refresh(); + + assertHitCount( + prepareSearch().setQuery( + simpleQueryStringQuery("the* text*").analyzeWildcard(true).defaultOperator(Operator.AND).field("body") + ), + 1 + ); + } + private void assertHits(SearchHits hits, String... ids) { assertThat(hits.getTotalHits().value, equalTo((long) ids.length)); Set hitIds = new HashSet<>(); diff --git a/server/src/main/java/org/elasticsearch/index/search/SimpleQueryStringQueryParser.java b/server/src/main/java/org/elasticsearch/index/search/SimpleQueryStringQueryParser.java index 860c2da21321c..a98fabd9a0a2e 100644 --- a/server/src/main/java/org/elasticsearch/index/search/SimpleQueryStringQueryParser.java +++ b/server/src/main/java/org/elasticsearch/index/search/SimpleQueryStringQueryParser.java @@ -198,6 +198,9 @@ public Query newPrefixQuery(String text) { if (disjuncts.size() == 1) { return disjuncts.get(0); } + if (disjuncts.size() == 0) { + return null; + } return new DisjunctionMaxQuery(disjuncts, 1.0f); }