use of org.apache.lucene.search.BooleanClause in project zm-mailbox by Zimbra.
the class LuceneQueryOperation method expandLazyMultiPhraseQuery.
private Query expandLazyMultiPhraseQuery(Query query) throws IOException {
if (query instanceof LazyMultiPhraseQuery) {
LazyMultiPhraseQuery lazy = (LazyMultiPhraseQuery) query;
int max = LC.zimbra_index_wildcard_max_terms_expanded.intValue();
MultiPhraseQuery mquery = new MultiPhraseQuery();
for (Term[] terms : lazy.getTermArrays()) {
if (terms.length != 1) {
mquery.add(terms);
continue;
}
Term base = terms[0];
if (!lazy.expand.contains(base)) {
mquery.add(terms);
continue;
}
List<Term> expanded = Lists.newArrayList();
TermFieldEnumeration itr = searcher.getIndexReader().getTermsForField(base.field(), base.text());
try {
while (itr.hasMoreElements()) {
BrowseTerm term = itr.nextElement();
if (term != null && term.getText().startsWith(base.text())) {
if (expanded.size() >= max) {
// too many terms expanded
break;
}
expanded.add(new Term(base.field(), term.getText()));
} else {
break;
}
}
} finally {
Closeables.closeQuietly(itr);
}
if (expanded.isEmpty()) {
return null;
} else {
mquery.add(expanded.toArray(new Term[expanded.size()]));
}
}
return mquery;
} else if (query instanceof BooleanQuery) {
ListIterator<BooleanClause> itr = ((BooleanQuery) query).clauses().listIterator();
while (itr.hasNext()) {
BooleanClause clause = itr.next();
Query result = expandLazyMultiPhraseQuery(clause.getQuery());
if (result == null) {
if (clause.isRequired()) {
return null;
} else {
itr.remove();
}
} else if (result != clause.getQuery()) {
clause.setQuery(result);
}
}
return ((BooleanQuery) query).clauses().isEmpty() ? null : query;
} else {
return query;
}
}
use of org.apache.lucene.search.BooleanClause in project jackrabbit-oak by apache.
the class IndexAugmentorFactoryTest method validateComposedQueryTerms.
void validateComposedQueryTerms(String type, String... expected) {
FulltextQueryTermsProvider compositeQueryTermsProvider = indexAugmentorFactory.getFulltextQueryTermsProvider(type);
if (expected.length > 0) {
assertTrue("Composed query terms provider doesn't declare correct supported type", compositeQueryTermsProvider.getSupportedTypes().contains(type));
}
Query q = compositeQueryTermsProvider.getQueryTerm(null, null, null);
if (q == null) {
assertEquals("No query terms generated for " + type + ".", 0, expected.length);
} else {
Set<String> ids = Sets.newHashSet();
if (q instanceof BooleanQuery) {
BooleanQuery query = (BooleanQuery) q;
List<BooleanClause> clauses = query.clauses();
for (BooleanClause clause : clauses) {
assertEquals(SHOULD, clause.getOccur());
Query subQuery = clause.getQuery();
String subQueryStr = subQuery.toString();
ids.add(subQueryStr.substring(0, subQueryStr.indexOf(":1")));
}
} else {
String subQueryStr = q.toString();
ids.add(subQueryStr.substring(0, subQueryStr.indexOf(":1")));
}
assertEquals(expected.length, Iterables.size(ids));
assertThat(ids, CoreMatchers.hasItems(expected));
}
}
use of org.apache.lucene.search.BooleanClause in project lucene-solr by apache.
the class TestSolrQueryParser method testAutoTerms.
// automatically use TermsQuery when appropriate
@Test
public void testAutoTerms() throws Exception {
SolrQueryRequest req = req();
QParser qParser;
Query q, qq;
Map<String, String> sowFalseParamsMap = new HashMap<>();
sowFalseParamsMap.put("sow", "false");
Map<String, String> sowTrueParamsMap = new HashMap<>();
sowTrueParamsMap.put("sow", "true");
List<MapSolrParams> paramMaps = Arrays.asList(// no sow param (i.e. the default sow value)
new MapSolrParams(Collections.emptyMap()), new MapSolrParams(sowFalseParamsMap), new MapSolrParams(sowTrueParamsMap));
for (MapSolrParams params : paramMaps) {
// relevance query should not be a filter
qParser = QParser.getParser("foo_s:(a b c)", req);
qParser.setParams(params);
q = qParser.getQuery();
assertEquals(3, ((BooleanQuery) q).clauses().size());
// small filter query should still use BooleanQuery
if (QueryParser.TERMS_QUERY_THRESHOLD > 3) {
qParser = QParser.getParser("foo_s:(a b c)", req);
qParser.setParams(params);
// this may change in the future
qParser.setIsFilter(true);
q = qParser.getQuery();
assertEquals(3, ((BooleanQuery) q).clauses().size());
}
// large relevancy query should use BooleanQuery
// TODO: we may decide that string fields shouldn't have relevance in the future... change to a text field w/o a stop filter if so
qParser = QParser.getParser("foo_s:(a b c d e f g h i j k l m n o p q r s t u v w x y z)", req);
qParser.setParams(params);
q = qParser.getQuery();
assertEquals(26, ((BooleanQuery) q).clauses().size());
// large filter query should use TermsQuery
qParser = QParser.getParser("foo_s:(a b c d e f g h i j k l m n o p q r s t u v w x y z)", req);
// this may change in the future
qParser.setIsFilter(true);
qParser.setParams(params);
q = qParser.getQuery();
assertEquals(26, ((TermInSetQuery) q).getTermData().size());
// large numeric filter query should use TermsQuery (for trie fields)
qParser = QParser.getParser("foo_ti:(1 2 3 4 5 6 7 8 9 10 20 19 18 17 16 15 14 13 12 11)", req);
// this may change in the future
qParser.setIsFilter(true);
qParser.setParams(params);
q = qParser.getQuery();
assertEquals(20, ((TermInSetQuery) q).getTermData().size());
// for point fields large filter query should use PointInSetQuery
qParser = QParser.getParser("foo_pi:(1 2 3 4 5 6 7 8 9 10 20 19 18 17 16 15 14 13 12 11)", req);
// this may change in the future
qParser.setIsFilter(true);
qParser.setParams(params);
q = qParser.getQuery();
assertTrue(q instanceof PointInSetQuery);
assertEquals(20, ((PointInSetQuery) q).getPackedPoints().size());
// a filter() clause inside a relevancy query should be able to use a TermsQuery
qParser = QParser.getParser("foo_s:aaa filter(foo_s:(a b c d e f g h i j k l m n o p q r s t u v w x y z))", req);
qParser.setParams(params);
q = qParser.getQuery();
assertEquals(2, ((BooleanQuery) q).clauses().size());
qq = ((BooleanQuery) q).clauses().get(0).getQuery();
if (qq instanceof TermQuery) {
qq = ((BooleanQuery) q).clauses().get(1).getQuery();
}
if (qq instanceof FilterQuery) {
qq = ((FilterQuery) qq).getQuery();
}
assertEquals(26, ((TermInSetQuery) qq).getTermData().size());
// test mixed boolean query, including quotes (which shouldn't matter)
qParser = QParser.getParser("foo_s:(a +aaa b -bbb c d e f bar_s:(qqq www) g h i j k l m n o p q r s t u v w x y z)", req);
// this may change in the future
qParser.setIsFilter(true);
qParser.setParams(params);
q = qParser.getQuery();
assertEquals(4, ((BooleanQuery) q).clauses().size());
qq = null;
for (BooleanClause clause : ((BooleanQuery) q).clauses()) {
qq = clause.getQuery();
if (qq instanceof TermInSetQuery)
break;
}
assertEquals(26, ((TermInSetQuery) qq).getTermData().size());
// test terms queries of two different fields (LUCENE-7637 changed to require all terms be in the same field)
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 17; i++) {
char letter = (char) ('a' + i);
sb.append("foo_s:" + letter + " bar_s:" + letter + " ");
}
qParser = QParser.getParser(sb.toString(), req);
// this may change in the future
qParser.setIsFilter(true);
qParser.setParams(params);
q = qParser.getQuery();
assertEquals(2, ((BooleanQuery) q).clauses().size());
for (BooleanClause clause : ((BooleanQuery) q).clauses()) {
qq = clause.getQuery();
assertEquals(17, ((TermInSetQuery) qq).getTermData().size());
}
}
req.close();
}
use of org.apache.lucene.search.BooleanClause in project jackrabbit-oak by apache.
the class LucenePropertyIndex method unwrapMustNot.
/**
* unwraps any NOT clauses from the provided boolean query into another boolean query.
*
* @param input the query to be analysed for the existence of NOT clauses. Cannot be null.
* @param output the query where the unwrapped NOTs will be saved into. Cannot be null.
* @return true if there where at least one unwrapped NOT. false otherwise.
*/
private static boolean unwrapMustNot(@Nonnull BooleanQuery input, @Nonnull BooleanQuery output) {
checkNotNull(input);
checkNotNull(output);
boolean unwrapped = false;
for (BooleanClause bc : input.getClauses()) {
if (bc.getOccur() == BooleanClause.Occur.MUST_NOT) {
output.add(bc);
unwrapped = true;
}
}
return unwrapped;
}
use of org.apache.lucene.search.BooleanClause in project jackrabbit-oak by apache.
the class LucenePropertyIndex method addNonFullTextConstraints.
private static void addNonFullTextConstraints(List<Query> qs, IndexPlan plan, IndexReader reader) {
Filter filter = plan.getFilter();
PlanResult planResult = getPlanResult(plan);
IndexDefinition defn = planResult.indexDefinition;
if (!filter.matchesAllTypes()) {
addNodeTypeConstraints(planResult.indexingRule, qs, filter);
}
String path = getPathRestriction(plan);
switch(filter.getPathRestriction()) {
case ALL_CHILDREN:
if (defn.evaluatePathRestrictions()) {
if ("/".equals(path)) {
break;
}
qs.add(new TermQuery(newAncestorTerm(path)));
}
break;
case DIRECT_CHILDREN:
if (defn.evaluatePathRestrictions()) {
BooleanQuery bq = new BooleanQuery();
bq.add(new BooleanClause(new TermQuery(newAncestorTerm(path)), BooleanClause.Occur.MUST));
bq.add(new BooleanClause(newDepthQuery(path), BooleanClause.Occur.MUST));
qs.add(bq);
}
break;
case EXACT:
qs.add(new TermQuery(newPathTerm(path)));
break;
case PARENT:
if (denotesRoot(path)) {
// there's no parent of the root node
// we add a path that can not possibly occur because there
// is no way to say "match no documents" in Lucene
qs.add(new TermQuery(new Term(FieldNames.PATH, "///")));
} else {
qs.add(new TermQuery(newPathTerm(getParentPath(path))));
}
break;
case NO_RESTRICTION:
break;
}
for (PropertyRestriction pr : filter.getPropertyRestrictions()) {
String name = pr.propertyName;
if (QueryImpl.REP_EXCERPT.equals(name) || QueryImpl.OAK_SCORE_EXPLANATION.equals(name) || QueryImpl.REP_FACET.equals(name)) {
continue;
}
if (QueryConstants.RESTRICTION_LOCAL_NAME.equals(name)) {
if (planResult.evaluateNodeNameRestriction()) {
Query q = createNodeNameQuery(pr);
if (q != null) {
qs.add(q);
}
}
continue;
}
if (pr.first != null && pr.first.equals(pr.last) && pr.firstIncluding && pr.lastIncluding) {
String first = pr.first.getValue(STRING);
first = first.replace("\\", "");
if (JCR_PATH.equals(name)) {
qs.add(new TermQuery(newPathTerm(first)));
continue;
} else if ("*".equals(name)) {
//TODO Revisit reference constraint. For performant impl
//references need to be indexed in a different manner
addReferenceConstraint(first, qs, reader);
continue;
}
}
PropertyDefinition pd = planResult.getPropDefn(pr);
if (pd == null) {
continue;
}
Query q = createQuery(pr, pd);
if (q != null) {
qs.add(q);
}
}
}
Aggregations