Search in sources :

Example 1 with SpanNotQuery

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

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

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

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

the class SpanNotQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(SpanNotQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
    assertThat(query, instanceOf(SpanNotQuery.class));
    SpanNotQuery spanNotQuery = (SpanNotQuery) query;
    assertThat(spanNotQuery.getExclude(), equalTo(queryBuilder.excludeQuery().toQuery(context)));
    assertThat(spanNotQuery.getInclude(), equalTo(queryBuilder.includeQuery().toQuery(context)));
}
Also used : SpanNotQuery(org.apache.lucene.queries.spans.SpanNotQuery)

Aggregations

SpanNotQuery (org.apache.lucene.queries.spans.SpanNotQuery)4 Term (org.apache.lucene.index.Term)2 SpanTermQuery (org.apache.lucene.queries.spans.SpanTermQuery)2 ArrayList (java.util.ArrayList)1 WhitespaceAnalyzer (org.apache.lucene.analysis.core.WhitespaceAnalyzer)1 Document (org.apache.lucene.document.Document)1 LongPoint (org.apache.lucene.document.LongPoint)1 TextField (org.apache.lucene.document.TextField)1 MemoryIndex (org.apache.lucene.index.memory.MemoryIndex)1 BlendedTermQuery (org.apache.lucene.queries.BlendedTermQuery)1 CommonTermsQuery (org.apache.lucene.queries.CommonTermsQuery)1 SpanNearQuery (org.apache.lucene.queries.spans.SpanNearQuery)1 SpanOrQuery (org.apache.lucene.queries.spans.SpanOrQuery)1 SpanQuery (org.apache.lucene.queries.spans.SpanQuery)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 Query (org.apache.lucene.search.Query)1 Result (org.opensearch.percolator.QueryAnalyzer.Result)1 QueryAnalyzer.selectBestResult (org.opensearch.percolator.QueryAnalyzer.selectBestResult)1