Search in sources :

Example 11 with SpanQuery

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

the class SpanOrQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(SpanOrQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
    assertThat(query, instanceOf(SpanOrQuery.class));
    SpanOrQuery spanOrQuery = (SpanOrQuery) query;
    assertThat(spanOrQuery.getClauses().length, equalTo(queryBuilder.clauses().size()));
    Iterator<SpanQueryBuilder> spanQueryBuilderIterator = queryBuilder.clauses().iterator();
    for (SpanQuery spanQuery : spanOrQuery.getClauses()) {
        assertThat(spanQuery, equalTo(spanQueryBuilderIterator.next().toQuery(context)));
    }
}
Also used : SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery)

Example 12 with SpanQuery

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

the class SpanMatchNoDocsQueryTests method testQuery.

public void testQuery() throws Exception {
    Directory dir = newDirectory();
    Analyzer analyzer = new MockAnalyzer(random());
    IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(analyzer).setMaxBufferedDocs(2).setMergePolicy(newLogMergePolicy()));
    addDoc("one", iw);
    addDoc("two", iw);
    addDoc("three", iw);
    IndexReader ir = DirectoryReader.open(iw);
    IndexSearcher searcher = new IndexSearcher(ir);
    Query query = new SpanMatchNoDocsQuery("unknown", "field not found");
    assertEquals(searcher.count(query), 0);
    ScoreDoc[] hits;
    hits = searcher.search(query, 1000).scoreDocs;
    assertEquals(0, hits.length);
    assertEquals(query.toString(), "SpanMatchNoDocsQuery(\"field not found\")");
    SpanOrQuery orQuery = new SpanOrQuery(new SpanMatchNoDocsQuery("unknown", "field not found"), new SpanTermQuery(new Term("unknown", "one")));
    assertEquals(searcher.count(orQuery), 0);
    hits = searcher.search(orQuery, 1000).scoreDocs;
    assertEquals(0, hits.length);
    orQuery = new SpanOrQuery(new SpanMatchNoDocsQuery("key", "a good reason"), new SpanTermQuery(new Term("key", "one")));
    assertEquals(searcher.count(orQuery), 1);
    hits = searcher.search(orQuery, 1000).scoreDocs;
    assertEquals(1, hits.length);
    Query rewrite = orQuery.rewrite(ir);
    assertEquals(rewrite, orQuery);
    SpanNearQuery nearQuery = new SpanNearQuery(new SpanQuery[] { new SpanMatchNoDocsQuery("same", ""), new SpanMatchNoDocsQuery("same", "") }, 0, true);
    assertEquals(searcher.count(nearQuery), 0);
    hits = searcher.search(nearQuery, 1000).scoreDocs;
    assertEquals(0, hits.length);
    rewrite = nearQuery.rewrite(ir);
    assertEquals(rewrite, nearQuery);
    iw.close();
    ir.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) SpanMatchNoDocsQuery(org.apache.lucene.queries.SpanMatchNoDocsQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) Term(org.apache.lucene.index.Term) MockAnalyzer(org.apache.lucene.tests.analysis.MockAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) ScoreDoc(org.apache.lucene.search.ScoreDoc) MockAnalyzer(org.apache.lucene.tests.analysis.MockAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) IndexReader(org.apache.lucene.index.IndexReader) SpanMatchNoDocsQuery(org.apache.lucene.queries.SpanMatchNoDocsQuery) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) Directory(org.apache.lucene.store.Directory)

Example 13 with SpanQuery

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

the class MatchQueryBuilderTests method testMultiWordSynonymsPhrase.

