Search in sources :

Example 1 with ESToParentBlockJoinQuery

use of org.elasticsearch.index.search.ESToParentBlockJoinQuery in project elasticsearch by elastic.

the class NestedQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    ObjectMapper nestedObjectMapper = context.getObjectMapper(path);
    if (nestedObjectMapper == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new IllegalStateException("[" + NAME + "] failed to find nested object under path [" + path + "]");
        }
    }
    if (!nestedObjectMapper.nested().isNested()) {
        throw new IllegalStateException("[" + NAME + "] nested object under path [" + path + "] is not of nested type");
    }
    final BitSetProducer parentFilter;
    Query innerQuery;
    ObjectMapper objectMapper = context.nestedScope().getObjectMapper();
    if (objectMapper == null) {
        parentFilter = context.bitsetFilter(Queries.newNonNestedFilter());
    } else {
        parentFilter = context.bitsetFilter(objectMapper.nestedTypeFilter());
    }
    try {
        context.nestedScope().nextLevel(nestedObjectMapper);
        innerQuery = this.query.toQuery(context);
    } finally {
        context.nestedScope().previousLevel();
    }
    // in its child space
    if (new NestedHelper(context.getMapperService()).mightMatchNonNestedDocs(innerQuery, path)) {
        innerQuery = Queries.filtered(innerQuery, nestedObjectMapper.nestedTypeFilter());
    }
    return new ESToParentBlockJoinQuery(innerQuery, parentFilter, scoreMode, objectMapper == null ? null : objectMapper.fullPath());
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) ESToParentBlockJoinQuery(org.elasticsearch.index.search.ESToParentBlockJoinQuery) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ESToParentBlockJoinQuery(org.elasticsearch.index.search.ESToParentBlockJoinQuery) NestedHelper(org.elasticsearch.index.search.NestedHelper) ObjectMapper(org.elasticsearch.index.mapper.ObjectMapper)

Example 2 with ESToParentBlockJoinQuery

use of org.elasticsearch.index.search.ESToParentBlockJoinQuery in project elasticsearch by elastic.

the class CustomFieldQuery method flatten.

