Search in sources :

Example 1 with InnerHitsContext

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);
}
Also used : HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) InnerHitsContext(org.elasticsearch.search.fetch.subphase.InnerHitsContext)

Example 2 with InnerHitsContext

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");
    }
}
Also used : DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) InnerHitsContext(org.elasticsearch.search.fetch.subphase.InnerHitsContext) ObjectMapper(org.elasticsearch.index.mapper.ObjectMapper)

Example 3 with InnerHitsContext

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));
}
Also used : SearchContext(org.elasticsearch.search.internal.SearchContext) InnerHitsContext(org.elasticsearch.search.fetch.subphase.InnerHitsContext)

Example 4 with InnerHitsContext

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));
}
Also used : SearchContext(org.elasticsearch.search.internal.SearchContext) InnerHitsContext(org.elasticsearch.search.fetch.subphase.InnerHitsContext)

Example 5 with InnerHitsContext

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));
}
Also used : SearchContext(org.elasticsearch.search.internal.SearchContext) InnerHitsContext(org.elasticsearch.search.fetch.subphase.InnerHitsContext)

Aggregations

InnerHitsContext (org.elasticsearch.search.fetch.subphase.InnerHitsContext)5 SearchContext (org.elasticsearch.search.internal.SearchContext)3 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)1 ObjectMapper (org.elasticsearch.index.mapper.ObjectMapper)1