public void testMultiWordSynonymsPhrase() throws Exception {
    final MatchQuery matchQuery = new MatchQuery(createShardContext());
    matchQuery.setAnalyzer(new MockSynonymAnalyzer());
    final Query actual = matchQuery.parse(Type.PHRASE, TEXT_FIELD_NAME, "guinea pig dogs");
    Query expected = SpanNearQuery.newOrderedNearQuery(TEXT_FIELD_NAME).addClause(new SpanOrQuery(new SpanQuery[] { SpanNearQuery.newOrderedNearQuery(TEXT_FIELD_NAME).addClause(new SpanTermQuery(new Term(TEXT_FIELD_NAME, "guinea"))).addClause(new SpanTermQuery(new Term(TEXT_FIELD_NAME, "pig"))).setSlop(0).build(), new SpanTermQuery(new Term(TEXT_FIELD_NAME, "cavy")) })).addClause(new SpanOrQuery(new SpanQuery[] { new SpanTermQuery(new Term(TEXT_FIELD_NAME, "dogs")), new SpanTermQuery(new Term(TEXT_FIELD_NAME, "dog")) })).build();
    assertEquals(expected, actual);
}
Also used : 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) ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MultiPhrasePrefixQuery(org.opensearch.common.lucene.search.MultiPhrasePrefixQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ZeroTermsQuery(org.opensearch.index.search.MatchQuery.ZeroTermsQuery) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) MatchQuery(org.opensearch.index.search.MatchQuery) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) MatchQuery(org.opensearch.index.search.MatchQuery) MockSynonymAnalyzer(org.apache.lucene.tests.analysis.MockSynonymAnalyzer) Term(org.apache.lucene.index.Term) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery)

Example 14 with SpanQuery

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

