use of org.elasticsearch.search.fetch.subphase.InnerHitsContext in project elasticsearch by elastic.
the class InnerHitBuilder method buildChildInnerHits.
private void buildChildInnerHits(SearchContext parentSearchContext, InnerHitsContext.BaseInnerHits innerHits) throws IOException {
Map<String, InnerHitsContext.BaseInnerHits> childInnerHits = new HashMap<>();
for (Map.Entry<String, InnerHitBuilder> entry : this.childInnerHits.entrySet()) {
InnerHitsContext.BaseInnerHits childInnerHit = entry.getValue().build(parentSearchContext, new InnerHitsContext());
if (childInnerHit != null) {
childInnerHits.put(entry.getKey(), childInnerHit);
}
}
innerHits.setChildInnerHits(childInnerHits);
}
use of org.elasticsearch.search.fetch.subphase.InnerHitsContext in project elasticsearch by elastic.
the class InnerHitBuilder method build.
public InnerHitsContext.BaseInnerHits build(SearchContext parentSearchContext, InnerHitsContext innerHitsContext) throws IOException {
QueryShardContext queryShardContext = parentSearchContext.getQueryShardContext();
if (nestedPath != null) {
ObjectMapper nestedObjectMapper = queryShardContext.getObjectMapper(nestedPath);
if (nestedObjectMapper == null) {
if (ignoreUnmapped == false) {
throw new IllegalStateException("[" + query.getName() + "] no mapping found for type [" + nestedPath + "]");
} else {
return null;
}
}
ObjectMapper parentObjectMapper = queryShardContext.nestedScope().nextLevel(nestedObjectMapper);
InnerHitsContext.NestedInnerHits nestedInnerHits = new InnerHitsContext.NestedInnerHits(name, parentSearchContext, parentObjectMapper, nestedObjectMapper);
setupInnerHitsContext(queryShardContext, nestedInnerHits);
if (childInnerHits != null) {
buildChildInnerHits(parentSearchContext, nestedInnerHits);
}
queryShardContext.nestedScope().previousLevel();
innerHitsContext.addInnerHitDefinition(nestedInnerHits);
return nestedInnerHits;
} else if (parentChildType != null) {
DocumentMapper documentMapper = queryShardContext.documentMapper(parentChildType);
if (documentMapper == null) {
if (ignoreUnmapped == false) {
throw new IllegalStateException("[" + query.getName() + "] no mapping found for type [" + parentChildType + "]");
} else {
return null;
}
}
InnerHitsContext.ParentChildInnerHits parentChildInnerHits = new InnerHitsContext.ParentChildInnerHits(name, parentSearchContext, queryShardContext.getMapperService(), documentMapper);
setupInnerHitsContext(queryShardContext, parentChildInnerHits);
if (childInnerHits != null) {
buildChildInnerHits(parentSearchContext, parentChildInnerHits);
}
innerHitsContext.addInnerHitDefinition(parentChildInnerHits);
return parentChildInnerHits;
} else {
throw new IllegalStateException("Neither a nested or parent/child inner hit");
}
}
use of org.elasticsearch.search.fetch.subphase.InnerHitsContext in project elasticsearch by elastic.
the class InnerHitBuilderTests method testBuild_ignoreUnmappedHasChildQuery.
public void testBuild_ignoreUnmappedHasChildQuery() throws Exception {
QueryShardContext queryShardContext = mock(QueryShardContext.class);
when(queryShardContext.documentMapper("type")).thenReturn(null);
SearchContext searchContext = mock(SearchContext.class);
when(searchContext.getQueryShardContext()).thenReturn(queryShardContext);
InnerHitBuilder leafInnerHits = randomInnerHits();
HasChildQueryBuilder query1 = new HasChildQueryBuilder("type", new MatchAllQueryBuilder(), ScoreMode.None).innerHit(leafInnerHits, false);
expectThrows(IllegalStateException.class, () -> query1.innerHit().build(searchContext, new InnerHitsContext()));
HasChildQueryBuilder query2 = new HasChildQueryBuilder("type", new MatchAllQueryBuilder(), ScoreMode.None).innerHit(leafInnerHits, true);
InnerHitsContext innerHitsContext = new InnerHitsContext();
query2.innerHit().build(searchContext, innerHitsContext);
assertThat(innerHitsContext.getInnerHits().size(), equalTo(0));
}
use of org.elasticsearch.search.fetch.subphase.InnerHitsContext in project elasticsearch by elastic.
the class InnerHitBuilderTests method testBuild_ingoreUnmappedNestQuery.
public void testBuild_ingoreUnmappedNestQuery() throws Exception {
QueryShardContext queryShardContext = mock(QueryShardContext.class);
when(queryShardContext.getObjectMapper("path")).thenReturn(null);
SearchContext searchContext = mock(SearchContext.class);
when(searchContext.getQueryShardContext()).thenReturn(queryShardContext);
InnerHitBuilder leafInnerHits = randomInnerHits();
NestedQueryBuilder query1 = new NestedQueryBuilder("path", new MatchAllQueryBuilder(), ScoreMode.None);
query1.innerHit(leafInnerHits, false);
expectThrows(IllegalStateException.class, () -> query1.innerHit().build(searchContext, new InnerHitsContext()));
NestedQueryBuilder query2 = new NestedQueryBuilder("path", new MatchAllQueryBuilder(), ScoreMode.None);
query2.innerHit(leafInnerHits, true);
InnerHitsContext innerHitsContext = new InnerHitsContext();
query2.innerHit().build(searchContext, innerHitsContext);
assertThat(innerHitsContext.getInnerHits().size(), equalTo(0));
}
use of org.elasticsearch.search.fetch.subphase.InnerHitsContext in project elasticsearch by elastic.
the class InnerHitBuilderTests method testBuild_ingoreUnmappedHasParentQuery.
public void testBuild_ingoreUnmappedHasParentQuery() throws Exception {
QueryShardContext queryShardContext = mock(QueryShardContext.class);
when(queryShardContext.documentMapper("type")).thenReturn(null);
SearchContext searchContext = mock(SearchContext.class);
when(searchContext.getQueryShardContext()).thenReturn(queryShardContext);
InnerHitBuilder leafInnerHits = randomInnerHits();
HasParentQueryBuilder query1 = new HasParentQueryBuilder("type", new MatchAllQueryBuilder(), false).innerHit(leafInnerHits, false);
expectThrows(IllegalStateException.class, () -> query1.innerHit().build(searchContext, new InnerHitsContext()));
HasParentQueryBuilder query2 = new HasParentQueryBuilder("type", new MatchAllQueryBuilder(), false).innerHit(leafInnerHits, true);
InnerHitsContext innerHitsContext = new InnerHitsContext();
query2.innerHit().build(searchContext, innerHitsContext);
assertThat(innerHitsContext.getInnerHits().size(), equalTo(0));
}
Aggregations