use of org.apache.lucene.queries.spans.SpanOrQuery 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)));
}
}
use of org.apache.lucene.queries.spans.SpanOrQuery 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();
}
use of org.apache.lucene.queries.spans.SpanOrQuery 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);
}
use of org.apache.lucene.queries.spans.SpanOrQuery in project OpenSearch by opensearch-project.
the class QueryStringQueryBuilderTests method testToQueryWithGraph.
public void testToQueryWithGraph() throws Exception {
for (Operator op : Operator.values()) {
BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
QueryStringQueryParser queryParser = new QueryStringQueryParser(createShardContext(), TEXT_FIELD_NAME);
queryParser.setAnalyzeWildcard(true);
queryParser.setMultiTermRewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE);
queryParser.setDefaultOperator(op.toQueryParserOperator());
queryParser.setAnalyzeWildcard(true);
queryParser.setMultiTermRewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE);
queryParser.setDefaultOperator(op.toQueryParserOperator());
queryParser.setForceAnalyzer(new MockSynonymAnalyzer());
queryParser.setAutoGenerateMultiTermSynonymsPhraseQuery(false);
// simple multi-term
Query query = queryParser.parse("guinea pig");
Query guineaPig = new BooleanQuery.Builder().add(new TermQuery(new Term(TEXT_FIELD_NAME, "guinea")), Occur.MUST).add(new TermQuery(new Term(TEXT_FIELD_NAME, "pig")), Occur.MUST).build();
TermQuery cavy = new TermQuery(new Term(TEXT_FIELD_NAME, "cavy"));
Query expectedQuery = new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(guineaPig, Occur.SHOULD).add(cavy, Occur.SHOULD).build(), defaultOp).build();
assertThat(query, Matchers.equalTo(expectedQuery));
queryParser.setAutoGenerateMultiTermSynonymsPhraseQuery(true);
// simple multi-term with phrase query
query = queryParser.parse("guinea pig");
expectedQuery = new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new PhraseQuery.Builder().add(new Term(TEXT_FIELD_NAME, "guinea")).add(new Term(TEXT_FIELD_NAME, "pig")).build(), Occur.SHOULD).add(new TermQuery(new Term(TEXT_FIELD_NAME, "cavy")), Occur.SHOULD).build(), defaultOp).build();
assertThat(query, Matchers.equalTo(expectedQuery));
queryParser.setAutoGenerateMultiTermSynonymsPhraseQuery(false);
// simple with additional tokens
query = queryParser.parse("that guinea pig smells");
expectedQuery = new BooleanQuery.Builder().add(new TermQuery(new Term(TEXT_FIELD_NAME, "that")), defaultOp).add(new BooleanQuery.Builder().add(guineaPig, Occur.SHOULD).add(cavy, Occur.SHOULD).build(), defaultOp).add(new TermQuery(new Term(TEXT_FIELD_NAME, "smells")), defaultOp).build();
assertThat(query, Matchers.equalTo(expectedQuery));
// complex
query = queryParser.parse("+that -(guinea pig) +smells");
expectedQuery = new BooleanQuery.Builder().add(new TermQuery(new Term(TEXT_FIELD_NAME, "that")), Occur.MUST).add(new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(guineaPig, Occur.SHOULD).add(cavy, Occur.SHOULD).build(), defaultOp).build(), Occur.MUST_NOT).add(new TermQuery(new Term(TEXT_FIELD_NAME, "smells")), Occur.MUST).build();
assertThat(query, Matchers.equalTo(expectedQuery));
// no parent should cause guinea and pig to be treated as separate tokens
query = queryParser.parse("+that -guinea pig +smells");
expectedQuery = new BooleanQuery.Builder().add(new TermQuery(new Term(TEXT_FIELD_NAME, "that")), BooleanClause.Occur.MUST).add(new TermQuery(new Term(TEXT_FIELD_NAME, "guinea")), BooleanClause.Occur.MUST_NOT).add(new TermQuery(new Term(TEXT_FIELD_NAME, "pig")), defaultOp).add(new TermQuery(new Term(TEXT_FIELD_NAME, "smells")), BooleanClause.Occur.MUST).build();
assertThat(query, Matchers.equalTo(expectedQuery));
// span query
query = queryParser.parse("\"that guinea pig smells\"");
expectedQuery = new SpanNearQuery.Builder(TEXT_FIELD_NAME, true).addClause(new SpanTermQuery(new Term(TEXT_FIELD_NAME, "that"))).addClause(new SpanOrQuery(new SpanNearQuery.Builder(TEXT_FIELD_NAME, true).addClause(new SpanTermQuery(new Term(TEXT_FIELD_NAME, "guinea"))).addClause(new SpanTermQuery(new Term(TEXT_FIELD_NAME, "pig"))).build(), new SpanTermQuery(new Term(TEXT_FIELD_NAME, "cavy")))).addClause(new SpanTermQuery(new Term(TEXT_FIELD_NAME, "smells"))).build();
assertThat(query, Matchers.equalTo(expectedQuery));
// span query with slop
query = queryParser.parse("\"that guinea pig smells\"~2");
PhraseQuery pq1 = new PhraseQuery.Builder().add(new Term(TEXT_FIELD_NAME, "that")).add(new Term(TEXT_FIELD_NAME, "guinea")).add(new Term(TEXT_FIELD_NAME, "pig")).add(new Term(TEXT_FIELD_NAME, "smells")).setSlop(2).build();
PhraseQuery pq2 = new PhraseQuery.Builder().add(new Term(TEXT_FIELD_NAME, "that")).add(new Term(TEXT_FIELD_NAME, "cavy")).add(new Term(TEXT_FIELD_NAME, "smells")).setSlop(2).build();
expectedQuery = new BooleanQuery.Builder().add(pq1, Occur.SHOULD).add(pq2, Occur.SHOULD).build();
assertThat(query, Matchers.equalTo(expectedQuery));
}
}
use of org.apache.lucene.queries.spans.SpanOrQuery in project OpenSearch by opensearch-project.
the class TextFieldMapperTests method testFastPhrasePrefixes.
public void testFastPhrasePrefixes() throws IOException {
MapperService mapperService = createMapperService(mapping(b -> {
b.startObject("field");
{
b.field("type", "text");
b.field("analyzer", "my_stop_analyzer");
b.startObject("index_prefixes").field("min_chars", 2).field("max_chars", 10).endObject();
}
b.endObject();
b.startObject("synfield");
{
b.field("type", "text");
// "standard" will be replaced with MockSynonymAnalyzer
b.field("analyzer", "standard");
b.field("index_phrases", true);
b.startObject("index_prefixes").field("min_chars", 2).field("max_chars", 10).endObject();
}
b.endObject();
}));
QueryShardContext queryShardContext = createQueryShardContext(mapperService);
{
Query q = new MatchPhrasePrefixQueryBuilder("field", "two words").toQuery(queryShardContext);
Query expected = new SpanNearQuery.Builder("field", true).addClause(new SpanTermQuery(new Term("field", "two"))).addClause(new FieldMaskingSpanQuery(new SpanTermQuery(new Term("field._index_prefix", "words")), "field")).build();
assertThat(q, equalTo(expected));
}
{
Query q = new MatchPhrasePrefixQueryBuilder("field", "three words here").toQuery(queryShardContext);
Query expected = new SpanNearQuery.Builder("field", true).addClause(new SpanTermQuery(new Term("field", "three"))).addClause(new SpanTermQuery(new Term("field", "words"))).addClause(new FieldMaskingSpanQuery(new SpanTermQuery(new Term("field._index_prefix", "here")), "field")).build();
assertThat(q, equalTo(expected));
}
{
Query q = new MatchPhrasePrefixQueryBuilder("field", "two words").slop(1).toQuery(queryShardContext);
MultiPhrasePrefixQuery mpq = new MultiPhrasePrefixQuery("field");
mpq.setSlop(1);
mpq.add(new Term("field", "two"));
mpq.add(new Term("field", "words"));
assertThat(q, equalTo(mpq));
}
{
Query q = new MatchPhrasePrefixQueryBuilder("field", "singleton").toQuery(queryShardContext);
assertThat(q, is(new SynonymQuery.Builder("field._index_prefix").addTerm(new Term("field._index_prefix", "singleton")).build()));
}
{
Query q = new MatchPhrasePrefixQueryBuilder("field", "sparkle a stopword").toQuery(queryShardContext);
Query expected = new SpanNearQuery.Builder("field", true).addClause(new SpanTermQuery(new Term("field", "sparkle"))).addGap(1).addClause(new FieldMaskingSpanQuery(new SpanTermQuery(new Term("field._index_prefix", "stopword")), "field")).build();
assertThat(q, equalTo(expected));
}
{
MatchQuery matchQuery = new MatchQuery(queryShardContext);
matchQuery.setAnalyzer(new MockSynonymAnalyzer());
Query q = matchQuery.parse(MatchQuery.Type.PHRASE_PREFIX, "synfield", "motor dogs");
Query expected = new SpanNearQuery.Builder("synfield", true).addClause(new SpanTermQuery(new Term("synfield", "motor"))).addClause(new SpanOrQuery(new FieldMaskingSpanQuery(new SpanTermQuery(new Term("synfield._index_prefix", "dogs")), "synfield"), new FieldMaskingSpanQuery(new SpanTermQuery(new Term("synfield._index_prefix", "dog")), "synfield"))).build();
assertThat(q, equalTo(expected));
}
{
MatchQuery matchQuery = new MatchQuery(queryShardContext);
matchQuery.setPhraseSlop(1);
matchQuery.setAnalyzer(new MockSynonymAnalyzer());
Query q = matchQuery.parse(MatchQuery.Type.PHRASE_PREFIX, "synfield", "two dogs");
MultiPhrasePrefixQuery mpq = new MultiPhrasePrefixQuery("synfield");
mpq.setSlop(1);
mpq.add(new Term("synfield", "two"));
mpq.add(new Term[] { new Term("synfield", "dogs"), new Term("synfield", "dog") });
assertThat(q, equalTo(mpq));
}
{
Query q = new MatchPhrasePrefixQueryBuilder("field", "motor d").toQuery(queryShardContext);
MultiPhrasePrefixQuery mpq = new MultiPhrasePrefixQuery("field");
mpq.add(new Term("field", "motor"));
mpq.add(new Term("field", "d"));
assertThat(q, equalTo(mpq));
}
}
Aggregations