Search in sources :

Example 1 with SpanTermQuery

use of org.apache.lucene.queries.spans.SpanTermQuery in project OpenSearch by opensearch-project.

the class CustomUnifiedHighlighter method rewriteCustomQuery.

/**
 * Translate custom queries in queries that are supported by the unified highlighter.
 */
private Collection<Query> rewriteCustomQuery(Query query) {
    if (query instanceof MultiPhrasePrefixQuery) {
        MultiPhrasePrefixQuery mpq = (MultiPhrasePrefixQuery) query;
        Term[][] terms = mpq.getTerms();
        int[] positions = mpq.getPositions();
        SpanQuery[] positionSpanQueries = new SpanQuery[positions.length];
        int sizeMinus1 = terms.length - 1;
        for (int i = 0; i < positions.length; i++) {
            SpanQuery[] innerQueries = new SpanQuery[terms[i].length];
            for (int j = 0; j < terms[i].length; j++) {
                if (i == sizeMinus1) {
                    innerQueries[j] = new SpanMultiTermQueryWrapper<>(new PrefixQuery(terms[i][j]));
                } else {
                    innerQueries[j] = new SpanTermQuery(terms[i][j]);
                }
            }
            if (innerQueries.length > 1) {
                positionSpanQueries[i] = new SpanOrQuery(innerQueries);
            } else {
                positionSpanQueries[i] = innerQueries[0];
            }
        }
        if (positionSpanQueries.length == 1) {
            return Collections.singletonList(positionSpanQueries[0]);
        }
        // sum position increments beyond 1
        int positionGaps = 0;
        if (positions.length >= 2) {
            // positions are in increasing order. max(0,...) is just a safeguard.
            positionGaps = Math.max(0, positions[positions.length - 1] - positions[0] - positions.length + 1);
        }
        // if original slop is 0 then require inOrder
        boolean inorder = (mpq.getSlop() == 0);
        return Collections.singletonList(new SpanNearQuery(positionSpanQueries, mpq.getSlop() + positionGaps, inorder));
    } else {
        return null;
    }
}
Also used : PrefixQuery(org.apache.lucene.search.PrefixQuery) MultiPhrasePrefixQuery(org.opensearch.common.lucene.search.MultiPhrasePrefixQuery) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) MultiPhrasePrefixQuery(org.opensearch.common.lucene.search.MultiPhrasePrefixQuery) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery)

Example 2 with SpanTermQuery

use of org.apache.lucene.queries.spans.SpanTermQuery 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.queries.spans.SpanTermQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) 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.queries.spans.SpanTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MultiPhrasePrefixQuery(org.opensearch.common.lucene.search.MultiPhrasePrefixQuery)

Example 3 with SpanTermQuery

use of org.apache.lucene.queries.spans.SpanTermQuery in project OpenSearch by opensearch-project.

the class QueryAnalyzerTests method testExtractQueryMetadata_spanOrQuery.

public void testExtractQueryMetadata_spanOrQuery() {
    SpanTermQuery spanTermQuery1 = new SpanTermQuery(new Term("_field", "_short_term"));
    SpanTermQuery spanTermQuery2 = new SpanTermQuery(new Term("_field", "_very_long_term"));
    SpanOrQuery spanOrQuery = new SpanOrQuery(spanTermQuery1, spanTermQuery2);
    Result result = analyze(spanOrQuery, Version.CURRENT);
    assertThat(result.verified, is(true));
    assertThat(result.minimumShouldMatch, equalTo(1));
    assertTermsEqual(result.extractions, spanTermQuery1.getTerm(), spanTermQuery2.getTerm());
}
Also used : SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) Result(org.opensearch.percolator.QueryAnalyzer.Result) QueryAnalyzer.selectBestResult(org.opensearch.percolator.QueryAnalyzer.selectBestResult)

Example 4 with SpanTermQuery

use of org.apache.lucene.queries.spans.SpanTermQuery in project OpenSearch by opensearch-project.

the class QueryAnalyzerTests method testExtractQueryMetadata_spanNotQuery.

public void testExtractQueryMetadata_spanNotQuery() {
    SpanTermQuery spanTermQuery1 = new SpanTermQuery(new Term("_field", "_short_term"));
    SpanTermQuery spanTermQuery2 = new SpanTermQuery(new Term("_field", "_very_long_term"));
    SpanNotQuery spanNotQuery = new SpanNotQuery(spanTermQuery1, spanTermQuery2);
    Result result = analyze(spanNotQuery, Version.CURRENT);
    assertThat(result.verified, is(false));
    assertThat(result.minimumShouldMatch, equalTo(1));
    assertTermsEqual(result.extractions, spanTermQuery1.getTerm());
}
Also used : SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) SpanNotQuery(org.apache.lucene.queries.spans.SpanNotQuery) Result(org.opensearch.percolator.QueryAnalyzer.Result) QueryAnalyzer.selectBestResult(org.opensearch.percolator.QueryAnalyzer.selectBestResult)

Example 5 with SpanTermQuery

use of org.apache.lucene.queries.spans.SpanTermQuery in project OpenSearch by opensearch-project.

the class SpanTermQueryBuilder method doToQuery.

@Override
protected SpanQuery doToQuery(QueryShardContext context) throws IOException {
    MappedFieldType mapper = context.fieldMapper(fieldName);
    Term term;
    if (mapper == null) {
        term = new Term(fieldName, BytesRefs.toBytesRef(value));
    } else {
        Query termQuery = mapper.termQuery(value, context);
        term = MappedFieldType.extractTerm(termQuery);
    }
    return new SpanTermQuery(term);
}
Also used : Query(org.apache.lucene.search.Query) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term)

Aggregations

SpanTermQuery (org.apache.lucene.queries.spans.SpanTermQuery)27 Term (org.apache.lucene.index.Term)23 SpanNearQuery (org.apache.lucene.queries.spans.SpanNearQuery)16 Query (org.apache.lucene.search.Query)16 TermQuery (org.apache.lucene.search.TermQuery)14 SpanOrQuery (org.apache.lucene.queries.spans.SpanOrQuery)11 BooleanQuery (org.apache.lucene.search.BooleanQuery)11 SpanQuery (org.apache.lucene.queries.spans.SpanQuery)10 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)8 PhraseQuery (org.apache.lucene.search.PhraseQuery)8 PrefixQuery (org.apache.lucene.search.PrefixQuery)8 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)7 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)7 SynonymQuery (org.apache.lucene.search.SynonymQuery)6 ArrayList (java.util.ArrayList)5 Document (org.apache.lucene.document.Document)5 IndexReader (org.apache.lucene.index.IndexReader)5 BooleanClause (org.apache.lucene.search.BooleanClause)5 BoostQuery (org.apache.lucene.search.BoostQuery)5 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)5