From be69a774dffa7be3335aed261e3091e417336c39 Mon Sep 17 00:00:00 2001 From: Jim Ferenczi Date: Fri, 23 Nov 2018 09:42:23 +0100 Subject: [PATCH] Fix analyzed prefix query in query_string (#35756) This change fixes analyzed prefix queries in `query_string` to be ignored if all terms are removed during the analysis. Closes #31702 --- .../index/search/QueryStringQueryParser.java | 11 +++++++---- .../index/query/QueryStringQueryBuilderTests.java | 10 ++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java b/server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java index bcf1e924315..84597d4d338 100644 --- a/server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java +++ b/server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java @@ -484,10 +484,13 @@ public class QueryStringQueryParser extends XQueryParser { List queries = new ArrayList<>(); for (Map.Entry entry : fields.entrySet()) { Query q = getPrefixQuerySingle(entry.getKey(), termStr); - assert q != null; - queries.add(applyBoost(q, entry.getValue())); + if (q != null) { + queries.add(applyBoost(q, entry.getValue())); + } } - if (queries.size() == 1) { + if (queries.isEmpty()) { + return null; + } else if (queries.size() == 1) { return queries.get(0); } else { float tiebreaker = groupTieBreaker == null ? type.tieBreaker() : groupTieBreaker; @@ -561,7 +564,7 @@ public class QueryStringQueryParser extends XQueryParser { } if (tlist.size() == 0) { - return super.getPrefixQuery(field, termStr); + return null; } if (tlist.size() == 1 && tlist.get(0).size() == 1) { diff --git a/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java index bb3f2751fa8..9ea98cebe71 100644 --- a/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java @@ -1422,6 +1422,16 @@ public class QueryStringQueryBuilderTests extends AbstractQueryTestCase