Search in sources :

Example 1 with OpenSearchToParentBlockJoinQuery

use of org.opensearch.index.search.OpenSearchToParentBlockJoinQuery in project OpenSearch by opensearch-project.

the class CustomFieldQuery method flatten.

@Override
protected 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 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 org.apache.lucene.queries.function.FunctionScoreQuery) {
        org.apache.lucene.queries.function.FunctionScoreQuery funcScoreQuery = (org.apache.lucene.queries.function.FunctionScoreQuery) sourceQuery;
        // flatten query with query boost
        flatten(funcScoreQuery.getWrappedQuery(), reader, flatQueries, boost);
    } 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 if (sourceQuery instanceof OpenSearchToParentBlockJoinQuery) {
        Query childQuery = ((OpenSearchToParentBlockJoinQuery) sourceQuery).getChildQuery();
        if (childQuery != null) {
            flatten(childQuery, 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) Query(org.apache.lucene.search.Query) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhrasePrefixQuery(org.opensearch.common.lucene.search.MultiPhrasePrefixQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) OpenSearchToParentBlockJoinQuery(org.opensearch.index.search.OpenSearchToParentBlockJoinQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) OpenSearchToParentBlockJoinQuery(org.opensearch.index.search.OpenSearchToParentBlockJoinQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MultiPhrasePrefixQuery(org.opensearch.common.lucene.search.MultiPhrasePrefixQuery)

Example 2 with OpenSearchToParentBlockJoinQuery

use of org.opensearch.index.search.OpenSearchToParentBlockJoinQuery in project OpenSearch by opensearch-project.

the class QueryAnalyzerTests method testToParentBlockJoinQuery.

public void testToParentBlockJoinQuery() {
    TermQuery termQuery = new TermQuery(new Term("field", "value"));
    QueryBitSetProducer queryBitSetProducer = new QueryBitSetProducer(new TermQuery(new Term("_type", "child")));
    OpenSearchToParentBlockJoinQuery query = new OpenSearchToParentBlockJoinQuery(termQuery, queryBitSetProducer, ScoreMode.None, "child");
    Result result = analyze(query, Version.CURRENT);
    assertFalse(result.verified);
    assertThat(result.minimumShouldMatch, equalTo(1));
    assertEquals(1, result.extractions.size());
    assertNull(result.extractions.toArray(new QueryExtraction[0])[0].range);
    assertEquals(new Term("field", "value"), result.extractions.toArray(new QueryExtraction[0])[0].term);
}
Also used : BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) QueryExtraction(org.opensearch.percolator.QueryAnalyzer.QueryExtraction) OpenSearchToParentBlockJoinQuery(org.opensearch.index.search.OpenSearchToParentBlockJoinQuery) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) Term(org.apache.lucene.index.Term) Result(org.opensearch.percolator.QueryAnalyzer.Result) QueryAnalyzer.selectBestResult(org.opensearch.percolator.QueryAnalyzer.selectBestResult)

Example 3 with OpenSearchToParentBlockJoinQuery

use of org.opensearch.index.search.OpenSearchToParentBlockJoinQuery in project OpenSearch by opensearch-project.

the class NestedQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    if (context.allowExpensiveQueries() == false) {
        throw new OpenSearchException("[joining] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false.");
    }
    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(context.indexVersionCreated()));
    } 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 OpenSearchToParentBlockJoinQuery(innerQuery, parentFilter, scoreMode, objectMapper == null ? null : objectMapper.fullPath());
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ParentChildrenBlockJoinQuery(org.apache.lucene.search.join.ParentChildrenBlockJoinQuery) OpenSearchToParentBlockJoinQuery(org.opensearch.index.search.OpenSearchToParentBlockJoinQuery) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) OpenSearchToParentBlockJoinQuery(org.opensearch.index.search.OpenSearchToParentBlockJoinQuery) OpenSearchException(org.opensearch.OpenSearchException) NestedHelper(org.opensearch.index.search.NestedHelper) ObjectMapper(org.opensearch.index.mapper.ObjectMapper)

Example 4 with OpenSearchToParentBlockJoinQuery

use of org.opensearch.index.search.OpenSearchToParentBlockJoinQuery in project OpenSearch by opensearch-project.

the class QueryPhaseTests method testMaxScoreQueryVisitor.

