use of org.apache.lucene.search.BooleanQuery in project elasticsearch by elastic.
the class Queries method applyMinimumShouldMatch.
public static Query applyMinimumShouldMatch(BooleanQuery query, @Nullable String minimumShouldMatch) {
if (minimumShouldMatch == null) {
return query;
}
int optionalClauses = 0;
for (BooleanClause c : query.clauses()) {
if (c.getOccur() == BooleanClause.Occur.SHOULD) {
optionalClauses++;
}
}
int msm = calculateMinShouldMatch(optionalClauses, minimumShouldMatch);
if (0 < msm) {
BooleanQuery.Builder builder = new BooleanQuery.Builder();
builder.setDisableCoord(query.isCoordDisabled());
for (BooleanClause clause : query) {
builder.add(clause);
}
builder.setMinimumNumberShouldMatch(msm);
return builder.build();
} else {
return query;
}
}
use of org.apache.lucene.search.BooleanQuery in project elasticsearch by elastic.
the class BoolQueryBuilder method doToQuery.
@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder();
booleanQueryBuilder.setDisableCoord(disableCoord);
addBooleanClauses(context, booleanQueryBuilder, mustClauses, BooleanClause.Occur.MUST);
addBooleanClauses(context, booleanQueryBuilder, mustNotClauses, BooleanClause.Occur.MUST_NOT);
addBooleanClauses(context, booleanQueryBuilder, shouldClauses, BooleanClause.Occur.SHOULD);
addBooleanClauses(context, booleanQueryBuilder, filterClauses, BooleanClause.Occur.FILTER);
BooleanQuery booleanQuery = booleanQueryBuilder.build();
if (booleanQuery.clauses().isEmpty()) {
return new MatchAllDocsQuery();
}
final String minimumShouldMatch;
if (context.isFilter() && this.minimumShouldMatch == null && shouldClauses.size() > 0) {
minimumShouldMatch = "1";
} else {
minimumShouldMatch = this.minimumShouldMatch;
}
Query query = Queries.applyMinimumShouldMatch(booleanQuery, minimumShouldMatch);
return adjustPureNegative ? fixNegativeQueryIfNeeded(query) : query;
}
use of org.apache.lucene.search.BooleanQuery in project elasticsearch by elastic.
the class CustomUnifiedHighlighterTests method testSentenceBoundedBreakIterator.
public void testSentenceBoundedBreakIterator() throws Exception {
final String[] inputs = { "The quick brown fox in a long sentence with another quick brown fox. " + "Another sentence with brown fox." };
final String[] outputs = { "The <b>quick</b> <b>brown</b>", "<b>fox</b> in a long", "with another <b>quick</b>", "<b>brown</b> <b>fox</b>.", "sentence with <b>brown</b>", "<b>fox</b>." };
BooleanQuery query = new BooleanQuery.Builder().add(new TermQuery(new Term("text", "quick")), BooleanClause.Occur.SHOULD).add(new TermQuery(new Term("text", "brown")), BooleanClause.Occur.SHOULD).add(new TermQuery(new Term("text", "fox")), BooleanClause.Occur.SHOULD).build();
assertHighlightOneDoc("text", inputs, new StandardAnalyzer(), query, Locale.ROOT, BoundedBreakIteratorScanner.getSentence(Locale.ROOT, 10), 0, outputs);
}
use of org.apache.lucene.search.BooleanQuery in project neo4j-mobile-android by neo4j-contrib.
the class FullTxData method injectOrphans.
private Query injectOrphans(Query query) {
if (query instanceof BooleanQuery) {
BooleanQuery source = (BooleanQuery) query;
BooleanQuery result = new BooleanQuery();
for (BooleanClause clause : source.clauses()) {
result.add(injectOrphans(clause.getQuery()), clause.getOccur());
}
return result;
} else {
BooleanQuery result = new BooleanQuery();
result.add(query, Occur.SHOULD);
result.add(new TermQuery(new Term(ORPHANS_KEY, extractTermField(query))), Occur.SHOULD);
return result;
}
}
use of org.apache.lucene.search.BooleanQuery in project graphdb by neo4j-attic.
the class FullTxData method injectOrphans.
private Query injectOrphans(Query query) {
if (query instanceof BooleanQuery) {
BooleanQuery source = (BooleanQuery) query;
BooleanQuery result = new BooleanQuery();
for (BooleanClause clause : source.clauses()) {
result.add(injectOrphans(clause.getQuery()), clause.getOccur());
}
return result;
} else {
Set<Term> terms = new HashSet<Term>();
query.extractTerms(terms);
// TODO Don't only use the first term
Term term = terms.iterator().next();
BooleanQuery result = new BooleanQuery();
result.add(query, Occur.SHOULD);
result.add(new TermQuery(new Term(ORPHANS_KEY, term.field())), Occur.SHOULD);
return result;
}
}
Aggregations