Search Fields: If a field is not stored, automatically extract it from _source (without the need for _source prefix), closes #562.
This commit is contained in:
parent
216b2ab912
commit
bc04243a2b
|
@ -20,6 +20,7 @@
|
|||
package org.elasticsearch.search.fetch;
|
||||
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.index.mapper.FieldMapper;
|
||||
import org.elasticsearch.script.search.SearchScript;
|
||||
import org.elasticsearch.search.SearchParseElement;
|
||||
import org.elasticsearch.search.fetch.script.ScriptFieldsContext;
|
||||
|
@ -42,7 +43,14 @@ public class FieldsParseElement implements SearchParseElement {
|
|||
SearchScript searchScript = new SearchScript(context.lookup(), null, name, null, context.scriptService());
|
||||
context.scriptFields().add(new ScriptFieldsContext.ScriptField(name, searchScript));
|
||||
} else {
|
||||
context.fieldNames().add(name);
|
||||
FieldMapper fieldMapper = context.mapperService().smartNameFieldMapper(name);
|
||||
if (!"*".equals(name) && (fieldMapper == null || !fieldMapper.stored())) {
|
||||
// script field to load from source
|
||||
SearchScript searchScript = new SearchScript(context.lookup(), null, "_source." + name, null, context.scriptService());
|
||||
context.scriptFields().add(new ScriptFieldsContext.ScriptField(name, searchScript));
|
||||
} else {
|
||||
context.fieldNames().add(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!added) {
|
||||
|
@ -55,7 +63,14 @@ public class FieldsParseElement implements SearchParseElement {
|
|||
SearchScript searchScript = new SearchScript(context.lookup(), null, name, null, context.scriptService());
|
||||
context.scriptFields().add(new ScriptFieldsContext.ScriptField(name, searchScript));
|
||||
} else {
|
||||
context.fieldNames().add(name);
|
||||
FieldMapper fieldMapper = context.mapperService().smartNameFieldMapper(name);
|
||||
if (!"*".equals(name) && (fieldMapper == null || !fieldMapper.stored())) {
|
||||
// script field to load from source
|
||||
SearchScript searchScript = new SearchScript(context.lookup(), null, "_source." + name, null, context.scriptService());
|
||||
context.scriptFields().add(new ScriptFieldsContext.ScriptField(name, searchScript));
|
||||
} else {
|
||||
context.fieldNames().add(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -82,9 +82,12 @@ public class SearchFieldsTests extends AbstractNodesTests {
|
|||
assertThat(searchResponse.hits().getAt(0).fields().size(), equalTo(1));
|
||||
assertThat(searchResponse.hits().getAt(0).fields().get("field1").value().toString(), equalTo("value1"));
|
||||
|
||||
// field2 is not stored, check that it gets extracted from source
|
||||
searchResponse = client.prepareSearch().setQuery(matchAllQuery()).addField("field2").execute().actionGet();
|
||||
assertThat(searchResponse.hits().getTotalHits(), equalTo(1l));
|
||||
assertThat(searchResponse.hits().hits().length, equalTo(1));
|
||||
assertThat(searchResponse.hits().getAt(0).fields().size(), equalTo(1));
|
||||
assertThat(searchResponse.hits().getAt(0).fields().get("field2").value().toString(), equalTo("value2"));
|
||||
|
||||
searchResponse = client.prepareSearch().setQuery(matchAllQuery()).addField("field3").execute().actionGet();
|
||||
assertThat(searchResponse.hits().getTotalHits(), equalTo(1l));
|
||||
|
|
Loading…
Reference in New Issue