Search in sources :

Example 66 with BooleanClause

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;
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermFieldEnumeration(com.zimbra.cs.index.ZimbraIndexReader.TermFieldEnumeration) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) Term(org.apache.lucene.index.Term) ListIterator(java.util.ListIterator)

Example 67 with BooleanClause

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));
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) FulltextQueryTermsProvider(org.apache.jackrabbit.oak.plugins.index.lucene.spi.FulltextQueryTermsProvider)

Example 68 with BooleanClause

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();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) PointInSetQuery(org.apache.lucene.search.PointInSetQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) TermQuery(org.apache.lucene.search.TermQuery) FilterQuery(org.apache.solr.query.FilterQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) HashMap(java.util.HashMap) PointInSetQuery(org.apache.lucene.search.PointInSetQuery) FilterQuery(org.apache.solr.query.FilterQuery) BooleanClause(org.apache.lucene.search.BooleanClause) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) MapSolrParams(org.apache.solr.common.params.MapSolrParams) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) Test(org.junit.Test)

Example 69 with BooleanClause

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;
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause)

Example 70 with BooleanClause

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);
        }
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) PropertyRestriction(org.apache.jackrabbit.oak.spi.query.Filter.PropertyRestriction) PlanResult(org.apache.jackrabbit.oak.plugins.index.lucene.IndexPlanner.PlanResult) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) NumericRangeQuery(org.apache.lucene.search.NumericRangeQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Filter(org.apache.jackrabbit.oak.spi.query.Filter) Term(org.apache.lucene.index.Term) TermFactory.newAncestorTerm(org.apache.jackrabbit.oak.plugins.index.lucene.TermFactory.newAncestorTerm) TermFactory.newPathTerm(org.apache.jackrabbit.oak.plugins.index.lucene.TermFactory.newPathTerm) FullTextTerm(org.apache.jackrabbit.oak.query.fulltext.FullTextTerm)

Aggregations

BooleanClause (org.apache.lucene.search.BooleanClause)102 BooleanQuery (org.apache.lucene.search.BooleanQuery)92 Query (org.apache.lucene.search.Query)56 TermQuery (org.apache.lucene.search.TermQuery)46 Term (org.apache.lucene.index.Term)44 BoostQuery (org.apache.lucene.search.BoostQuery)33 ArrayList (java.util.ArrayList)23 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)21 PhraseQuery (org.apache.lucene.search.PhraseQuery)20 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)19 SynonymQuery (org.apache.lucene.search.SynonymQuery)18 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)18 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)17 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)16 SpanQuery (org.apache.lucene.search.spans.SpanQuery)15 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)14 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)14 WildcardQuery (org.apache.lucene.search.WildcardQuery)14 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)13 PrefixQuery (org.apache.lucene.search.PrefixQuery)13