public void testMaxScoreQueryVisitor() {
    BitSetProducer producer = context -> new FixedBitSet(1);
    Query query = new OpenSearchToParentBlockJoinQuery(new MatchAllDocsQuery(), producer, ScoreMode.Avg, "nested");
    assertTrue(hasInfMaxScore(query));
    query = new OpenSearchToParentBlockJoinQuery(new MatchAllDocsQuery(), producer, ScoreMode.None, "nested");
    assertFalse(hasInfMaxScore(query));
    for (Occur occur : Occur.values()) {
        query = new BooleanQuery.Builder().add(new OpenSearchToParentBlockJoinQuery(new MatchAllDocsQuery(), producer, ScoreMode.Avg, "nested"), occur).build();
        if (occur == Occur.MUST) {
            assertTrue(hasInfMaxScore(query));
        } else {
            assertFalse(hasInfMaxScore(query));
        }
        query = new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new OpenSearchToParentBlockJoinQuery(new MatchAllDocsQuery(), producer, ScoreMode.Avg, "nested"), occur).build(), occur).build();
        if (occur == Occur.MUST) {
            assertTrue(hasInfMaxScore(query));
        } else {
            assertFalse(hasInfMaxScore(query));
        }
        query = new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new OpenSearchToParentBlockJoinQuery(new MatchAllDocsQuery(), producer, ScoreMode.Avg, "nested"), occur).build(), Occur.FILTER).build();
        assertFalse(hasInfMaxScore(query));
        query = new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new SpanTermQuery(new Term("field", "foo")), occur).add(new OpenSearchToParentBlockJoinQuery(new MatchAllDocsQuery(), producer, ScoreMode.Avg, "nested"), occur).build(), occur).build();
        if (occur == Occur.MUST) {
            assertTrue(hasInfMaxScore(query));
        } else {
            assertFalse(hasInfMaxScore(query));
        }
    }
}
Also used : Query(org.apache.lucene.search.Query) NoMergePolicy(org.apache.lucene.index.NoMergePolicy) LatLonDocValuesField(org.apache.lucene.document.LatLonDocValuesField) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ScoreDoc(org.apache.lucene.search.ScoreDoc) SearchContext(org.opensearch.search.internal.SearchContext) Term(org.apache.lucene.index.Term) ContextIndexSearcher(org.opensearch.search.internal.ContextIndexSearcher) FieldDoc(org.apache.lucene.search.FieldDoc) Document(org.apache.lucene.document.Document) IndexShardTestCase(org.opensearch.index.shard.IndexShardTestCase) MapperService(org.opensearch.index.mapper.MapperService) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) Directory(org.apache.lucene.store.Directory) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) SortField(org.apache.lucene.search.SortField) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) LatLonPoint(org.apache.lucene.document.LatLonPoint) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) Occur(org.apache.lucene.search.BooleanClause.Occur) Sort(org.apache.lucene.search.Sort) BytesRef(org.apache.lucene.util.BytesRef) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) DirectoryReader(org.apache.lucene.index.DirectoryReader) PrefixQuery(org.apache.lucene.search.PrefixQuery) Settings(org.opensearch.common.settings.Settings) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ScoreMode(org.apache.lucene.search.join.ScoreMode) MinDocQuery(org.apache.lucene.queries.MinDocQuery) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) IndexWriter(org.apache.lucene.index.IndexWriter) List(java.util.List) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Matchers.equalTo(org.hamcrest.Matchers.equalTo) QueryShardContext(org.opensearch.index.query.QueryShardContext) TaskCancelledException(org.opensearch.tasks.TaskCancelledException) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) TestSearchContext(org.opensearch.test.TestSearchContext) Matchers.anyOf(org.hamcrest.Matchers.anyOf) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) Mockito.mock(org.mockito.Mockito.mock) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) LeafCollector(org.apache.lucene.search.LeafCollector) LongPoint(org.apache.lucene.document.LongPoint) FilterLeafCollector(org.apache.lucene.search.FilterLeafCollector) StringField(org.apache.lucene.document.StringField) DocValueFormat(org.opensearch.search.DocValueFormat) FilterCollector(org.apache.lucene.search.FilterCollector) Weight(org.apache.lucene.search.Weight) SortAndFormats(org.opensearch.search.sort.SortAndFormats) FieldComparator(org.apache.lucene.search.FieldComparator) FixedBitSet(org.apache.lucene.util.FixedBitSet) Mockito.spy(org.mockito.Mockito.spy) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) ArrayList(java.util.ArrayList) SearchShardTask(org.opensearch.action.search.SearchShardTask) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) IndexShard(org.opensearch.index.shard.IndexShard) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Store(org.apache.lucene.document.Field.Store) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) TopDocs(org.apache.lucene.search.TopDocs) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) TopDocsCollectorContext.hasInfMaxScore(org.opensearch.search.query.TopDocsCollectorContext.hasInfMaxScore) IOException(java.io.IOException) Collector(org.apache.lucene.search.Collector) Mockito.when(org.mockito.Mockito.when) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) ScrollContext(org.opensearch.search.internal.ScrollContext) TotalHits(org.apache.lucene.search.TotalHits) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) DateFieldMapper(org.opensearch.index.mapper.DateFieldMapper) TextField(org.apache.lucene.document.TextField) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) ParsedQuery(org.opensearch.index.query.ParsedQuery) OpenSearchToParentBlockJoinQuery(org.opensearch.index.search.OpenSearchToParentBlockJoinQuery) Collections(java.util.Collections) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MinDocQuery(org.apache.lucene.queries.MinDocQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ParsedQuery(org.opensearch.index.query.ParsedQuery) OpenSearchToParentBlockJoinQuery(org.opensearch.index.search.OpenSearchToParentBlockJoinQuery) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) FixedBitSet(org.apache.lucene.util.FixedBitSet) OpenSearchToParentBlockJoinQuery(org.opensearch.index.search.OpenSearchToParentBlockJoinQuery) Occur(org.apache.lucene.search.BooleanClause.Occur) Term(org.apache.lucene.index.Term) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Aggregations

OpenSearchToParentBlockJoinQuery (org.opensearch.index.search.OpenSearchToParentBlockJoinQuery)4 Term (org.apache.lucene.index.Term)3 Query (org.apache.lucene.search.Query)3 TermQuery (org.apache.lucene.search.TermQuery)3 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)3 BlendedTermQuery (org.apache.lucene.queries.BlendedTermQuery)2 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 BitSetProducer (org.apache.lucene.search.join.BitSetProducer)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)1 Document (org.apache.lucene.document.Document)1 Store (org.apache.lucene.document.Field.Store)1 LatLonDocValuesField (org.apache.lucene.document.LatLonDocValuesField)1 LatLonPoint (org.apache.lucene.document.LatLonPoint)1 LongPoint (org.apache.lucene.document.LongPoint)1 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)1