Skip to content

Commit ad35339

Browse files
committed
Fix full text queries, which were broken after the merge.
1 parent 58533cf commit ad35339

File tree

5 files changed

+18
-13
lines changed

5 files changed

+18
-13
lines changed

server/src/main/java/org/elasticsearch/index/mapper/FieldTypeLookup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ private MappedFieldType getKeyedFlatObjectField(String field) {
190190
/**
191191
* Returns a list of the full names of a simple match regex like pattern against full name and index name.
192192
*/
193-
public Collection<String> simpleMatchToFullName(String pattern) {
193+
public Set<String> simpleMatchToFullName(String pattern) {
194194
Set<String> fields = new HashSet<>();
195195
for (MappedFieldType fieldType : this) {
196196
if (Regex.simpleMatch(pattern, fieldType.name())) {

server/src/main/java/org/elasticsearch/index/mapper/MapperService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import com.carrotsearch.hppc.ObjectHashSet;
2323
import com.carrotsearch.hppc.cursors.ObjectCursor;
24-
2524
import org.apache.logging.log4j.LogManager;
2625
import org.apache.logging.log4j.message.ParameterizedMessage;
2726
import org.apache.lucene.analysis.Analyzer;
@@ -742,10 +741,10 @@ public MappedFieldType fullName(String fullName) {
742741
* Returns all the fields that match the given pattern. If the pattern is prefixed with a type
743742
* then the fields will be returned with a type prefix.
744743
*/
745-
public Collection<String> simpleMatchToFullName(String pattern) {
744+
public Set<String> simpleMatchToFullName(String pattern) {
746745
if (Regex.isSimpleMatchPattern(pattern) == false) {
747746
// no wildcards
748-
return Collections.singletonList(pattern);
747+
return Collections.singleton(pattern);
749748
}
750749
return fieldTypes.simpleMatchToFullName(pattern);
751750
}

server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@
5858
import org.elasticsearch.transport.RemoteClusterAware;
5959

6060
import java.io.IOException;
61-
import java.util.Collection;
6261
import java.util.HashMap;
6362
import java.util.List;
6463
import java.util.Map;
64+
import java.util.Set;
6565
import java.util.function.BiConsumer;
6666
import java.util.function.BiFunction;
6767
import java.util.function.Function;
@@ -191,7 +191,7 @@ public Map<String, Query> copyNamedQueries() {
191191
* Returns all the fields that match a given pattern. If prefixed with a
192192
* type then the fields will be returned with a type prefix.
193193
*/
194-
public Collection<String> simpleMatchToIndexNames(String pattern) {
194+
public Set<String> simpleMatchToIndexNames(String pattern) {
195195
return mapperService.simpleMatchToFullName(pattern);
196196
}
197197

server/src/main/java/org/elasticsearch/index/search/QueryParserHelper.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.HashMap;
3131
import java.util.List;
3232
import java.util.Map;
33+
import java.util.Set;
3334

3435
/**
3536
* Helpers to extract and expand field names and boosts
@@ -130,8 +131,9 @@ public static Map<String, Float> resolveMappingField(QueryShardContext context,
130131
*/
131132
public static Map<String, Float> resolveMappingField(QueryShardContext context, String fieldOrPattern, float weight,
132133
boolean acceptAllTypes, boolean acceptMetadataField, String fieldSuffix) {
133-
Collection<String> allFields = context.simpleMatchToIndexNames(fieldOrPattern);
134+
Set<String> allFields = context.simpleMatchToIndexNames(fieldOrPattern);
134135
Map<String, Float> fields = new HashMap<>();
136+
135137
for (String fieldName : allFields) {
136138
if (fieldSuffix != null && context.fieldMapper(fieldName + fieldSuffix) != null) {
137139
fieldName = fieldName + fieldSuffix;
@@ -159,13 +161,17 @@ public static Map<String, Float> resolveMappingField(QueryShardContext context,
159161
// other exceptions are parsing errors or not indexed fields: keep
160162
}
161163
}
162-
// handle duplicates
163-
float w = weight;
164-
if (fields.containsKey(fieldType.name())) {
165-
w *= fields.get(fieldType.name());
164+
165+
// Deduplicate aliases and their concrete fields.
166+
String resolvedFieldName = fieldType.name();
167+
if (allFields.contains(resolvedFieldName)) {
168+
fieldName = resolvedFieldName;
166169
}
167-
fields.put(fieldType.name(), w);
170+
171+
float w = fields.getOrDefault(fieldName, 1.0F);
172+
fields.put(fieldName, w * weight);
168173
}
174+
169175
checkForTooManyFields(fields, context);
170176
return fields;
171177
}

server/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldTypeTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void testTermQuery() {
6363
MapperService mapperService = mock(MapperService.class);
6464
when(mapperService.fullName("_field_names")).thenReturn(fieldNamesFieldType);
6565
when(mapperService.fullName("field_name")).thenReturn(fieldType);
66-
when(mapperService.simpleMatchToFullName("field_name")).thenReturn(Collections.singletonList("field_name"));
66+
when(mapperService.simpleMatchToFullName("field_name")).thenReturn(Collections.singleton("field_name"));
6767

6868
QueryShardContext queryShardContext = new QueryShardContext(0,
6969
indexSettings, null, null, null, mapperService, null, null, null, null, null, null, () -> 0L, null);

0 commit comments

Comments
 (0)