Search in sources :

Example 1 with FullTextVisitor

use of org.apache.jackrabbit.oak.spi.query.fulltext.FullTextVisitor in project jackrabbit-oak by apache.

the class AggregateIndex method flatten.

private Cursor flatten(FullTextExpression constraint, final AggregateIndexPlan plan, final Filter filter, final NodeState state, final String path) {
    if (constraint == null) {
        return null;
    }
    final AtomicReference<Cursor> result = new AtomicReference<Cursor>();
    constraint.accept(new FullTextVisitor() {

        @Override
        public boolean visit(FullTextContains contains) {
            return contains.getBase().accept(this);
        }

        @Override
        public boolean visit(FullTextTerm term) {
            IndexPlan p = plan.getPlan(path);
            result.set(newAggregationCursor(p, state));
            return true;
        }

        @Override
        public boolean visit(FullTextAnd and) {
            Iterator<FullTextExpression> iterator = and.list.iterator();
            int index = 0;
            Cursor c = flatten(iterator.next(), plan, filter, state, path + " and(" + index + ")");
            while (iterator.hasNext()) {
                index++;
                FullTextExpression input = iterator.next();
                Cursor newC = flatten(input, plan, filter, state, path + " and(" + index + ")");
                c = Cursors.newIntersectionCursor(c, newC, filter.getQueryLimits());
            }
            result.set(c);
            return true;
        }

        @Override
        public boolean visit(FullTextOr or) {
            final int[] index = new int[1];
            List<Cursor> cursors = Lists.transform(or.list, new Function<FullTextExpression, Cursor>() {

                @Override
                public Cursor apply(FullTextExpression input) {
                    return flatten(input, plan, filter, state, path + " or(" + index[0]++ + ")");
                }
            });
            result.set(Cursors.newConcatCursor(cursors, filter.getQueryLimits()));
            return true;
        }
    });
    return result.get();
}
Also used : FullTextOr(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextOr) FullTextContains(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextContains) AtomicReference(java.util.concurrent.atomic.AtomicReference) Cursor(org.apache.jackrabbit.oak.spi.query.Cursor) Function(com.google.common.base.Function) FullTextVisitor(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextVisitor) FullTextTerm(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextTerm) FullTextAnd(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextAnd) Iterator(java.util.Iterator) FullTextExpression(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextExpression) List(java.util.List)

Example 2 with FullTextVisitor

use of org.apache.jackrabbit.oak.spi.query.fulltext.FullTextVisitor in project jackrabbit-oak by apache.

the class LucenePropertyIndex method getFullTextQuery.

static Query getFullTextQuery(final IndexPlan plan, FullTextExpression ft, final Analyzer analyzer, final FulltextQueryTermsProvider augmentor) {
    final PlanResult pr = getPlanResult(plan);
    // a reference to the query, so it can be set in the visitor
    // (a "non-local return")
    final AtomicReference<Query> result = new AtomicReference<Query>();
    ft.accept(new FullTextVisitor() {

        @Override
        public boolean visit(FullTextContains contains) {
            visitTerm(contains.getPropertyName(), contains.getRawText(), null, contains.isNot());
            return true;
        }

        @Override
        public boolean visit(FullTextOr or) {
            BooleanQuery q = new BooleanQuery();
            for (FullTextExpression e : or.list) {
                Query x = getFullTextQuery(plan, e, analyzer, augmentor);
                q.add(x, SHOULD);
            }
            result.set(q);
            return true;
        }

        @Override
        public boolean visit(FullTextAnd and) {
            BooleanQuery q = new BooleanQuery();
            for (FullTextExpression e : and.list) {
                Query x = getFullTextQuery(plan, e, analyzer, augmentor);
                /* Only unwrap the clause if MUST_NOT(x) */
                boolean hasMustNot = false;
                if (x instanceof BooleanQuery) {
                    BooleanQuery bq = (BooleanQuery) x;
                    if ((bq.getClauses().length == 1) && (bq.getClauses()[0].getOccur() == BooleanClause.Occur.MUST_NOT)) {
                        hasMustNot = true;
                        q.add(bq.getClauses()[0]);
                    }
                }
                if (!hasMustNot) {
                    q.add(x, MUST);
                }
            }
            result.set(q);
            return true;
        }

        @Override
        public boolean visit(FullTextTerm term) {
            return visitTerm(term.getPropertyName(), term.getText(), term.getBoost(), term.isNot());
        }

        private boolean visitTerm(String propertyName, String text, String boost, boolean not) {
            String p = getLuceneFieldName(propertyName, pr);
            Query q = tokenToQuery(text, p, pr, analyzer, augmentor);
            if (q == null) {
                return false;
            }
            if (boost != null) {
                q.setBoost(Float.parseFloat(boost));
            }
            if (not) {
                BooleanQuery bq = new BooleanQuery();
                bq.add(q, MUST_NOT);
                result.set(bq);
            } else {
                result.set(q);
            }
            return true;
        }
    });
    return result.get();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) 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) FullTextOr(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextOr) FullTextContains(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextContains) AtomicReference(java.util.concurrent.atomic.AtomicReference) FullTextVisitor(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextVisitor) FullTextTerm(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextTerm) FullTextAnd(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextAnd) FullTextExpression(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextExpression)

