use of org.apache.lucene.search.spans.SpanTermQuery in project Anserini by castorini.
the class SdmQueryGenerator method buildQuery.
/*
* Sequential Dependency Model
*/
@Override
public Query buildQuery(String field, Analyzer analyzer, String queryText) {
List<String> tokens = AnalyzerUtils.analyze(analyzer, queryText);
BooleanQuery.Builder termsBuilder = new BooleanQuery.Builder();
if (tokens.size() == 1) {
termsBuilder.add(new TermQuery(new Term(field, tokens.get(0))), BooleanClause.Occur.SHOULD);
return termsBuilder.build();
}
BooleanQuery.Builder orderedWindowBuilder = new BooleanQuery.Builder();
BooleanQuery.Builder unorderedWindowBuilder = new BooleanQuery.Builder();
for (int i = 0; i < tokens.size() - 1; i++) {
termsBuilder.add(new TermQuery(new Term(field, tokens.get(i))), BooleanClause.Occur.SHOULD);
SpanTermQuery t1 = new SpanTermQuery(new Term(field, tokens.get(i)));
SpanTermQuery t2 = new SpanTermQuery(new Term(field, tokens.get(i + 1)));
SpanNearQuery orderedQ = new SpanNearQuery(new SpanQuery[] { t1, t2 }, 1, true);
SpanNearQuery unorderedQ = new SpanNearQuery(new SpanQuery[] { t1, t2 }, 8, false);
orderedWindowBuilder.add(orderedQ, BooleanClause.Occur.SHOULD);
unorderedWindowBuilder.add(unorderedQ, BooleanClause.Occur.SHOULD);
}
termsBuilder.add(new TermQuery(new Term(field, tokens.get(tokens.size() - 1))), BooleanClause.Occur.SHOULD);
BooleanQuery.Builder builder = new BooleanQuery.Builder();
builder.add(new BoostQuery(termsBuilder.build(), termWeight), BooleanClause.Occur.SHOULD);
builder.add(new BoostQuery(orderedWindowBuilder.build(), orderWindowWeight), BooleanClause.Occur.SHOULD);
builder.add(new BoostQuery(unorderedWindowBuilder.build(), unorderWindowWeight), BooleanClause.Occur.SHOULD);
return builder.build();
}
use of org.apache.lucene.search.spans.SpanTermQuery in project elasticsearch by elastic.
the class CustomFieldQuery method flatten.
@Override
void flatten(Query sourceQuery, IndexReader reader, Collection<Query> flatQueries, float boost) throws IOException {
if (sourceQuery instanceof BoostQuery) {
BoostQuery bq = (BoostQuery) sourceQuery;
sourceQuery = bq.getQuery();
boost *= bq.getBoost();
flatten(sourceQuery, reader, flatQueries, boost);
} else if (sourceQuery instanceof SpanTermQuery) {
super.flatten(new TermQuery(((SpanTermQuery) sourceQuery).getTerm()), reader, flatQueries, boost);
} else if (sourceQuery instanceof ConstantScoreQuery) {
flatten(((ConstantScoreQuery) sourceQuery).getQuery(), reader, flatQueries, boost);
} else if (sourceQuery instanceof FunctionScoreQuery) {
flatten(((FunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost);
} else if (sourceQuery instanceof MultiPhrasePrefixQuery) {
flatten(sourceQuery.rewrite(reader), reader, flatQueries, boost);
} else if (sourceQuery instanceof FiltersFunctionScoreQuery) {
flatten(((FiltersFunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost);
} else if (sourceQuery instanceof MultiPhraseQuery) {
MultiPhraseQuery q = ((MultiPhraseQuery) sourceQuery);
convertMultiPhraseQuery(0, new int[q.getTermArrays().length], q, q.getTermArrays(), q.getPositions(), reader, flatQueries);
} else if (sourceQuery instanceof BlendedTermQuery) {
final BlendedTermQuery blendedTermQuery = (BlendedTermQuery) sourceQuery;
flatten(blendedTermQuery.rewrite(reader), reader, flatQueries, boost);
} else if (sourceQuery instanceof ESToParentBlockJoinQuery) {
ESToParentBlockJoinQuery blockJoinQuery = (ESToParentBlockJoinQuery) sourceQuery;
flatten(blockJoinQuery.getChildQuery(), reader, flatQueries, boost);
} else if (sourceQuery instanceof BoostingQuery) {
BoostingQuery boostingQuery = (BoostingQuery) sourceQuery;
//flatten positive query with query boost
flatten(boostingQuery.getMatch(), reader, flatQueries, boost);
//flatten negative query with negative boost
flatten(boostingQuery.getContext(), reader, flatQueries, boostingQuery.getBoost());
} else if (sourceQuery instanceof SynonymQuery) {
// SynonymQuery should be handled by the parent class directly.
// This statement should be removed when https://issues.apache.org/jira/browse/LUCENE-7484 is merged.
SynonymQuery synQuery = (SynonymQuery) sourceQuery;
for (Term term : synQuery.getTerms()) {
flatten(new TermQuery(term), reader, flatQueries, boost);
}
} else {
super.flatten(sourceQuery, reader, flatQueries, boost);
}
}
use of org.apache.lucene.search.spans.SpanTermQuery in project elasticsearch by elastic.
the class QueryStringQueryBuilderTests method testToQueryWithGraph.
public void testToQueryWithGraph() throws Exception {
assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
for (Operator op : Operator.values()) {
BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
MapperQueryParser queryParser = new MapperQueryParser(createShardContext());
QueryParserSettings settings = new QueryParserSettings("");
settings.defaultField(STRING_FIELD_NAME);
settings.fieldsAndWeights(Collections.emptyMap());
settings.fuzziness(Fuzziness.AUTO);
settings.analyzeWildcard(true);
settings.rewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE);
settings.defaultOperator(op.toQueryParserOperator());
settings.forceAnalyzer(new MockSynonymAnalyzer());
settings.forceQuoteAnalyzer(new MockSynonymAnalyzer());
queryParser.reset(settings);
// simple multi-term
Query query = queryParser.parse("guinea pig");
Query expectedQuery = new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new TermQuery(new Term(STRING_FIELD_NAME, "guinea")), Occur.MUST).add(new TermQuery(new Term(STRING_FIELD_NAME, "pig")), Occur.MUST).build(), defaultOp).add(new TermQuery(new Term(STRING_FIELD_NAME, "cavy")), defaultOp).build();
assertThat(query, Matchers.equalTo(expectedQuery));
// simple with additional tokens
query = queryParser.parse("that guinea pig smells");
expectedQuery = new BooleanQuery.Builder().add(new TermQuery(new Term(STRING_FIELD_NAME, "that")), defaultOp).add(new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new BooleanClause(new TermQuery(new Term(STRING_FIELD_NAME, "guinea")), Occur.MUST)).add(new BooleanClause(new TermQuery(new Term(STRING_FIELD_NAME, "pig")), Occur.MUST)).build(), Occur.SHOULD).add(new TermQuery(new Term(STRING_FIELD_NAME, "cavy")), Occur.SHOULD).build(), defaultOp).add(new TermQuery(new Term(STRING_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(STRING_FIELD_NAME, "that")), Occur.MUST).add(new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new TermQuery(new Term(STRING_FIELD_NAME, "guinea")), Occur.MUST).add(new TermQuery(new Term(STRING_FIELD_NAME, "pig")), Occur.MUST).build(), defaultOp).add(new TermQuery(new Term(STRING_FIELD_NAME, "cavy")), defaultOp).build(), Occur.MUST_NOT).add(new TermQuery(new Term(STRING_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(STRING_FIELD_NAME, "that")), BooleanClause.Occur.MUST).add(new TermQuery(new Term(STRING_FIELD_NAME, "guinea")), BooleanClause.Occur.MUST_NOT).add(new TermQuery(new Term(STRING_FIELD_NAME, "pig")), defaultOp).add(new TermQuery(new Term(STRING_FIELD_NAME, "smells")), BooleanClause.Occur.MUST).build();
assertThat(query, Matchers.equalTo(expectedQuery));
// span query
query = queryParser.parse("\"that guinea pig smells\"");
expectedQuery = new BooleanQuery.Builder().add(new SpanNearQuery.Builder(STRING_FIELD_NAME, true).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "that"))).addClause(new SpanOrQuery(new SpanNearQuery.Builder(STRING_FIELD_NAME, true).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "guinea"))).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "pig"))).build(), new SpanTermQuery(new Term(STRING_FIELD_NAME, "cavy")))).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "smells"))).build(), Occur.SHOULD).setDisableCoord(true).build();
assertThat(query, Matchers.equalTo(expectedQuery));
// span query with slop
query = queryParser.parse("\"that guinea pig smells\"~2");
expectedQuery = new BooleanQuery.Builder().add(new SpanNearQuery.Builder(STRING_FIELD_NAME, true).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "that"))).addClause(new SpanOrQuery(new SpanNearQuery.Builder(STRING_FIELD_NAME, true).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "guinea"))).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "pig"))).build(), new SpanTermQuery(new Term(STRING_FIELD_NAME, "cavy")))).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "smells"))).setSlop(2).build(), Occur.SHOULD).setDisableCoord(true).build();
assertThat(query, Matchers.equalTo(expectedQuery));
}
}
use of org.apache.lucene.search.spans.SpanTermQuery in project elasticsearch by elastic.
the class SimpleQueryParserTests method testAnalyzerWithGraph.
public void testAnalyzerWithGraph() {
SimpleQueryParser.Settings settings = new SimpleQueryParser.Settings();
settings.analyzeWildcard(true);
Map<String, Float> weights = new HashMap<>();
weights.put("field1", 1.0f);
SimpleQueryParser parser = new MockSimpleQueryParser(new MockSynonymAnalyzer(), weights, -1, settings);
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("field1", "guinea")), defaultOp)).add(new BooleanClause(new TermQuery(new Term("field1", "pig")), defaultOp)).build();
assertThat(query, equalTo(expectedQuery));
// phrase will pick it up
query = parser.parse("\"guinea pig\"");
SpanTermQuery span1 = new SpanTermQuery(new Term("field1", "guinea"));
SpanTermQuery span2 = new SpanTermQuery(new Term("field1", "pig"));
expectedQuery = new SpanOrQuery(new SpanNearQuery(new SpanQuery[] { span1, span2 }, 0, true), new SpanTermQuery(new Term("field1", "cavy")));
assertThat(query, equalTo(expectedQuery));
// phrase with slop
query = parser.parse("big \"tiny guinea pig\"~2");
expectedQuery = new BooleanQuery.Builder().add(new TermQuery(new Term("field1", "big")), defaultOp).add(new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term("field1", "tiny")), new SpanOrQuery(new SpanNearQuery(new SpanQuery[] { span1, span2 }, 0, true), new SpanTermQuery(new Term("field1", "cavy"))) }, 2, true), defaultOp).build();
assertThat(query, equalTo(expectedQuery));
}
}
use of org.apache.lucene.search.spans.SpanTermQuery in project elasticsearch by elastic.
the class CandidateQueryTests method testDuelSpecificQueries.
public void testDuelSpecificQueries() throws Exception {
List<ParseContext.Document> documents = new ArrayList<>();
CommonTermsQuery commonTermsQuery = new CommonTermsQuery(BooleanClause.Occur.SHOULD, BooleanClause.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.booleanBlendedQuery(new Term[] { new Term("field", "quick"), new Term("field", "brown"), new Term("field", "fox") }, false);
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);
}
Aggregations