Search in sources :

Example 1 with SpanQuery

use of org.apache.lucene.queries.spans.SpanQuery 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 SpanQuery

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

the class SpanContainingQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    Query innerBig = big.toQuery(context);
    assert innerBig instanceof SpanQuery;
    Query innerLittle = little.toQuery(context);
    assert innerLittle instanceof SpanQuery;
    return new SpanContainingQuery((SpanQuery) innerBig, (SpanQuery) innerLittle);
}
Also used : Query(org.apache.lucene.search.Query) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) SpanContainingQuery(org.apache.lucene.queries.spans.SpanContainingQuery) SpanContainingQuery(org.apache.lucene.queries.spans.SpanContainingQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery)

Example 3 with SpanQuery

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

the class SpanNearQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    SpanQueryBuilder queryBuilder = clauses.get(0);
    boolean isGap = queryBuilder instanceof SpanGapQueryBuilder;
    Query query = null;
    if (!isGap) {
        query = queryBuilder.toQuery(context);
        assert query instanceof SpanQuery;
    }
    if (clauses.size() == 1) {
        assert !isGap;
        return query;
    }
    String spanNearFieldName = null;
    if (isGap) {
        String fieldName = ((SpanGapQueryBuilder) queryBuilder).fieldName();
        spanNearFieldName = queryFieldName(context, fieldName);
    } else {
        spanNearFieldName = ((SpanQuery) query).getField();
    }
    SpanNearQuery.Builder builder = new SpanNearQuery.Builder(spanNearFieldName, inOrder);
    builder.setSlop(slop);
    /*
         * Lucene SpanNearQuery throws exceptions for certain use cases like adding gap to a
         * unordered SpanNearQuery. Should OpenSearch have the same checks or wrap those thrown exceptions?
         */
    if (isGap) {
        int gap = ((SpanGapQueryBuilder) queryBuilder).width();
        builder.addGap(gap);
    } else {
        builder.addClause((SpanQuery) query);
    }
    for (int i = 1; i < clauses.size(); i++) {
        queryBuilder = clauses.get(i);
        isGap = queryBuilder instanceof SpanGapQueryBuilder;
        if (isGap) {
            String fieldName = ((SpanGapQueryBuilder) queryBuilder).fieldName();
            String spanGapFieldName = queryFieldName(context, fieldName);
            if (!spanNearFieldName.equals(spanGapFieldName)) {
                throw new IllegalArgumentException("[span_near] clauses must have same field");
            }
            int gap = ((SpanGapQueryBuilder) queryBuilder).width();
            builder.addGap(gap);
        } else {
            query = clauses.get(i).toQuery(context);
            assert query instanceof SpanQuery;
            builder.addClause((SpanQuery) query);
        }
    }
    return builder.build();
}
Also used : Query(org.apache.lucene.search.Query) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery)

Example 4 with SpanQuery

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

the class SpanNotQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    Query includeQuery = this.include.toQuery(context);
    assert includeQuery instanceof SpanQuery;
    Query excludeQuery = this.exclude.toQuery(context);
    assert excludeQuery instanceof SpanQuery;
    return new SpanNotQuery((SpanQuery) includeQuery, (SpanQuery) excludeQuery, pre, post);
}
Also used : Query(org.apache.lucene.search.Query) SpanNotQuery(org.apache.lucene.queries.spans.SpanNotQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) SpanNotQuery(org.apache.lucene.queries.spans.SpanNotQuery)

Example 5 with SpanQuery

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

the class SpanOrQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    SpanQuery[] spanQueries = new SpanQuery[clauses.size()];
    for (int i = 0; i < clauses.size(); i++) {
        Query query = clauses.get(i).toQuery(context);
        assert query instanceof SpanQuery;
        spanQueries[i] = (SpanQuery) query;
    }
    return new SpanOrQuery(spanQueries);
}
Also used : Query(org.apache.lucene.search.Query) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery)

Aggregations

SpanQuery (org.apache.lucene.queries.spans.SpanQuery)16 Query (org.apache.lucene.search.Query)10 SpanTermQuery (org.apache.lucene.queries.spans.SpanTermQuery)9 SpanNearQuery (org.apache.lucene.queries.spans.SpanNearQuery)8 SpanOrQuery (org.apache.lucene.queries.spans.SpanOrQuery)8 Term (org.apache.lucene.index.Term)6 SpanMatchNoDocsQuery (org.apache.lucene.queries.SpanMatchNoDocsQuery)3 Analyzer (org.apache.lucene.analysis.Analyzer)2 IndexReader (org.apache.lucene.index.IndexReader)2 FieldMaskingSpanQuery (org.apache.lucene.queries.spans.FieldMaskingSpanQuery)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)2 PhraseQuery (org.apache.lucene.search.PhraseQuery)2 PrefixQuery (org.apache.lucene.search.PrefixQuery)2 SynonymQuery (org.apache.lucene.search.SynonymQuery)2 TermQuery (org.apache.lucene.search.TermQuery)2 MockSynonymAnalyzer (org.apache.lucene.tests.analysis.MockSynonymAnalyzer)2 BytesRef (org.apache.lucene.util.BytesRef)2