use of org.apache.lucene.search.BooleanClause in project querydsl by querydsl.
the class LuceneSerializer method toQuery.
private Query toQuery(Operation<?> operation, QueryMetadata metadata) {
Operator op = operation.getOperator();
if (op == Ops.OR) {
return toTwoHandSidedQuery(operation, Occur.SHOULD, metadata);
} else if (op == Ops.AND) {
return toTwoHandSidedQuery(operation, Occur.MUST, metadata);
} else if (op == Ops.NOT) {
BooleanQuery bq = new BooleanQuery();
bq.add(new BooleanClause(toQuery(operation.getArg(0), metadata), Occur.MUST_NOT));
bq.add(new BooleanClause(new MatchAllDocsQuery(), Occur.MUST));
return bq;
} else if (op == Ops.LIKE) {
return like(operation, metadata);
} else if (op == Ops.LIKE_IC) {
throw new IgnoreCaseUnsupportedException();
} else if (op == Ops.EQ) {
return eq(operation, metadata, false);
} else if (op == Ops.EQ_IGNORE_CASE) {
throw new IgnoreCaseUnsupportedException();
} else if (op == Ops.NE) {
return ne(operation, metadata, false);
} else if (op == Ops.STARTS_WITH) {
return startsWith(metadata, operation, false);
} else if (op == Ops.STARTS_WITH_IC) {
throw new IgnoreCaseUnsupportedException();
} else if (op == Ops.ENDS_WITH) {
return endsWith(operation, metadata, false);
} else if (op == Ops.ENDS_WITH_IC) {
throw new IgnoreCaseUnsupportedException();
} else if (op == Ops.STRING_CONTAINS) {
return stringContains(operation, metadata, false);
} else if (op == Ops.STRING_CONTAINS_IC) {
throw new IgnoreCaseUnsupportedException();
} else if (op == Ops.BETWEEN) {
return between(operation, metadata);
} else if (op == Ops.IN) {
return in(operation, metadata, false);
} else if (op == Ops.NOT_IN) {
return notIn(operation, metadata, false);
} else if (op == Ops.LT) {
return lt(operation, metadata);
} else if (op == Ops.GT) {
return gt(operation, metadata);
} else if (op == Ops.LOE) {
return le(operation, metadata);
} else if (op == Ops.GOE) {
return ge(operation, metadata);
} else if (op == LuceneOps.LUCENE_QUERY) {
@SuppressWarnings("unchecked") Constant<Query> // this is the expected type
expectedConstant = (Constant<Query>) operation.getArg(0);
return expectedConstant.getConstant();
}
throw new UnsupportedOperationException("Illegal operation " + operation);
}
use of org.apache.lucene.search.BooleanClause in project OpenGrok by OpenGrok.
the class QueryBuilder method build.
/**
* Build a new query based on the query text that has been passed in to this
* builder.
*
* @return a query, or {@code null} if no query text is available.
* @throws ParseException if the query text cannot be parsed
*/
public Query build() throws ParseException {
if (queries.isEmpty()) {
// We don't have any text to parse
return null;
}
// Parse each of the query texts separately
ArrayList<Query> queryList = new ArrayList<>(queries.size());
for (Map.Entry<String, String> entry : queries.entrySet()) {
String field = entry.getKey();
String queryText = entry.getValue();
queryList.add(buildQuery(field, escapeQueryString(field, queryText)));
}
// If we only have one sub-query, return it directly
if (queryList.size() == 1) {
return queryList.get(0);
}
// We have multiple subqueries, so let's combine them into a
// BooleanQuery.
//
// If the subquery is a BooleanQuery, we pull out each clause and
// add it to the outer BooleanQuery so that any negations work on
// the query as a whole. One exception to this rule: If the query
// contains one or more Occur.SHOULD clauses and no Occur.MUST
// clauses, we keep it in a subquery so that the requirement that
// at least one of the Occur.SHOULD clauses must match (pulling them
// out would make all of them optional).
//
// All other types of subqueries are added directly to the outer
// query with Occur.MUST.
BooleanQuery.Builder combinedQuery = new BooleanQuery.Builder();
for (Query query : queryList) {
if (query instanceof BooleanQuery) {
BooleanQuery boolQuery = (BooleanQuery) query;
if (hasClause(boolQuery, Occur.SHOULD) && !hasClause(boolQuery, Occur.MUST)) {
combinedQuery.add(query, Occur.MUST);
} else {
for (BooleanClause clause : boolQuery) {
combinedQuery.add(clause);
}
}
} else {
combinedQuery.add(query, Occur.MUST);
}
}
return combinedQuery.build();
}
use of org.apache.lucene.search.BooleanClause in project incubator-atlas by apache.
the class BooleanQueryExpression method processOrClauses.
private Collection<Pipe> processOrClauses(Map<BooleanClause.Occur, Collection<BooleanClause>> groupedClauses) {
Collection<BooleanClause> shouldClauses = groupedClauses.get(BooleanClause.Occur.SHOULD);
Collection<Pipe> orPipes = new ArrayList<>();
if (shouldClauses != null) {
for (BooleanClause shouldClause : shouldClauses) {
QueryExpression queryExpression = queryFactory.create(shouldClause.getQuery(), resourceDefinition);
// don't negate expression if we negated MUST_NOT -> SHOULD
if (negate && shouldClause.getOccur() != BooleanClause.Occur.MUST_NOT) {
queryExpression.setNegate();
}
properties.addAll(queryExpression.getProperties());
orPipes.add(queryExpression.asPipe());
}
}
return orPipes;
}
use of org.apache.lucene.search.BooleanClause in project jackrabbit by apache.
the class JackrabbitQueryParser method getSynonymQuery.
/**
* Factory method for generating a synonym query.
* Called when parser parses an input term token that has the synonym
* prefix (~term) prepended.
*
* @param field Name of the field query will use.
* @param termStr Term token to use for building term for the query
*
* @return Resulting {@link Query} built for the term
* @exception ParseException throw in overridden method to disallow
*/
protected Query getSynonymQuery(String field, String termStr) throws ParseException {
List<BooleanClause> synonyms = new ArrayList<BooleanClause>();
synonyms.add(new BooleanClause(getFieldQuery(field, termStr), BooleanClause.Occur.SHOULD));
if (synonymProvider != null) {
for (String term : synonymProvider.getSynonyms(termStr)) {
synonyms.add(new BooleanClause(getFieldQuery(field, term), BooleanClause.Occur.SHOULD));
}
}
if (synonyms.size() == 1) {
return synonyms.get(0).getQuery();
} else {
return getBooleanQuery(synonyms);
}
}
use of org.apache.lucene.search.BooleanClause in project zm-mailbox by Zimbra.
the class LuceneQueryOperation method addAndedClause.
/**
* Adds the specified text clause ANDED with the existing query.
* <p>
* e.g. going in w/ "a b c" if we addAndedClause("d") we get "(a b c) AND d".
* <p>
* This API may only be called AFTER query optimizing and AFTER remote queries have been split.
* <p>
* Note that this API does *not* update the text-representation of this query.
*/
void addAndedClause(Query query, boolean bool) {
assert (luceneQuery != null);
// will need to re-run the search with this new clause
haveRunSearch = false;
curHitNo = 0;
if (luceneQuery instanceof BooleanQuery) {
BooleanQuery bquery = ((BooleanQuery) luceneQuery);
boolean orOnly = true;
for (BooleanClause clause : bquery) {
if (clause.getOccur() != BooleanClause.Occur.SHOULD) {
orOnly = false;
break;
}
}
if (!orOnly) {
bquery.add(new BooleanClause(query, bool ? BooleanClause.Occur.MUST : BooleanClause.Occur.MUST_NOT));
return;
}
}
BooleanQuery bquery = new BooleanQuery();
bquery.add(new BooleanClause(luceneQuery, BooleanClause.Occur.MUST));
bquery.add(new BooleanClause(query, bool ? BooleanClause.Occur.MUST : BooleanClause.Occur.MUST_NOT));
luceneQuery = bquery;
}
Aggregations