From 8c5fdf4aeec0d1cbfdca94571276f480ff9b022e Mon Sep 17 00:00:00 2001 From: kimchy Date: Wed, 4 Aug 2010 13:22:55 +0300 Subject: [PATCH] Ability to return all stored fields with a *, closes #296. --- .../mapper/FieldMappersFieldSelector.java | 2 +- .../search/fetch/FetchPhase.java | 5 + .../search/fields/SearchFieldsTests.java | 101 ++++++++++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/fields/SearchFieldsTests.java diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FieldMappersFieldSelector.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FieldMappersFieldSelector.java index d37e9c2e7eb..daafda0bb62 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FieldMappersFieldSelector.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FieldMappersFieldSelector.java @@ -25,7 +25,7 @@ import org.apache.lucene.document.FieldSelectorResult; import java.util.HashSet; /** - * @author kimchy (Shay Banon) + * @author kimchy (shay.banon) */ public class FieldMappersFieldSelector implements FieldSelector { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java index ef968cc4be5..399d5942613 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java @@ -205,6 +205,11 @@ public class FetchPhase implements SearchPhase { return new UidFieldSelector(); } + // asked for all stored fields, just return null so all of them will be loaded + if (context.fieldNames().get(0).equals("*")) { + return null; + } + FieldMappersFieldSelector fieldSelector = new FieldMappersFieldSelector(); for (String fieldName : context.fieldNames()) { FieldMappers x = context.mapperService().smartNameFieldMappers(fieldName); 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 new file mode 100644 index 00000000000..6d4628f10d6 --- /dev/null +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/fields/SearchFieldsTests.java @@ -0,0 +1,101 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.test.integration.search.fields; + +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.client.Client; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.test.integration.AbstractNodesTests; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import static org.elasticsearch.common.xcontent.XContentFactory.*; +import static org.elasticsearch.index.query.xcontent.QueryBuilders.*; +import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.*; + +/** + * @author kimchy (shay.banon) + */ +public class SearchFieldsTests extends AbstractNodesTests { + + private Client client; + + @BeforeClass public void createNodes() throws Exception { + startNode("server1"); + startNode("server2"); + client = getClient(); + } + + @AfterClass public void closeNodes() { + client.close(); + closeAllNodes(); + } + + protected Client getClient() { + return client("server1"); + } + + @Test public void testStoredFields() throws Exception { + client.admin().indices().prepareCreate("test").execute().actionGet(); + client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet(); + + String mapping = XContentFactory.contentTextBuilder(XContentType.JSON).startObject().startObject("type").startObject("properties") + .startObject("field1").field("type", "string").field("store", "yes").endObject() + .startObject("field2").field("type", "string").field("store", "no").endObject() + .startObject("field3").field("type", "string").field("store", "yes").endObject() + .endObject().endObject().endObject().string(); + + client.admin().indices().preparePutMapping().setType("type1").setSource(mapping).execute().actionGet(); + + client.prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject() + .field("field1", "value1") + .field("field2", "value2") + .field("field3", "value3") + .endObject()).execute().actionGet(); + + client.admin().indices().prepareRefresh().execute().actionGet(); + + SearchResponse searchResponse = client.prepareSearch().setQuery(matchAllQuery()).addField("field1").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("field1").value().toString(), equalTo("value1")); + + searchResponse = client.prepareSearch().setQuery(matchAllQuery()).addField("field2").execute().actionGet(); + assertThat(searchResponse.hits().getTotalHits(), equalTo(1l)); + assertThat(searchResponse.hits().hits().length, equalTo(1)); + + searchResponse = client.prepareSearch().setQuery(matchAllQuery()).addField("field3").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("field3").value().toString(), equalTo("value3")); + + searchResponse = client.prepareSearch().setQuery(matchAllQuery()).addField("*").execute().actionGet(); + assertThat(searchResponse.hits().getTotalHits(), equalTo(1l)); + assertThat(searchResponse.hits().hits().length, equalTo(1)); + assertThat(searchResponse.hits().getAt(0).fields().size(), equalTo(2)); + assertThat(searchResponse.hits().getAt(0).fields().get("field1").value().toString(), equalTo("value1")); + assertThat(searchResponse.hits().getAt(0).fields().get("field3").value().toString(), equalTo("value3")); + } +}