diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search/100_stored_fields.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search/100_stored_fields.yml index f39b4dbd3f5f8..92910a4f1f935 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/search/100_stored_fields.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search/100_stored_fields.yml @@ -39,6 +39,5 @@ setup: stored_fields: "_none_" - is_false: hits.hits.0._id - - is_false: hits.hits.0._type - is_false: hits.hits.0._source diff --git a/server/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java b/server/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java index 64ed5f4479514..f745ee1163c16 100644 --- a/server/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java +++ b/server/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java @@ -192,20 +192,15 @@ private SearchHit createSearchHit(SearchContext context, int subDocId, Map> storedToRequestedFields, LeafReaderContext subReaderContext) { + DocumentMapper documentMapper = context.mapperService().documentMapper(); + Text typeText = documentMapper.typeText(); if (fieldsVisitor == null) { - return new SearchHit(docId); + return new SearchHit(docId, null, typeText, null); } Map searchFields = getSearchFields(context, fieldsVisitor, subDocId, storedToRequestedFields, subReaderContext); - DocumentMapper documentMapper = context.mapperService().documentMapper(fieldsVisitor.uid().type()); - Text typeText; - if (documentMapper == null) { - typeText = new Text(fieldsVisitor.uid().type()); - } else { - typeText = documentMapper.typeText(); - } SearchHit searchHit = new SearchHit(docId, fieldsVisitor.uid().id(), typeText, searchFields); // Set _source if requested. SourceLookup sourceLookup = context.lookup().source(); @@ -275,7 +270,7 @@ private SearchHit createNestedSearchHit(SearchContext context, storedToRequestedFields, subReaderContext); } - DocumentMapper documentMapper = context.mapperService().documentMapper(uid.type()); + DocumentMapper documentMapper = context.mapperService().documentMapper(); SourceLookup sourceLookup = context.lookup().source(); sourceLookup.setSegmentAndDocument(subReaderContext, nestedSubDocId); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsIT.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsIT.java index 952eb22848e1a..8c0e64762e2be 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsIT.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsIT.java @@ -1052,7 +1052,7 @@ public void testNoStoredFields() throws Exception { for (SearchHit hit : hits) { assertThat(hit.getSourceAsMap(), nullValue()); assertThat(hit.getId(), nullValue()); - assertThat(hit.getType(), nullValue()); + assertThat(hit.getType(), equalTo("type")); } } } diff --git a/server/src/test/java/org/elasticsearch/search/source/MetadataFetchingIT.java b/server/src/test/java/org/elasticsearch/search/source/MetadataFetchingIT.java index 460fd11fbd9f1..c64ae84092393 100644 --- a/server/src/test/java/org/elasticsearch/search/source/MetadataFetchingIT.java +++ b/server/src/test/java/org/elasticsearch/search/source/MetadataFetchingIT.java @@ -18,12 +18,20 @@ */ package org.elasticsearch.search.source; +import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.action.search.SearchPhaseExecutionException; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.index.query.InnerHitBuilder; +import org.elasticsearch.index.query.NestedQueryBuilder; +import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.search.SearchContextException; +import org.elasticsearch.search.SearchHits; +import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.test.ESIntegTestCase; +import java.util.Collections; + import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.nullValue; @@ -33,7 +41,7 @@ public void testSimple() { assertAcked(prepareCreate("test")); ensureGreen(); - client().prepareIndex("test", "type1", "1").setSource("field", "value").execute().actionGet(); + client().prepareIndex("test", "_doc", "1").setSource("field", "value").execute().actionGet(); refresh(); SearchResponse response = client() @@ -42,7 +50,7 @@ public void testSimple() { .setFetchSource(false) .get(); assertThat(response.getHits().getAt(0).getId(), nullValue()); - assertThat(response.getHits().getAt(0).getType(), nullValue()); + assertThat(response.getHits().getAt(0).getType(), equalTo("_doc")); assertThat(response.getHits().getAt(0).getSourceAsString(), nullValue()); response = client() @@ -50,15 +58,45 @@ public void testSimple() { .storedFields("_none_") .get(); assertThat(response.getHits().getAt(0).getId(), nullValue()); - assertThat(response.getHits().getAt(0).getType(), nullValue()); + assertThat(response.getHits().getAt(0).getType(), equalTo("_doc")); + assertThat(response.getHits().getAt(0).getSourceAsString(), nullValue()); + } + + public void testInnerHits() { + assertAcked(prepareCreate("test").addMapping("_doc", "nested", "type=nested")); + ensureGreen(); + client().prepareIndex("test", "_doc", "1") + .setSource("field", "value", "nested", Collections.singletonMap("title", "foo")).execute().actionGet(); + refresh(); + + SearchResponse response = client() + .prepareSearch("test") + .storedFields("_none_") + .setFetchSource(false) + .setQuery( + new NestedQueryBuilder("nested", new TermQueryBuilder("nested.title", "foo"), ScoreMode.Total) + .innerHit(new InnerHitBuilder() + .setStoredFieldNames(Collections.singletonList("_none_")) + .setFetchSourceContext(new FetchSourceContext(false))) + ) + .get(); + assertThat(response.getHits().totalHits, equalTo(1L)); + assertThat(response.getHits().getAt(0).getId(), nullValue()); + assertThat(response.getHits().getAt(0).getType(), equalTo("_doc")); assertThat(response.getHits().getAt(0).getSourceAsString(), nullValue()); + assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1)); + SearchHits hits = response.getHits().getAt(0).getInnerHits().get("nested"); + assertThat(hits.totalHits, equalTo(1L)); + assertThat(hits.getAt(0).getId(), nullValue()); + assertThat(hits.getAt(0).getType(), equalTo("_doc")); + assertThat(hits.getAt(0).getSourceAsString(), nullValue()); } public void testWithRouting() { assertAcked(prepareCreate("test")); ensureGreen(); - client().prepareIndex("test", "type1", "1").setSource("field", "value").setRouting("toto").execute().actionGet(); + client().prepareIndex("test", "_doc", "1").setSource("field", "value").setRouting("toto").execute().actionGet(); refresh(); SearchResponse response = client() @@ -67,7 +105,7 @@ public void testWithRouting() { .setFetchSource(false) .get(); assertThat(response.getHits().getAt(0).getId(), nullValue()); - assertThat(response.getHits().getAt(0).getType(), nullValue()); + assertThat(response.getHits().getAt(0).getType(), equalTo("_doc")); assertThat(response.getHits().getAt(0).field("_routing"), nullValue()); assertThat(response.getHits().getAt(0).getSourceAsString(), nullValue()); @@ -76,7 +114,7 @@ public void testWithRouting() { .storedFields("_none_") .get(); assertThat(response.getHits().getAt(0).getId(), nullValue()); - assertThat(response.getHits().getAt(0).getType(), nullValue()); + assertThat(response.getHits().getAt(0).getType(), equalTo("_doc")); assertThat(response.getHits().getAt(0).getSourceAsString(), nullValue()); } diff --git a/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java b/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java index 3002711bdbd8e..22c5772ff2d53 100644 --- a/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java @@ -37,6 +37,7 @@ import org.elasticsearch.common.lease.Releasables; import org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.text.Text; import org.elasticsearch.common.util.MockBigArrays; import org.elasticsearch.common.util.MockPageCacheRecycler; import org.elasticsearch.index.Index; @@ -137,6 +138,7 @@ protected AggregatorFactory createAggregatorFactory(Query query, when(mapperService.getIndexSettings()).thenReturn(indexSettings); when(mapperService.hasNested()).thenReturn(false); DocumentMapper mapper = mock(DocumentMapper.class); + when(mapper.typeText()).thenReturn(new Text(TYPE_NAME)); when(mapper.type()).thenReturn(TYPE_NAME); when(mapperService.documentMapper()).thenReturn(mapper); when(searchContext.mapperService()).thenReturn(mapperService);