the class SpanNearQueryBuilderTests 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(SpanMatchNoDocsQuery.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()) {
            assertThat(spanQuery, equalTo(spanQueryBuilderIterator.next().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) SpanMatchNoDocsQuery(org.apache.lucene.queries.SpanMatchNoDocsQuery) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery)

Example 15 with SpanQuery

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

the class TextFieldMapper method createPhrasePrefixQuery.

public static Query createPhrasePrefixQuery(TokenStream stream, String field, int slop, int maxExpansions, String prefixField, IntPredicate usePrefixField) throws IOException {
    MultiPhrasePrefixQuery builder = new MultiPhrasePrefixQuery(field);
    builder.setSlop(slop);
    builder.setMaxExpansions(maxExpansions);
    List<Term> currentTerms = new ArrayList<>();
    TermToBytesRefAttribute termAtt = stream.getAttribute(TermToBytesRefAttribute.class);
    PositionIncrementAttribute posIncrAtt = stream.getAttribute(PositionIncrementAttribute.class);
    stream.reset();
    int position = -1;
    while (stream.incrementToken()) {
        if (posIncrAtt.getPositionIncrement() != 0) {
            if (currentTerms.isEmpty() == false) {
                builder.add(currentTerms.toArray(new Term[0]), position);
            }
            position += posIncrAtt.getPositionIncrement();
            currentTerms.clear();
        }
        currentTerms.add(new Term(field, termAtt.getBytesRef()));
    }
    builder.add(currentTerms.toArray(new Term[0]), position);
    if (prefixField == null) {
        return builder;
    }
    int lastPos = builder.getTerms().length - 1;
    final Term[][] terms = builder.getTerms();
    final int[] positions = builder.getPositions();
    for (Term term : terms[lastPos]) {
        String value = term.text();
        if (usePrefixField.test(value.length()) == false) {
            return builder;
        }
    }
    if (terms.length == 1) {
        SynonymQuery.Builder sb = new SynonymQuery.Builder(prefixField);
        Arrays.stream(terms[0]).map(term -> new Term(prefixField, term.bytes())).forEach(sb::addTerm);
        return sb.build();
    }
    SpanNearQuery.Builder spanQuery = new SpanNearQuery.Builder(field, true);
    spanQuery.setSlop(slop);
    int previousPos = -1;
    for (int i = 0; i < terms.length; i++) {
        Term[] posTerms = terms[i];
        int posInc = positions[i] - previousPos;
        previousPos = positions[i];
        if (posInc > 1) {
            spanQuery.addGap(posInc - 1);
        }
        if (i == lastPos) {
            if (posTerms.length == 1) {
                FieldMaskingSpanQuery fieldMask = new FieldMaskingSpanQuery(new SpanTermQuery(new Term(prefixField, posTerms[0].bytes())), field);
                spanQuery.addClause(fieldMask);
            } else {
                SpanQuery[] queries = Arrays.stream(posTerms).map(term -> new FieldMaskingSpanQuery(new SpanTermQuery(new Term(prefixField, term.bytes())), field)).toArray(SpanQuery[]::new);
                spanQuery.addClause(new SpanOrQuery(queries));
            }
        } else {
            if (posTerms.length == 1) {
                spanQuery.addClause(new SpanTermQuery(posTerms[0]));
            } else {
                SpanTermQuery[] queries = Arrays.stream(posTerms).map(SpanTermQuery::new).toArray(SpanTermQuery[]::new);
                spanQuery.addClause(new SpanOrQuery(queries));
            }
        }
    }
    return spanQuery.build();
}
Also used : Query(org.apache.lucene.search.Query) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) Arrays(java.util.Arrays) SimilarityProvider(org.opensearch.index.similarity.SimilarityProvider) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) EdgeNGramTokenFilter(org.apache.lucene.analysis.ngram.EdgeNGramTokenFilter) FieldType(org.apache.lucene.document.FieldType) ToXContent(org.opensearch.common.xcontent.ToXContent) Term(org.apache.lucene.index.Term) PhraseQuery(org.apache.lucene.search.PhraseQuery) Version(org.opensearch.Version) AnalyzerScope(org.opensearch.index.analysis.AnalyzerScope) IntervalMode(org.opensearch.index.query.IntervalMode) IntPredicate(java.util.function.IntPredicate) Operations(org.apache.lucene.util.automaton.Operations) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) PagedBytesIndexFieldData(org.opensearch.index.fielddata.plain.PagedBytesIndexFieldData) Map(java.util.Map) Lucene(org.opensearch.common.lucene.Lucene) IntervalsSource(org.apache.lucene.queries.intervals.IntervalsSource) Automata(org.apache.lucene.util.automaton.Automata) PositionIncrementAttribute(org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute) Iterators(org.opensearch.common.collect.Iterators) BytesRef(org.apache.lucene.util.BytesRef) Automaton(org.apache.lucene.util.automaton.Automaton) PrefixQuery(org.apache.lucene.search.PrefixQuery) SearchLookup(org.opensearch.search.lookup.SearchLookup) Objects(java.util.Objects) SpanMultiTermQueryWrapper(org.apache.lucene.queries.spans.SpanMultiTermQueryWrapper) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) List(java.util.List) QueryShardContext(org.opensearch.index.query.QueryShardContext) TokenFilter(org.apache.lucene.analysis.TokenFilter) FixedShingleFilter(org.apache.lucene.analysis.shingle.FixedShingleFilter) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) IndexAnalyzers(org.opensearch.index.analysis.IndexAnalyzers) AutomatonQuery(org.apache.lucene.search.AutomatonQuery) AutomatonQueries(org.opensearch.common.lucene.search.AutomatonQueries) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) IndexFieldData(org.opensearch.index.fielddata.IndexFieldData) LegacyESVersion(org.opensearch.LegacyESVersion) ParserContext(org.opensearch.index.mapper.Mapper.TypeParser.ParserContext) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) IntervalBuilder(org.opensearch.index.query.IntervalBuilder) BytesTermAttribute(org.apache.lucene.analysis.tokenattributes.BytesTermAttribute) CachingTokenFilter(org.apache.lucene.analysis.CachingTokenFilter) CoreValuesSourceType(org.opensearch.search.aggregations.support.CoreValuesSourceType) TermToBytesRefAttribute(org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute) NamedAnalyzer(org.opensearch.index.analysis.NamedAnalyzer) TokenStream(org.apache.lucene.analysis.TokenStream) Iterator(java.util.Iterator) Analyzer(org.apache.lucene.analysis.Analyzer) IOException(java.io.IOException) MultiPhrasePrefixQuery(org.opensearch.common.lucene.search.MultiPhrasePrefixQuery) BooleanClause(org.apache.lucene.search.BooleanClause) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) TermQuery(org.apache.lucene.search.TermQuery) AnalyzerWrapper(org.apache.lucene.analysis.AnalyzerWrapper) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Field(org.apache.lucene.document.Field) FieldMaskingSpanQuery(org.apache.lucene.queries.spans.FieldMaskingSpanQuery) XContentMapValues(org.opensearch.common.xcontent.support.XContentMapValues) Intervals(org.apache.lucene.queries.intervals.Intervals) IndexOptions(org.apache.lucene.index.IndexOptions) Collections(java.util.Collections) SynonymQuery(org.apache.lucene.search.SynonymQuery) IntervalBuilder(org.opensearch.index.query.IntervalBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) PositionIncrementAttribute(org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) FieldMaskingSpanQuery(org.apache.lucene.queries.spans.FieldMaskingSpanQuery) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) TermToBytesRefAttribute(org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute) FieldMaskingSpanQuery(org.apache.lucene.queries.spans.FieldMaskingSpanQuery) MultiPhrasePrefixQuery(org.opensearch.common.lucene.search.MultiPhrasePrefixQuery) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery)

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