Allow specifying nested fields in simple_query_string

Fixes #5091
This commit is contained in:
Lee Hinman 2014-02-12 21:41:20 -07:00
parent eb0e1aa38f
commit 28c6dc3e19
2 changed files with 47 additions and 1 deletions

View File

@ -28,6 +28,7 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.MapperService;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
@ -122,10 +123,15 @@ public class SimpleQueryStringParser implements QueryParser {
for (String fieldName : parseContext.mapperService().simpleMatchToIndexNames(fField)) { for (String fieldName : parseContext.mapperService().simpleMatchToIndexNames(fField)) {
fieldsAndWeights.put(fieldName, fBoost); fieldsAndWeights.put(fieldName, fBoost);
} }
} else {
MapperService.SmartNameFieldMappers mappers = parseContext.smartFieldMappers(fField);
if (mappers != null && mappers.hasMapper()) {
fieldsAndWeights.put(mappers.mapper().names().indexName(), fBoost);
} else { } else {
fieldsAndWeights.put(fField, fBoost); fieldsAndWeights.put(fField, fBoost);
} }
} }
}
} else { } else {
throw new QueryParsingException(parseContext.index(), throw new QueryParsingException(parseContext.index(),
"[" + NAME + "] query does not support [" + currentFieldName + "]"); "[" + NAME + "] query does not support [" + currentFieldName + "]");

View File

@ -1979,6 +1979,46 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
assertSearchHits(searchResponse, "5", "6"); assertSearchHits(searchResponse, "5", "6");
} }
@Test
public void testNestedFieldSimpleQueryString() throws IOException {
assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)
.addMapping("type1", jsonBuilder()
.startObject()
.startObject("type1")
.startObject("properties")
.startObject("body").field("type", "string")
.startObject("fields")
.startObject("sub").field("type", "string")
.endObject() // sub
.endObject() // fields
.endObject() // body
.endObject() // properties
.endObject() // type1
.endObject()));
client().prepareIndex("test", "type1", "1").setSource("body", "foo bar baz").get();
refresh();
SearchResponse searchResponse = client().prepareSearch().setQuery(
simpleQueryString("foo bar baz").field("body")).get();
assertHitCount(searchResponse, 1l);
assertSearchHits(searchResponse, "1");
searchResponse = client().prepareSearch().setQuery(
simpleQueryString("foo bar baz").field("type1.body")).get();
assertHitCount(searchResponse, 1l);
assertSearchHits(searchResponse, "1");
searchResponse = client().prepareSearch().setQuery(
simpleQueryString("foo bar baz").field("body.sub")).get();
assertHitCount(searchResponse, 1l);
assertSearchHits(searchResponse, "1");
searchResponse = client().prepareSearch().setQuery(
simpleQueryString("foo bar baz").field("type1.body.sub")).get();
assertHitCount(searchResponse, 1l);
assertSearchHits(searchResponse, "1");
}
@Test @Test
public void testSimpleQueryStringFlags() { public void testSimpleQueryStringFlags() {
assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1));