use of org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex in project jackrabbit-oak by apache.
the class LuceneIndexTest method testPropertyNonExistence.
@Test
public void testPropertyNonExistence() throws Exception {
root = TestUtil.registerTestNodeType(builder).getNodeState();
NodeBuilder index = newLucenePropertyIndexDefinition(builder.child(INDEX_DEFINITIONS_NAME), "lucene", ImmutableSet.of("foo"), null);
NodeBuilder rules = index.child(INDEX_RULES);
NodeBuilder propNode = rules.child(NT_TEST).child(LuceneIndexConstants.PROP_NODE);
NodeBuilder fooProp = propNode.child("foo");
fooProp.setProperty(LuceneIndexConstants.PROP_PROPERTY_INDEX, true);
fooProp.setProperty(LuceneIndexConstants.PROP_NULL_CHECK_ENABLED, true);
NodeState before = builder.getNodeState();
createNodeWithType(builder, "a", NT_TEST).setProperty("foo", "bar");
createNodeWithType(builder, "b", NT_TEST).setProperty("foo", "bar");
createNodeWithType(builder, "c", NT_TEST);
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_TEST);
filter.restrictProperty("foo", Operator.EQUAL, null);
assertFilter(filter, queryIndex, indexed, ImmutableList.of("/c"));
}
use of org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex in project jackrabbit-oak by apache.
the class SelectorImpl method getIndexCostInfo.
@Override
public String getIndexCostInfo(NodeState rootState) {
StringBuilder buff = new StringBuilder();
buff.append(quoteJson(selectorName)).append(": ");
QueryIndex index = getIndex();
if (index != null) {
if (index instanceof AdvancedQueryIndex) {
IndexPlan p = plan.getIndexPlan();
buff.append("{ perEntry: ").append(p.getCostPerEntry());
buff.append(", perExecution: ").append(p.getCostPerExecution());
buff.append(", count: ").append(p.getEstimatedEntryCount());
buff.append(" }");
} else {
buff.append(index.getCost(createFilter(true), rootState));
}
}
return buff.toString();
}
use of org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex in project jackrabbit-oak by apache.
the class LuceneIndexTest method testCursorStability.
@Test
public void testCursorStability() throws Exception {
NodeBuilder index = newLucenePropertyIndexDefinition(builder.child(INDEX_DEFINITIONS_NAME), "lucene", ImmutableSet.of("foo"), null);
NodeBuilder rules = index.child(INDEX_RULES);
NodeBuilder fooProp = rules.child("nt:base").child(LuceneIndexConstants.PROP_NODE).child("foo");
fooProp.setProperty(LuceneIndexConstants.PROP_PROPERTY_INDEX, true);
//1. Create 60 nodes
NodeState before = builder.getNodeState();
int noOfDocs = LucenePropertyIndex.LUCENE_QUERY_BATCH_SIZE + 10;
for (int i = 0; i < noOfDocs; i++) {
builder.child("a" + i).setProperty("foo", (long) i);
}
NodeState after = builder.getNodeState();
NodeState indexed = HOOK.processCommit(before, after, CommitInfo.EMPTY);
tracker = new IndexTracker();
tracker.update(indexed);
//Perform query and get hold of cursor
AdvancedQueryIndex queryIndex = new LucenePropertyIndex(tracker);
FilterImpl filter = createFilter(NT_BASE);
filter.restrictProperty("foo", Operator.GREATER_OR_EQUAL, PropertyValues.newLong(0L));
List<IndexPlan> plans = queryIndex.getPlans(filter, null, indexed);
Cursor cursor = queryIndex.query(plans.get(0), indexed);
//Trigger loading of cursor
assertTrue(cursor.hasNext());
//Now before traversing further go ahead and delete all but 10 nodes
before = indexed;
builder = indexed.builder();
for (int i = 0; i < noOfDocs - 10; i++) {
builder.child("a" + i).remove();
}
after = builder.getNodeState();
indexed = HOOK.processCommit(before, after, CommitInfo.EMPTY);
builder = indexed.builder();
//Ensure that Lucene actually removes deleted docs
NodeBuilder idx = builder.child(INDEX_DEFINITIONS_NAME).child("lucene");
purgeDeletedDocs(idx, new IndexDefinition(root, idx.getNodeState(), "/foo"));
int numDeletes = getDeletedDocCount(idx, new IndexDefinition(root, idx.getNodeState(), "/foo"));
Assert.assertEquals(0, numDeletes);
//Update the IndexSearcher
tracker.update(builder.getNodeState());
//its hard to get correct size estimate as post deletion cursor
// would have already picked up 50 docs which would not be considered
//deleted by QE for the revision at which query was triggered
//So just checking for >
List<String> resultPaths = Lists.newArrayList();
while (cursor.hasNext()) {
resultPaths.add(cursor.next().getPath());
}
Set<String> uniquePaths = Sets.newHashSet(resultPaths);
assertEquals(resultPaths.size(), uniquePaths.size());
assertTrue(!uniquePaths.isEmpty());
}
use of org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex in project jackrabbit-oak by apache.
the class LuceneIndexTest method testLucene2.
@Test
public void testLucene2() throws Exception {
NodeBuilder index = builder.child(INDEX_DEFINITIONS_NAME);
newLucenePropertyIndexDefinition(index, "lucene", ImmutableSet.of("foo"), null);
NodeState before = builder.getNodeState();
builder.setProperty("foo", "bar");
builder.child("a").setProperty("foo", "bar");
builder.child("a").child("b").setProperty("foo", "bar");
builder.child("a").child("b").child("c").setProperty("foo", "bar");
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.restrictPath("/", Filter.PathRestriction.EXACT);
filter.restrictProperty("foo", Operator.EQUAL, PropertyValues.newString("bar"));
List<IndexPlan> plans = queryIndex.getPlans(filter, null, indexed);
Cursor cursor = queryIndex.query(plans.get(0), indexed);
assertTrue(cursor.hasNext());
assertEquals("/a/b/c", cursor.next().getPath());
assertEquals("/a/b", cursor.next().getPath());
assertEquals("/a", cursor.next().getPath());
assertEquals("/", cursor.next().getPath());
assertFalse(cursor.hasNext());
}
use of org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex in project jackrabbit-oak by apache.
the class LuceneIndexTest method customScoreQuery.
@Test
public void customScoreQuery() throws Exception {
NodeBuilder nb = newLuceneIndexDefinition(builder.child(INDEX_DEFINITIONS_NAME), "lucene", of(TYPENAME_STRING));
TestUtil.useV2(nb);
nb.setProperty(LuceneIndexConstants.PROP_SCORER_PROVIDER, "testScorer");
NodeState before = builder.getNodeState();
builder.child("a").setProperty("jcr:createdBy", "bar bar");
builder.child("b").setProperty("jcr:createdBy", "foo bar");
NodeState after = builder.getNodeState();
NodeState indexed = HOOK.processCommit(before, after, CommitInfo.EMPTY);
tracker = new IndexTracker();
tracker.update(indexed);
SimpleScorerFactory factory = new SimpleScorerFactory();
ScorerProvider provider = new ScorerProvider() {
String scorerName = "testScorer";
@Override
public String getName() {
return scorerName;
}
@Override
public CustomScoreQuery createCustomScoreQuery(Query query) {
return new ModifiedCustomScoreQuery(query);
}
class ModifiedCustomScoreQuery extends CustomScoreQuery {
private Query query;
public ModifiedCustomScoreQuery(Query query) {
super(query);
this.query = query;
}
@Override
public CustomScoreProvider getCustomScoreProvider(AtomicReaderContext context) {
return new CustomScoreProvider(context) {
public float customScore(int doc, float subQueryScore, float valSrcScore) {
AtomicReader atomicReader = context.reader();
try {
Document document = atomicReader.document(doc);
// boosting docs created by foo
String fieldValue = document.get("full:jcr:createdBy");
if (fieldValue != null && fieldValue.contains("foo")) {
valSrcScore *= 2.0;
}
} catch (IOException e) {
return subQueryScore * valSrcScore;
}
return subQueryScore * valSrcScore;
}
};
}
}
};
factory.providers.put(provider.getName(), provider);
AdvancedQueryIndex queryIndex = new LucenePropertyIndex(tracker, factory);
FilterImpl filter = createFilter(NT_BASE);
filter.setFullTextConstraint(new FullTextTerm(null, "bar", false, false, null));
assertFilter(filter, queryIndex, indexed, asList("/b", "/a"), true);
}
Aggregations