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.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.MapperService;
import java.io.IOException;
import java.util.Collections;
@ -123,7 +124,12 @@ public class SimpleQueryStringParser implements QueryParser {
fieldsAndWeights.put(fieldName, fBoost);
}
} else {
fieldsAndWeights.put(fField, fBoost);
MapperService.SmartNameFieldMappers mappers = parseContext.smartFieldMappers(fField);
if (mappers != null && mappers.hasMapper()) {
fieldsAndWeights.put(mappers.mapper().names().indexName(), fBoost);
} else {
fieldsAndWeights.put(fField, fBoost);
}
}
}
} else {

View File

@ -1979,6 +1979,46 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
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
public void testSimpleQueryStringFlags() {
assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1));