From 306826874b1941e43e746120cdd5511128d58a79 Mon Sep 17 00:00:00 2001 From: Julie Tibshirani Date: Mon, 8 Feb 2021 16:15:44 -0800 Subject: [PATCH 1/3] Clarify that cross-fields only supports boolean queries. --- .../index/search/MatchQueryParser.java | 11 +---- .../index/search/MultiMatchQueryParser.java | 41 ++++++++++++------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/search/MatchQueryParser.java b/server/src/main/java/org/elasticsearch/index/search/MatchQueryParser.java index a428bd547426e..5390d8aed22e9 100644 --- a/server/src/main/java/org/elasticsearch/index/search/MatchQueryParser.java +++ b/server/src/main/java/org/elasticsearch/index/search/MatchQueryParser.java @@ -258,32 +258,23 @@ public Query parse(Type type, String fieldName, Object value) throws IOException } } - return parseInternal(type, fieldType.name(), builder, value); - } - - protected final Query parseInternal(Type type, String fieldName, MatchQueryBuilder builder, Object value) throws IOException { - final Query query; + Query query; switch (type) { case BOOLEAN: query = builder.createBooleanQuery(fieldName, value.toString(), occur); break; - case BOOLEAN_PREFIX: query = builder.createBooleanPrefixQuery(fieldName, value.toString(), occur); break; - case PHRASE: query = builder.createPhraseQuery(fieldName, value.toString(), phraseSlop); break; - case PHRASE_PREFIX: query = builder.createPhrasePrefixQuery(fieldName, value.toString(), phraseSlop); break; - default: throw new IllegalStateException("No type found for [" + type + "]"); } - return query == null ? zeroTermsQuery() : query; } diff --git a/server/src/main/java/org/elasticsearch/index/search/MultiMatchQueryParser.java b/server/src/main/java/org/elasticsearch/index/search/MultiMatchQueryParser.java index 6c4d8c1c9bdfe..c4822c80e0ae3 100644 --- a/server/src/main/java/org/elasticsearch/index/search/MultiMatchQueryParser.java +++ b/server/src/main/java/org/elasticsearch/index/search/MultiMatchQueryParser.java @@ -12,6 +12,7 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.index.Term; import org.apache.lucene.queries.BlendedTermQuery; +import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BoostQuery; import org.apache.lucene.search.DisjunctionMaxQuery; import org.apache.lucene.search.MatchNoDocsQuery; @@ -66,7 +67,7 @@ public Query parse(MultiMatchQueryBuilder.Type type, Map fieldNam break; case CROSS_FIELDS: - queries = buildCrossFieldQuery(type, fieldNames, value, minimumShouldMatch, tieBreaker); + queries = buildCrossFieldQuery(fieldNames, value, minimumShouldMatch, tieBreaker); break; default: @@ -108,15 +109,16 @@ private List buildFieldQueries(MultiMatchQueryBuilder.Type type, Map buildCrossFieldQuery(MultiMatchQueryBuilder.Type type, Map fieldNames, - Object value, String minimumShouldMatch, float tieBreaker) throws IOException { + private List buildCrossFieldQuery(Map fieldNames, + Object value, String minimumShouldMatch, float tieBreaker) { + Map> groups = new HashMap<>(); List queries = new ArrayList<>(); for (Map.Entry entry : fieldNames.entrySet()) { String name = entry.getKey(); MappedFieldType fieldType = context.getFieldType(name); if (fieldType != null) { - Analyzer actualAnalyzer = getAnalyzer(fieldType, type == MultiMatchQueryBuilder.Type.PHRASE); + Analyzer actualAnalyzer = getAnalyzer(fieldType, false); if (groups.containsKey(actualAnalyzer) == false) { groups.put(actualAnalyzer, new ArrayList<>()); } @@ -140,7 +142,11 @@ private List buildCrossFieldQuery(MultiMatchQueryBuilder.Type type, Map disjunctions = new ArrayList<>(); - for (FieldAndBoost fieldType : blendedFields) { - Query query = fieldType.fieldType.prefixQuery(term.text(), null, context); - if (fieldType.boost != 1f) { - query = new BoostQuery(query, fieldType.boost); - } - disjunctions.add(query); - } - return new DisjunctionMaxQuery(disjunctions, tieBreaker); + throw new IllegalArgumentException("[multi_match] queries in [cross_fields] mode don't support prefix"); } @Override From c5545d884abcedaa6c4df321c1229347387359c6 Mon Sep 17 00:00:00 2001 From: Julie Tibshirani Date: Tue, 9 Feb 2021 15:47:44 -0800 Subject: [PATCH 2/3] Rename BlendedQueryBuilder -> CrossFieldsQueryBuilder. --- .../elasticsearch/index/search/MultiMatchQueryParser.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/search/MultiMatchQueryParser.java b/server/src/main/java/org/elasticsearch/index/search/MultiMatchQueryParser.java index c4822c80e0ae3..ff255c4eca278 100644 --- a/server/src/main/java/org/elasticsearch/index/search/MultiMatchQueryParser.java +++ b/server/src/main/java/org/elasticsearch/index/search/MultiMatchQueryParser.java @@ -132,7 +132,7 @@ private List buildCrossFieldQuery(Map fieldNames, builder = new MatchQueryBuilder(group.getKey(), group.getValue().get(0).fieldType, enablePositionIncrements, autoGenerateSynonymsPhraseQuery); } else { - builder = new BlendedQueryBuilder(group.getKey(), group.getValue(), tieBreaker, + builder = new CrossFieldsQueryBuilder(group.getKey(), group.getValue(), tieBreaker, enablePositionIncrements, autoGenerateSynonymsPhraseQuery); } @@ -163,11 +163,11 @@ private List buildCrossFieldQuery(Map fieldNames, return queries; } - private class BlendedQueryBuilder extends MatchQueryBuilder { + private class CrossFieldsQueryBuilder extends MatchQueryBuilder { private final List blendedFields; private final float tieBreaker; - BlendedQueryBuilder(Analyzer analyzer, List blendedFields, float tieBreaker, + CrossFieldsQueryBuilder(Analyzer analyzer, List blendedFields, float tieBreaker, boolean enablePositionIncrements, boolean autoGenerateSynonymsPhraseQuery) { super(analyzer, blendedFields.get(0).fieldType, enablePositionIncrements, autoGenerateSynonymsPhraseQuery); this.blendedFields = blendedFields; From 27f5506ae9ed63be107675bfe3f0f512531ad6e5 Mon Sep 17 00:00:00 2001 From: Julie Tibshirani Date: Tue, 9 Feb 2021 16:32:57 -0800 Subject: [PATCH 3/3] Make sure aliases are handled correctly. --- .../elasticsearch/index/search/MatchQueryParser.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/search/MatchQueryParser.java b/server/src/main/java/org/elasticsearch/index/search/MatchQueryParser.java index 5390d8aed22e9..7a3b060f6485c 100644 --- a/server/src/main/java/org/elasticsearch/index/search/MatchQueryParser.java +++ b/server/src/main/java/org/elasticsearch/index/search/MatchQueryParser.java @@ -241,6 +241,8 @@ public Query parse(Type type, String fieldName, Object value) throws IOException assert analyzer != null; MatchQueryBuilder builder = new MatchQueryBuilder(analyzer, fieldType, enablePositionIncrements, autoGenerateSynonymsPhraseQuery); + String resolvedFieldName = fieldType.name(); + String stringValue = value.toString(); /* * If a keyword analyzer is used, we know that further analysis isn't @@ -249,7 +251,7 @@ public Query parse(Type type, String fieldName, Object value) throws IOException * a prefix query instead */ if (analyzer == Lucene.KEYWORD_ANALYZER && type != Type.PHRASE_PREFIX) { - final Term term = new Term(fieldType.name(), value.toString()); + final Term term = new Term(resolvedFieldName, stringValue); if (type == Type.BOOLEAN_PREFIX && (fieldType instanceof TextFieldMapper.TextFieldType || fieldType instanceof KeywordFieldMapper.KeywordFieldType)) { return builder.newPrefixQuery(term); @@ -261,16 +263,16 @@ public Query parse(Type type, String fieldName, Object value) throws IOException Query query; switch (type) { case BOOLEAN: - query = builder.createBooleanQuery(fieldName, value.toString(), occur); + query = builder.createBooleanQuery(resolvedFieldName, stringValue, occur); break; case BOOLEAN_PREFIX: - query = builder.createBooleanPrefixQuery(fieldName, value.toString(), occur); + query = builder.createBooleanPrefixQuery(resolvedFieldName, stringValue, occur); break; case PHRASE: - query = builder.createPhraseQuery(fieldName, value.toString(), phraseSlop); + query = builder.createPhraseQuery(resolvedFieldName, stringValue, phraseSlop); break; case PHRASE_PREFIX: - query = builder.createPhrasePrefixQuery(fieldName, value.toString(), phraseSlop); + query = builder.createPhrasePrefixQuery(resolvedFieldName, stringValue, phraseSlop); break; default: throw new IllegalStateException("No type found for [" + type + "]");