Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inner hits retrieval when stored fields are disabled (_none_) #33018

Merged
merged 6 commits into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,15 @@ private SearchHit createSearchHit(SearchContext context,
int subDocId,
Map<String, Set<String>> 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<String, DocumentField> 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();
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand All @@ -42,23 +50,53 @@ 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()
.prepareSearch("test")
.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()
Expand All @@ -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());

Expand All @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down