Search in sources :

Example 1 with FullTextTerm

use of org.apache.jackrabbit.oak.query.fulltext.FullTextTerm 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.getQueryEngineSettings());
            }
            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.getQueryEngineSettings()));
            return true;
        }
    });
    return result.get();
}
Also used : FullTextOr(org.apache.jackrabbit.oak.query.fulltext.FullTextOr) FullTextContains(org.apache.jackrabbit.oak.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.query.fulltext.FullTextVisitor) FullTextTerm(org.apache.jackrabbit.oak.query.fulltext.FullTextTerm) FullTextAnd(org.apache.jackrabbit.oak.query.fulltext.FullTextAnd) Iterator(java.util.Iterator) FullTextExpression(org.apache.jackrabbit.oak.query.fulltext.FullTextExpression) List(java.util.List)

Example 2 with FullTextTerm

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

the class LMSEstimatorTest method testMultipleUpdates.

@Test
public void testMultipleUpdates() throws Exception {
    LMSEstimator lmsEstimator = new LMSEstimator();
    Filter filter = mock(Filter.class);
    FullTextExpression fte = new FullTextTerm("foo", "bar", false, false, "");
    when(filter.getFullTextConstraint()).thenReturn(fte);
    SolrDocumentList docs = new SolrDocumentList();
    lmsEstimator.update(filter, docs);
    long actualCount = 10;
    docs.setNumFound(actualCount);
    long estimate = lmsEstimator.estimate(filter);
    long diff = actualCount - estimate;
    // update causes weights adjustment
    lmsEstimator.update(filter, docs);
    long estimate2 = lmsEstimator.estimate(filter);
    long diff2 = actualCount - estimate2;
    // new estimate is more accurate than previous one
    assertTrue(diff2 < diff);
    // update doesn't cause weight adjustments therefore estimates stays unchanged
    lmsEstimator.update(filter, docs);
    long estimate3 = lmsEstimator.estimate(filter);
    assertEquals(estimate3, estimate2);
}
Also used : FullTextTerm(org.apache.jackrabbit.oak.query.fulltext.FullTextTerm) Filter(org.apache.jackrabbit.oak.spi.query.Filter) FullTextExpression(org.apache.jackrabbit.oak.query.fulltext.FullTextExpression) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Test(org.junit.Test)

Example 3 with FullTextTerm

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

the class LuceneIndexTest method analyzerWithStopWords.

@Test
public void analyzerWithStopWords() throws Exception {
    NodeBuilder nb = newLuceneIndexDefinition(builder.child(INDEX_DEFINITIONS_NAME), "lucene", of(TYPENAME_STRING));
    TestUtil.useV2(nb);
    NodeState before = builder.getNodeState();
    builder.setProperty("foo", "fox jumping");
    NodeState after = builder.getNodeState();
    NodeState indexed = HOOK.processCommit(before, after, CommitInfo.EMPTY);
    tracker = new IndexTracker();
    tracker.update(indexed);
    AdvancedQueryIndex queryIndex = new LucenePropertyIndex(tracker);
    FilterImpl filter = createFilter("nt:base");
    filter.setFullTextConstraint(new FullTextTerm(null, "fox jumping", false, false, null));
    assertFilter(filter, queryIndex, indexed, ImmutableList.of("/"));
    //No stop word configured so default analyzer would also check for 'was'
    filter.setFullTextConstraint(new FullTextTerm(null, "fox was jumping", false, false, null));
    assertFilter(filter, queryIndex, indexed, Collections.<String>emptyList());
    //Change the default analyzer to use the default stopword set
    //and trigger a reindex such that new analyzer is used
    NodeBuilder anlnb = nb.child(ANALYZERS).child(ANL_DEFAULT);
    anlnb.child(ANL_TOKENIZER).setProperty(ANL_NAME, "whitespace");
    anlnb.child(ANL_FILTERS).child("stop");
    nb.setProperty(IndexConstants.REINDEX_PROPERTY_NAME, true);
    before = after;
    after = builder.getNodeState();
    indexed = HOOK.processCommit(before, after, CommitInfo.EMPTY);
    tracker.update(indexed);
    queryIndex = new LucenePropertyIndex(tracker);
    filter.setFullTextConstraint(new FullTextTerm(null, "fox jumping", false, false, null));
    assertFilter(filter, queryIndex, indexed, ImmutableList.of("/"));
    //Now this should get passed as the analyzer would ignore 'was'
    filter.setFullTextConstraint(new FullTextTerm(null, "fox was jumping", false, false, null));
    assertFilter(filter, queryIndex, indexed, ImmutableList.of("/"));
}
Also used : AdvancedQueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) FilterImpl(org.apache.jackrabbit.oak.query.index.FilterImpl) FullTextTerm(org.apache.jackrabbit.oak.query.fulltext.FullTextTerm) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) Test(org.junit.Test)

Example 4 with FullTextTerm

use of org.apache.jackrabbit.oak.query.fulltext.FullTextTerm 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.query.fulltext.FullTextOr) FullTextContains(org.apache.jackrabbit.oak.query.fulltext.FullTextContains) AtomicReference(java.util.concurrent.atomic.AtomicReference) FullTextVisitor(org.apache.jackrabbit.oak.query.fulltext.FullTextVisitor) FullTextTerm(org.apache.jackrabbit.oak.query.fulltext.FullTextTerm) FullTextAnd(org.apache.jackrabbit.oak.query.fulltext.FullTextAnd) FullTextExpression(org.apache.jackrabbit.oak.query.fulltext.FullTextExpression)

Example 5 with FullTextTerm

use of org.apache.jackrabbit.oak.query.fulltext.FullTextTerm 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.query.fulltext.FullTextVisitor) FullTextTerm(org.apache.jackrabbit.oak.query.fulltext.FullTextTerm) FullTextOr(org.apache.jackrabbit.oak.query.fulltext.FullTextOr) FullTextContains(org.apache.jackrabbit.oak.query.fulltext.FullTextContains) FullTextAnd(org.apache.jackrabbit.oak.query.fulltext.FullTextAnd) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Aggregations

FullTextTerm (org.apache.jackrabbit.oak.query.fulltext.FullTextTerm)9 FullTextContains (org.apache.jackrabbit.oak.query.fulltext.FullTextContains)6 FullTextVisitor (org.apache.jackrabbit.oak.query.fulltext.FullTextVisitor)6 FullTextAnd (org.apache.jackrabbit.oak.query.fulltext.FullTextAnd)5 FullTextExpression (org.apache.jackrabbit.oak.query.fulltext.FullTextExpression)5 FullTextOr (org.apache.jackrabbit.oak.query.fulltext.FullTextOr)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Query (org.apache.lucene.search.Query)3 Test (org.junit.Test)3 FilterImpl (org.apache.jackrabbit.oak.query.index.FilterImpl)2 AdvancedQueryIndex (org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex)2 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)2 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)2 CustomScoreQuery (org.apache.lucene.queries.CustomScoreQuery)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 PrefixQuery (org.apache.lucene.search.PrefixQuery)2 TermQuery (org.apache.lucene.search.TermQuery)2 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)2 WildcardQuery (org.apache.lucene.search.WildcardQuery)2