From bc04243a2b60c8ee3309f232a24e4c8ba7368a65 Mon Sep 17 00:00:00 2001 From: kimchy Date: Sun, 12 Dec 2010 05:00:24 +0200 Subject: [PATCH] Search Fields: If a field is not stored, automatically extract it from _source (without the need for _source prefix), closes #562. --- .../search/fetch/FieldsParseElement.java | 19 +++++++++++++++++-- .../search/fields/SearchFieldsTests.java | 3 +++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/fetch/FieldsParseElement.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/fetch/FieldsParseElement.java index 9073ae89c77..e8c0cc94ada 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/fetch/FieldsParseElement.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/fetch/FieldsParseElement.java @@ -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); + } } } } diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/fields/SearchFieldsTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/fields/SearchFieldsTests.java index 3e28851cd34..28b1553ad9d 100644 --- a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/fields/SearchFieldsTests.java +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/fields/SearchFieldsTests.java @@ -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));