Skip to content

Commit a921c37

Browse files
committed
Make sure we properly handle wildcard fields in query string queries.
1 parent 89b7937 commit a921c37

File tree

4 files changed

+92
-9
lines changed

4 files changed

+92
-9
lines changed

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.index.mapper.FieldMapper;
2626
import org.elasticsearch.index.mapper.IpFieldMapper;
2727
import org.elasticsearch.index.mapper.KeywordFieldMapper;
28+
import org.elasticsearch.index.mapper.MappedFieldType;
2829
import org.elasticsearch.index.mapper.MapperService;
2930
import org.elasticsearch.index.mapper.MetadataFieldMapper;
3031
import org.elasticsearch.index.mapper.NumberFieldMapper;
@@ -167,23 +168,27 @@ public static Map<String, Float> resolveMappingField(QueryShardContext context,
167168
if (fieldSuffix != null && context.fieldMapper(fieldName + fieldSuffix) != null) {
168169
fieldName = fieldName + fieldSuffix;
169170
}
170-
FieldMapper mapper = getFieldMapper(context.getMapperService(), fieldName);
171-
if (mapper == null) {
172-
// Unmapped fields are not ignored
173-
fields.put(fieldOrPattern, weight);
174-
continue;
175-
}
176-
if (acceptMetadataField == false && mapper instanceof MetadataFieldMapper) {
177-
// Ignore metadata fields
171+
172+
MappedFieldType fieldType = context.getMapperService().fullName(fieldName);
173+
if (fieldType == null) {
174+
// Note that we don't ignore unmapped fields.
175+
fields.put(fieldName, weight);
178176
continue;
179177
}
178+
180179
// Ignore fields that are not in the allowed mapper types. Some
181180
// types do not support term queries, and thus we cannot generate
182181
// a special query for them.
183-
String mappingType = mapper.fieldType().typeName();
182+
String mappingType = fieldType.typeName();
184183
if (acceptAllTypes == false && ALLOWED_QUERY_MAPPER_TYPES.contains(mappingType) == false) {
185184
continue;
186185
}
186+
187+
// Ignore metadata fields.
188+
FieldMapper mapper = getFieldMapper(context.getMapperService(), fieldName);
189+
if (acceptMetadataField == false && mapper instanceof MetadataFieldMapper) {
190+
continue;
191+
}
187192
fields.put(fieldName, weight);
188193
}
189194
checkForTooManyFields(fields);

server/src/test/java/org/elasticsearch/search/query/QueryStringIT.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,38 @@ public void testFieldAliasWithEmbeddedFieldNames() throws Exception {
409409
assertHits(response.getHits(), "3");
410410
}
411411

412+
public void testFieldAliasWithWildcardField() throws Exception {
413+
List<IndexRequestBuilder> indexRequests = new ArrayList<>();
414+
indexRequests.add(client().prepareIndex("test", "_doc", "1").setSource("f3", "text", "f2", "one"));
415+
indexRequests.add(client().prepareIndex("test", "_doc", "2").setSource("f3", "value", "f2", "two"));
416+
indexRequests.add(client().prepareIndex("test", "_doc", "3").setSource("f3", "another value", "f2", "three"));
417+
indexRandom(true, false, indexRequests);
418+
419+
SearchResponse response = client().prepareSearch("test")
420+
.setQuery(queryStringQuery("value").field("f3_*"))
421+
.execute().actionGet();
422+
423+
assertNoFailures(response);
424+
assertHitCount(response, 2);
425+
assertHits(response.getHits(), "2", "3");
426+
}
427+
428+
public void testFieldAliasOnDisallowedFieldType() throws Exception {
429+
List<IndexRequestBuilder> indexRequests = new ArrayList<>();
430+
indexRequests.add(client().prepareIndex("test", "_doc", "1").setSource("f3", "text", "f2", "one"));
431+
indexRandom(true, false, indexRequests);
432+
433+
// The wildcard field matches aliases for both a text and boolean field.
434+
// By default, the boolean field should be ignored when building the query.
435+
SearchResponse response = client().prepareSearch("test")
436+
.setQuery(queryStringQuery("text").field("f*_alias"))
437+
.execute().actionGet();
438+
439+
assertNoFailures(response);
440+
assertHitCount(response, 1);
441+
assertHits(response.getHits(), "1");
442+
}
443+
412444
private void assertHits(SearchHits hits, String... ids) {
413445
assertThat(hits.getTotalHits(), equalTo((long) ids.length));
414446
Set<String> hitIds = new HashSet<>();

server/src/test/java/org/elasticsearch/search/query/SimpleQueryStringIT.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import static java.util.Collections.singletonList;
5959
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
6060
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
61+
import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery;
6162
import static org.elasticsearch.index.query.QueryBuilders.simpleQueryStringQuery;
6263
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
6364
import static org.elasticsearch.test.StreamsUtils.copyToStringFromClasspath;
@@ -605,6 +606,47 @@ public void testFieldAlias() throws Exception {
605606
assertHits(response.getHits(), "2", "3");
606607
}
607608

609+
public void testFieldAliasWithWildcardField() throws Exception {
610+
String indexBody = copyToStringFromClasspath("/org/elasticsearch/search/query/all-query-index.json");
611+
assertAcked(prepareCreate("test").setSource(indexBody, XContentType.JSON));
612+
ensureGreen("test");
613+
614+
List<IndexRequestBuilder> indexRequests = new ArrayList<>();
615+
indexRequests.add(client().prepareIndex("test", "_doc", "1").setSource("f3", "text", "f2", "one"));
616+
indexRequests.add(client().prepareIndex("test", "_doc", "2").setSource("f3", "value", "f2", "two"));
617+
indexRequests.add(client().prepareIndex("test", "_doc", "3").setSource("f3", "another value", "f2", "three"));
618+
indexRandom(true, false, indexRequests);
619+
620+
SearchResponse response = client().prepareSearch("test")
621+
.setQuery(simpleQueryStringQuery("value").field("f3_*"))
622+
.execute().actionGet();
623+
624+
assertNoFailures(response);
625+
assertHitCount(response, 2);
626+
assertHits(response.getHits(), "2", "3");
627+
}
628+
629+
630+
public void testFieldAliasOnDisallowedFieldType() throws Exception {
631+
String indexBody = copyToStringFromClasspath("/org/elasticsearch/search/query/all-query-index.json");
632+
assertAcked(prepareCreate("test").setSource(indexBody, XContentType.JSON));
633+
ensureGreen("test");
634+
635+
List<IndexRequestBuilder> indexRequests = new ArrayList<>();
636+
indexRequests.add(client().prepareIndex("test", "_doc", "1").setSource("f3", "text", "f2", "one"));
637+
indexRandom(true, false, indexRequests);
638+
639+
// The wildcard field matches aliases for both a text and boolean field.
640+
// By default, the boolean field should be ignored when building the query.
641+
SearchResponse response = client().prepareSearch("test")
642+
.setQuery(queryStringQuery("text").field("f*_alias"))
643+
.execute().actionGet();
644+
645+
assertNoFailures(response);
646+
assertHitCount(response, 1);
647+
assertHits(response.getHits(), "1");
648+
}
649+
608650
private void assertHits(SearchHits hits, String... ids) {
609651
assertThat(hits.getTotalHits(), equalTo((long) ids.length));
610652
Set<String> hitIds = new HashSet<>();

server/src/test/resources/org/elasticsearch/search/query/all-query-index.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
"format": "yyyy/MM/dd||epoch_millis"
4747
},
4848
"f_bool": {"type": "boolean"},
49+
"f_bool_alias": {
50+
"type": "alias",
51+
"path": "f_bool"
52+
},
4953
"f_byte": {"type": "byte"},
5054
"f_short": {"type": "short"},
5155
"f_int": {"type": "integer"},

0 commit comments

Comments
 (0)