Example 3 with FullTextVisitor

use of org.apache.jackrabbit.oak.spi.query.fulltext.FullTextVisitor in project jackrabbit-oak by apache.

the class FilterQueryParser method parseFullTextExpression.

private static String parseFullTextExpression(FullTextExpression ft, final OakSolrConfiguration configuration) {
    final StringBuilder fullTextString = new StringBuilder();
    ft.accept(new FullTextVisitor() {

        @Override
        public boolean visit(FullTextOr or) {
            fullTextString.append('(');
            for (int i = 0; i < or.list.size(); i++) {
                FullTextExpression e = or.list.get(i);
                if (e.toString().contains("\"OR\"")) {
                    continue;
                }
                if (i > 0 && i < or.list.size()) {
                    fullTextString.append(" OR ");
                }
                String orTerm = parseFullTextExpression(e, configuration);
                fullTextString.append(orTerm);
            }
            fullTextString.append(')');
            fullTextString.append(' ');
            return true;
        }

        @Override
        public boolean visit(FullTextContains contains) {
            return contains.getBase().accept(this);
        }

        @Override
        public boolean visit(FullTextAnd and) {
            fullTextString.append('(');
            for (int i = 0; i < and.list.size(); i++) {
                FullTextExpression e = and.list.get(i);
                if (e.toString().contains("\"AND\"")) {
                    continue;
                }
                if (i > 0 && i < and.list.size()) {
                    fullTextString.append(" AND ");
                }
                String andTerm = parseFullTextExpression(e, configuration);
                fullTextString.append(andTerm);
            }
            fullTextString.append(')');
            fullTextString.append(' ');
            return true;
        }

        @Override
        public boolean visit(FullTextTerm term) {
            if (term.isNot()) {
                fullTextString.append('-');
            }
            String p = term.getPropertyName();
            if (p != null && p.indexOf('/') >= 0) {
                p = getName(p);
            }
            if (p == null || "*".equals(p)) {
                p = configuration.getCatchAllField();
            }
            fullTextString.append(partialEscape(p));
            fullTextString.append(':');
            String termText = term.getText();
            if (termText.indexOf(' ') > 0) {
                fullTextString.append('"');
            }
            fullTextString.append(termText.replace("/", "\\/").replace(":", "\\:"));
            if (termText.indexOf(' ') > 0) {
                fullTextString.append('"');
            }
            String boost = term.getBoost();
            if (boost != null) {
                fullTextString.append('^');
                fullTextString.append(boost);
            }
            fullTextString.append(' ');
            return true;
        }
    });
    return fullTextString.toString();
}
Also used : FullTextVisitor(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextVisitor) FullTextTerm(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextTerm) FullTextOr(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextOr) FullTextContains(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextContains) FullTextAnd(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextAnd) FullTextExpression(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextExpression)

Example 4 with FullTextVisitor

use of org.apache.jackrabbit.oak.spi.query.fulltext.FullTextVisitor in project jackrabbit-oak by apache.

the class AggregateIndex method hasCompositeExpression.

private static boolean hasCompositeExpression(FullTextExpression ft) {
    if (ft == null) {
        return false;
    }
    final AtomicReference<Boolean> composite = new AtomicReference<Boolean>();
    composite.set(false);
    ft.accept(new FullTextVisitor() {

        @Override
        public boolean visit(FullTextContains contains) {
            return contains.getBase().accept(this);
        }

        @Override
        public boolean visit(FullTextTerm term) {
            return true;
        }

        @Override
        public boolean visit(FullTextAnd and) {
            composite.set(true);
            return true;
        }

        @Override
        public boolean visit(FullTextOr or) {
            composite.set(true);
            return true;
        }
    });
    return composite.get() && !hasNegativeContains(ft);
}
Also used : FullTextVisitor(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextVisitor) FullTextTerm(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextTerm) FullTextOr(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextOr) FullTextContains(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextContains) FullTextAnd(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextAnd) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 5 with FullTextVisitor