@Override
void flatten(Query sourceQuery, IndexReader reader, Collection<Query> flatQueries, float boost) throws IOException {
    if (sourceQuery instanceof BoostQuery) {
        BoostQuery bq = (BoostQuery) sourceQuery;
        sourceQuery = bq.getQuery();
        boost *= bq.getBoost();
        flatten(sourceQuery, reader, flatQueries, boost);
    } else if (sourceQuery instanceof SpanTermQuery) {
        super.flatten(new TermQuery(((SpanTermQuery) sourceQuery).getTerm()), reader, flatQueries, boost);
    } else if (sourceQuery instanceof ConstantScoreQuery) {
        flatten(((ConstantScoreQuery) sourceQuery).getQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof FunctionScoreQuery) {
        flatten(((FunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof MultiPhrasePrefixQuery) {
        flatten(sourceQuery.rewrite(reader), reader, flatQueries, boost);
    } else if (sourceQuery instanceof FiltersFunctionScoreQuery) {
        flatten(((FiltersFunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof MultiPhraseQuery) {
        MultiPhraseQuery q = ((MultiPhraseQuery) sourceQuery);
        convertMultiPhraseQuery(0, new int[q.getTermArrays().length], q, q.getTermArrays(), q.getPositions(), reader, flatQueries);
    } else if (sourceQuery instanceof BlendedTermQuery) {
        final BlendedTermQuery blendedTermQuery = (BlendedTermQuery) sourceQuery;
        flatten(blendedTermQuery.rewrite(reader), reader, flatQueries, boost);
    } else if (sourceQuery instanceof ESToParentBlockJoinQuery) {
        ESToParentBlockJoinQuery blockJoinQuery = (ESToParentBlockJoinQuery) sourceQuery;
        flatten(blockJoinQuery.getChildQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof BoostingQuery) {
        BoostingQuery boostingQuery = (BoostingQuery) sourceQuery;
        //flatten positive query with query boost
        flatten(boostingQuery.getMatch(), reader, flatQueries, boost);
        //flatten negative query with negative boost
        flatten(boostingQuery.getContext(), reader, flatQueries, boostingQuery.getBoost());
    } else if (sourceQuery instanceof SynonymQuery) {
        // SynonymQuery should be handled by the parent class directly.
        // This statement should be removed when https://issues.apache.org/jira/browse/LUCENE-7484 is merged.
        SynonymQuery synQuery = (SynonymQuery) sourceQuery;
        for (Term term : synQuery.getTerms()) {
            flatten(new TermQuery(term), reader, flatQueries, boost);
        }
    } else {
        super.flatten(sourceQuery, reader, flatQueries, boost);
    }
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) ESToParentBlockJoinQuery(org.elasticsearch.index.search.ESToParentBlockJoinQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BoostingQuery(org.apache.lucene.queries.BoostingQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery)

Example 3 with ESToParentBlockJoinQuery

use of org.elasticsearch.index.search.ESToParentBlockJoinQuery in project elasticsearch by elastic.

the class NestedQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(NestedQueryBuilder queryBuilder, Query query, SearchContext searchContext) throws IOException {
    QueryBuilder innerQueryBuilder = queryBuilder.query();
    assertThat(query, instanceOf(ESToParentBlockJoinQuery.class));
    ESToParentBlockJoinQuery parentBlockJoinQuery = (ESToParentBlockJoinQuery) query;
    // TODO how to assert this?
    if (queryBuilder.innerHit() != null) {
        // have to rewrite again because the provided queryBuilder hasn't been rewritten (directly returned from
        // doCreateTestQueryBuilder)
        queryBuilder = (NestedQueryBuilder) queryBuilder.rewrite(searchContext.getQueryShardContext());
        assertNotNull(searchContext);
        Map<String, InnerHitBuilder> innerHitBuilders = new HashMap<>();
        InnerHitBuilder.extractInnerHits(queryBuilder, innerHitBuilders);
        for (InnerHitBuilder builder : innerHitBuilders.values()) {
            builder.build(searchContext, searchContext.innerHits());
        }
        assertNotNull(searchContext.innerHits());
        assertEquals(1, searchContext.innerHits().getInnerHits().size());
        assertTrue(searchContext.innerHits().getInnerHits().containsKey(queryBuilder.innerHit().getName()));
        InnerHitsContext.BaseInnerHits innerHits = searchContext.innerHits().getInnerHits().get(queryBuilder.innerHit().getName());
        assertEquals(innerHits.size(), queryBuilder.innerHit().getSize());
        assertEquals(innerHits.sort().sort.getSort().length, 1);
        assertEquals(innerHits.sort().sort.getSort()[0].getField(), INT_FIELD_NAME);
    }
}
Also used : HashMap(java.util.HashMap) ESToParentBlockJoinQuery(org.elasticsearch.index.search.ESToParentBlockJoinQuery) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) InnerHitsContext(org.elasticsearch.search.fetch.subphase.InnerHitsContext)

Aggregations

ESToParentBlockJoinQuery (org.elasticsearch.index.search.ESToParentBlockJoinQuery)3 HashMap (java.util.HashMap)1 Term (org.apache.lucene.index.Term)1 BlendedTermQuery (org.apache.lucene.queries.BlendedTermQuery)1 BoostingQuery (org.apache.lucene.queries.BoostingQuery)1 BoostQuery (org.apache.lucene.search.BoostQuery)1 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)1 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)1 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)1 Query (org.apache.lucene.search.Query)1 SynonymQuery (org.apache.lucene.search.SynonymQuery)1 TermQuery (org.apache.lucene.search.TermQuery)1 BitSetProducer (org.apache.lucene.search.join.BitSetProducer)1 ToParentBlockJoinQuery (org.apache.lucene.search.join.ToParentBlockJoinQuery)1 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)1 MultiPhrasePrefixQuery (org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery)1 FiltersFunctionScoreQuery (org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery)1 FunctionScoreQuery (org.elasticsearch.common.lucene.search.function.FunctionScoreQuery)1 ObjectMapper (org.elasticsearch.index.mapper.ObjectMapper)1 NestedHelper (org.elasticsearch.index.search.NestedHelper)1