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();
}
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);
}
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("/"));
}
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();
}
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);
}
Aggregations