Search in sources :

Example 1 with SpanNearQuery

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

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

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

the class CandidateQueryTests method testDuelSpecificQueries.

public void testDuelSpecificQueries() throws Exception {
    List<ParseContext.Document> documents = new ArrayList<>();
    CommonTermsQuery commonTermsQuery = new CommonTermsQuery(Occur.SHOULD, Occur.SHOULD, 128);
    commonTermsQuery.add(new Term("field", "quick"));
    commonTermsQuery.add(new Term("field", "brown"));
    commonTermsQuery.add(new Term("field", "fox"));
    addQuery(commonTermsQuery, documents);
    BlendedTermQuery blendedTermQuery = BlendedTermQuery.dismaxBlendedQuery(new Term[] { new Term("field", "quick"), new Term("field", "brown"), new Term("field", "fox") }, 1.0f);
    addQuery(blendedTermQuery, documents);
    SpanNearQuery spanNearQuery = new SpanNearQuery.Builder("field", true).addClause(new SpanTermQuery(new Term("field", "quick"))).addClause(new SpanTermQuery(new Term("field", "brown"))).addClause(new SpanTermQuery(new Term("field", "fox"))).build();
    addQuery(spanNearQuery, documents);
    SpanNearQuery spanNearQuery2 = new SpanNearQuery.Builder("field", true).addClause(new SpanTermQuery(new Term("field", "the"))).addClause(new SpanTermQuery(new Term("field", "lazy"))).addClause(new SpanTermQuery(new Term("field", "doc"))).build();
    SpanOrQuery spanOrQuery = new SpanOrQuery(spanNearQuery, spanNearQuery2);
    addQuery(spanOrQuery, documents);
    SpanNotQuery spanNotQuery = new SpanNotQuery(spanNearQuery, spanNearQuery);
    addQuery(spanNotQuery, documents);
    long lowerLong = randomIntBetween(0, 256);
    long upperLong = lowerLong + randomIntBetween(0, 32);
    addQuery(LongPoint.newRangeQuery("long_field", lowerLong, upperLong), documents);
    indexWriter.addDocuments(documents);
    indexWriter.close();
    directoryReader = DirectoryReader.open(directory);
    IndexSearcher shardSearcher = newSearcher(directoryReader);
    // Disable query cache, because ControlQuery cannot be cached...
    shardSearcher.setQueryCache(null);
    Document document = new Document();
    document.add(new TextField("field", "the quick brown fox jumps over the lazy dog", Field.Store.NO));
    long randomLong = randomIntBetween((int) lowerLong, (int) upperLong);
    document.add(new LongPoint("long_field", randomLong));
    MemoryIndex memoryIndex = MemoryIndex.fromDocument(document, new WhitespaceAnalyzer());
    duelRun(queryStore, memoryIndex, shardSearcher);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) ArrayList(java.util.ArrayList) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) Term(org.apache.lucene.index.Term) LongPoint(org.apache.lucene.document.LongPoint) Document(org.apache.lucene.document.Document) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) CommonTermsQuery(org.apache.lucene.queries.CommonTermsQuery) SpanNotQuery(org.apache.lucene.queries.spans.SpanNotQuery) MemoryIndex(org.apache.lucene.index.memory.MemoryIndex) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) TextField(org.apache.lucene.document.TextField) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery)

Example 4 with SpanNearQuery

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

the class SimpleQueryStringBuilderTests method testAnalyzerWithGraph.