use of org.apache.jackrabbit.oak.spi.query.fulltext.FullTextVisitor in project jackrabbit-oak by apache.

the class LuceneIndex method getFullTextQuery.

static Query getFullTextQuery(FullTextExpression ft, final Analyzer analyzer, final IndexReader reader) {
    // a reference to the query, so it can be set in the visitor
    // (a "non-local return")
    final AtomicReference<Query> result = new AtomicReference<Query>();
    ft.accept(new FullTextVisitor() {

        @Override
        public boolean visit(FullTextContains contains) {
            return contains.getBase().accept(this);
        }

        @Override
        public boolean visit(FullTextOr or) {
            BooleanQuery q = new BooleanQuery();
            for (FullTextExpression e : or.list) {
                Query x = getFullTextQuery(e, analyzer, reader);
                q.add(x, SHOULD);
            }
            result.set(q);
            return true;
        }

        @Override
        public boolean visit(FullTextAnd and) {
            BooleanQuery q = new BooleanQuery();
            for (FullTextExpression e : and.list) {
                Query x = getFullTextQuery(e, analyzer, reader);
                /* Only unwrap the clause if MUST_NOT(x) */
                boolean hasMustNot = false;
                if (x instanceof BooleanQuery) {
                    BooleanQuery bq = (BooleanQuery) x;
                    if ((bq.getClauses().length == 1) && (bq.getClauses()[0].getOccur() == BooleanClause.Occur.MUST_NOT)) {
                        hasMustNot = true;
                        q.add(bq.getClauses()[0]);
                    }
                }
                if (!hasMustNot) {
                    q.add(x, MUST);
                }
            }
            result.set(q);
            return true;
        }

        @Override
        public boolean visit(FullTextTerm term) {
            return visitTerm(term.getPropertyName(), term.getText(), term.getBoost(), term.isNot());
        }

        private boolean visitTerm(String propertyName, String text, String boost, boolean not) {
            String p = propertyName;
            if (p != null && p.indexOf('/') >= 0) {
                p = getName(p);
            }
            Query q = tokenToQuery(text, p, analyzer, reader);
            if (q == null) {
                return false;
            }
            if (boost != null) {
                q.setBoost(Float.parseFloat(boost));
            }
            if (not) {
                BooleanQuery bq = new BooleanQuery();
                bq.add(q, MUST_NOT);
                result.set(bq);
            } else {
                result.set(q);
            }
            return true;
        }
    });
    return result.get();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) FullTextOr(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextOr) FullTextContains(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextContains) AtomicReference(java.util.concurrent.atomic.AtomicReference) FullTextVisitor(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextVisitor) FullTextTerm(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextTerm) FullTextAnd(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextAnd) FullTextExpression(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextExpression)

Aggregations

FullTextAnd (org.apache.jackrabbit.oak.spi.query.fulltext.FullTextAnd)5 FullTextContains (org.apache.jackrabbit.oak.spi.query.fulltext.FullTextContains)5 FullTextOr (org.apache.jackrabbit.oak.spi.query.fulltext.FullTextOr)5 FullTextTerm (org.apache.jackrabbit.oak.spi.query.fulltext.FullTextTerm)5 FullTextVisitor (org.apache.jackrabbit.oak.spi.query.fulltext.FullTextVisitor)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 FullTextExpression (org.apache.jackrabbit.oak.spi.query.fulltext.FullTextExpression)4 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 PrefixQuery (org.apache.lucene.search.PrefixQuery)2 Query (org.apache.lucene.search.Query)2 TermQuery (org.apache.lucene.search.TermQuery)2 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)2 WildcardQuery (org.apache.lucene.search.WildcardQuery)2 Function (com.google.common.base.Function)1 Iterator (java.util.Iterator)1 List (java.util.List)1 PlanResult (org.apache.jackrabbit.oak.plugins.index.lucene.IndexPlanner.PlanResult)1 Cursor (org.apache.jackrabbit.oak.spi.query.Cursor)1 CustomScoreQuery (org.apache.lucene.queries.CustomScoreQuery)1