From 9f3b251741ca2e4f42064f7a5af997120f3ff62c Mon Sep 17 00:00:00 2001 From: Chris Hegarty <62058229+ChrisHegarty@users.noreply.github.com> Date: Mon, 27 Sep 2021 13:38:51 +0100 Subject: [PATCH] Fix split package org.apache.lucene.queryparser.classic (#78307) With LUCENE-10115 integrated, we can now remove the package -private dependency with org.apache.lucene.queryparser.classic. XQueryParser, and override getFuzzyDistance to customise the calculation of the similarity distance for fuzzy queries. --- server/build.gradle | 3 +- .../queryparser/classic/XQueryParser.java | 29 ------------------- .../index/search/QueryStringQueryParser.java | 17 +++++------ 3 files changed, 9 insertions(+), 40 deletions(-) delete mode 100644 server/src/main/java/org/apache/lucene/queryparser/classic/XQueryParser.java diff --git a/server/build.gradle b/server/build.gradle index c8b354d92ec79..c1a0b05f13a88 100644 --- a/server/build.gradle +++ b/server/build.gradle @@ -269,8 +269,7 @@ tasks.named("licenseHeaders").configure { tasks.named('splitPackagesAudit').configure { // Lucene packages should be owned by Lucene! - ignoreClasses 'org.apache.lucene.queryparser.classic.XQueryParser', - 'org.apache.lucene.queries.BinaryDocValuesRangeQuery', + ignoreClasses 'org.apache.lucene.queries.BinaryDocValuesRangeQuery', 'org.apache.lucene.queries.BlendedTermQuery', 'org.apache.lucene.queries.SpanMatchNoDocsQuery', 'org.apache.lucene.search.grouping.CollapseTopFieldDocs', diff --git a/server/src/main/java/org/apache/lucene/queryparser/classic/XQueryParser.java b/server/src/main/java/org/apache/lucene/queryparser/classic/XQueryParser.java deleted file mode 100644 index aa840bca89dc2..0000000000000 --- a/server/src/main/java/org/apache/lucene/queryparser/classic/XQueryParser.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -package org.apache.lucene.queryparser.classic; - -import org.apache.lucene.analysis.Analyzer; -import org.apache.lucene.search.Query; - -/** - * This class is just a workaround to make {@link QueryParser#handleBareFuzzy(String, Token, String)} accessible by sub-classes. - * It is needed for {@link QueryParser}s that need to override the parsing of the slop in a fuzzy query (e.g. word~2, word~). - * - * TODO: We should maybe rewrite this with the flexible query parser which matches the same syntax with more freedom. - */ -public class XQueryParser extends QueryParser { - public XQueryParser(String f, Analyzer a) { - super(f, a); - } - - @Override - protected Query handleBareFuzzy(String field, Token fuzzySlop, String termImage) throws ParseException { - return super.handleBareFuzzy(field, fuzzySlop, termImage); - } -} 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 e4d1d52bd9939..4b5536cfb2b87 100644 --- a/server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java +++ b/server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java @@ -17,8 +17,8 @@ import org.apache.lucene.queries.spans.SpanOrQuery; import org.apache.lucene.queries.spans.SpanQuery; import org.apache.lucene.queryparser.classic.ParseException; +import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.queryparser.classic.Token; -import org.apache.lucene.queryparser.classic.XQueryParser; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BoostAttribute; import org.apache.lucene.search.BoostQuery; @@ -64,12 +64,12 @@ import static org.elasticsearch.index.search.QueryParserHelper.resolveMappingFields; /** - * A {@link XQueryParser} that uses the {@link MapperService} in order to build smarter + * A {@link QueryParser} that uses the {@link MapperService} in order to build smarter * queries based on the mapping information. - * This class uses {@link MultiMatchQueryParser} to build the text query around operators and {@link XQueryParser} + * This class uses {@link MultiMatchQueryParser} to build the text query around operators and {@link QueryParser} * to assemble the result logically. */ -public class QueryStringQueryParser extends XQueryParser { +public class QueryStringQueryParser extends QueryParser { private static final String EXISTS_FIELD = "_exists_"; private final SearchExecutionContext context; @@ -433,12 +433,11 @@ private Query getRangeQuerySingle(String field, String part1, String part2, } @Override - protected Query handleBareFuzzy(String field, Token fuzzySlop, String termImage) throws ParseException { - if (fuzzySlop.image.length() == 1) { - return getFuzzyQuery(field, termImage, fuzziness.asDistance(termImage)); + protected float getFuzzyDistance(Token fuzzyToken, String termStr) { + if (fuzzyToken.image.length() == 1) { + return fuzziness.asDistance(termStr); } - float distance = Fuzziness.fromString(fuzzySlop.image.substring(1)).asDistance(termImage); - return getFuzzyQuery(field, termImage, distance); + return Fuzziness.fromString(fuzzyToken.image.substring(1)).asDistance(termStr); } @Override