public void testAnalyzerWithGraph() {
    SimpleQueryStringQueryParser.Settings settings = new SimpleQueryStringQueryParser.Settings();
    settings.analyzeWildcard(true);
    SimpleQueryStringQueryParser parser = new SimpleQueryStringQueryParser(new MockSynonymAnalyzer(), Collections.singletonMap(TEXT_FIELD_NAME, 1.0f), -1, settings, createShardContext());
    for (Operator op : Operator.values()) {
        BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
        parser.setDefaultOperator(defaultOp);
        // non-phrase won't detect multi-word synonym because of whitespace splitting
        Query query = parser.parse("guinea pig");
        Query expectedQuery = new BooleanQuery.Builder().add(new BooleanClause(new TermQuery(new Term(TEXT_FIELD_NAME, "guinea")), defaultOp)).add(new BooleanClause(new TermQuery(new Term(TEXT_FIELD_NAME, "pig")), defaultOp)).build();
        assertThat(query, equalTo(expectedQuery));
        // phrase will pick it up
        query = parser.parse("\"guinea pig\"");
        SpanTermQuery span1 = new SpanTermQuery(new Term(TEXT_FIELD_NAME, "guinea"));
        SpanTermQuery span2 = new SpanTermQuery(new Term(TEXT_FIELD_NAME, "pig"));
        expectedQuery = new SpanOrQuery(new SpanNearQuery(new SpanQuery[] { span1, span2 }, 0, true), new SpanTermQuery(new Term(TEXT_FIELD_NAME, "cavy")));
        assertThat(query, equalTo(expectedQuery));
        // phrase with slop
        query = parser.parse("big \"tiny guinea pig\"~2");
        PhraseQuery pq1 = new PhraseQuery.Builder().add(new Term(TEXT_FIELD_NAME, "tiny")).add(new Term(TEXT_FIELD_NAME, "guinea")).add(new Term(TEXT_FIELD_NAME, "pig")).setSlop(2).build();
        PhraseQuery pq2 = new PhraseQuery.Builder().add(new Term(TEXT_FIELD_NAME, "tiny")).add(new Term(TEXT_FIELD_NAME, "cavy")).setSlop(2).build();
        expectedQuery = new BooleanQuery.Builder().add(new TermQuery(new Term(TEXT_FIELD_NAME, "big")), defaultOp).add(new BooleanQuery.Builder().add(pq1, BooleanClause.Occur.SHOULD).add(pq2, BooleanClause.Occur.SHOULD).build(), defaultOp).build();
        assertThat(query, equalTo(expectedQuery));
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) SimpleQueryStringQueryParser(org.opensearch.index.search.SimpleQueryStringQueryParser) Query(org.apache.lucene.search.Query) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MockSynonymAnalyzer(org.apache.lucene.tests.analysis.MockSynonymAnalyzer) Term(org.apache.lucene.index.Term) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) BooleanClause(org.apache.lucene.search.BooleanClause) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) Settings(org.opensearch.common.settings.Settings)

Example 5 with SpanNearQuery

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

the class SpanGapQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(SpanNearQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
    assertThat(query, either(instanceOf(SpanNearQuery.class)).or(instanceOf(SpanTermQuery.class)).or(instanceOf(MatchAllQueryBuilder.class)));
    if (query instanceof SpanNearQuery) {
        SpanNearQuery spanNearQuery = (SpanNearQuery) query;
        assertThat(spanNearQuery.getSlop(), equalTo(queryBuilder.slop()));
        assertThat(spanNearQuery.isInOrder(), equalTo(queryBuilder.inOrder()));
        assertThat(spanNearQuery.getClauses().length, equalTo(queryBuilder.clauses().size()));
        Iterator<SpanQueryBuilder> spanQueryBuilderIterator = queryBuilder.clauses().iterator();
        for (SpanQuery spanQuery : spanNearQuery.getClauses()) {
            SpanQueryBuilder spanQB = spanQueryBuilderIterator.next();
            if (spanQB instanceof SpanGapQueryBuilder)
                continue;
            assertThat(spanQuery, equalTo(spanQB.toQuery(context)));
        }
    } else if (query instanceof SpanTermQuery) {
        assertThat(queryBuilder.clauses().size(), equalTo(1));
        assertThat(query, equalTo(queryBuilder.clauses().get(0).toQuery(context)));
    }
}
Also used : SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) SpanGapQueryBuilder(org.opensearch.index.query.SpanNearQueryBuilder.SpanGapQueryBuilder) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery)

Aggregations

SpanNearQuery (org.apache.lucene.queries.spans.SpanNearQuery)9 SpanTermQuery (org.apache.lucene.queries.spans.SpanTermQuery)8 SpanQuery (org.apache.lucene.queries.spans.SpanQuery)6 Term (org.apache.lucene.index.Term)5 SpanOrQuery (org.apache.lucene.queries.spans.SpanOrQuery)4 Query (org.apache.lucene.search.Query)4 Document (org.apache.lucene.document.Document)2 TextField (org.apache.lucene.document.TextField)2 IndexReader (org.apache.lucene.index.IndexReader)2 SpanMatchNoDocsQuery (org.apache.lucene.queries.SpanMatchNoDocsQuery)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 PrefixQuery (org.apache.lucene.search.PrefixQuery)2 TermQuery (org.apache.lucene.search.TermQuery)2 Directory (org.apache.lucene.store.Directory)2 ArrayList (java.util.ArrayList)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 WhitespaceAnalyzer (org.apache.lucene.analysis.core.WhitespaceAnalyzer)1 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)1