Search in sources :

Example 6 with SpanQuery

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

Example 7 with SpanQuery

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

the class SpanWithinQueryBuilder 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 SpanWithinQuery((SpanQuery) innerBig, (SpanQuery) innerLittle);
}
Also used : Query(org.apache.lucene.search.Query) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) SpanWithinQuery(org.apache.lucene.queries.spans.SpanWithinQuery) SpanWithinQuery(org.apache.lucene.queries.spans.SpanWithinQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery)

Example 8 with SpanQuery

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

the class FieldMaskingSpanQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    String fieldInQuery = fieldName;
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType != null) {
        fieldInQuery = fieldType.name();
    }
    Query innerQuery = queryBuilder.toQuery(context);
    assert innerQuery instanceof SpanQuery;
    return new FieldMaskingSpanQuery((SpanQuery) innerQuery, fieldInQuery);
}
Also used : Query(org.apache.lucene.search.Query) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) FieldMaskingSpanQuery(org.apache.lucene.queries.spans.FieldMaskingSpanQuery) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) FieldMaskingSpanQuery(org.apache.lucene.queries.spans.FieldMaskingSpanQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) FieldMaskingSpanQuery(org.apache.lucene.queries.spans.FieldMaskingSpanQuery)

Example 9 with SpanQuery

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

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

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