Skip to content

Commit

Permalink
Search Fields: If a field is not stored, automatically extract it fro…
Browse files Browse the repository at this point in the history
…m _source (without the need for _source prefix), closes elastic#562.
  • Loading branch information
kimchy committed Dec 12, 2010
1 parent 216b2ab commit bc04243
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ protected Client getClient() {
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));
Expand Down

0 comments on commit bc04243

Please sign in to comment.