Search in sources :

Example 11 with AdvancedQueryIndex

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"));
}
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) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) Test(org.junit.Test)

Example 12 with AdvancedQueryIndex

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();
}
Also used : AdvancedQueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex) IndexPlan(org.apache.jackrabbit.oak.spi.query.QueryIndex.IndexPlan) QueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex) AdvancedQueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex)

Example 13 with AdvancedQueryIndex

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());
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) FilterImpl(org.apache.jackrabbit.oak.query.index.FilterImpl) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) Cursor(org.apache.jackrabbit.oak.spi.query.Cursor) AdvancedQueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex) LuceneIndexHelper.newLucenePropertyIndexDefinition(org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexHelper.newLucenePropertyIndexDefinition) LuceneIndexHelper.newLuceneIndexDefinition(org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexHelper.newLuceneIndexDefinition) IndexPlan(org.apache.jackrabbit.oak.spi.query.QueryIndex.IndexPlan) Test(org.junit.Test)

Example 14 with AdvancedQueryIndex

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());
}
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) IndexPlan(org.apache.jackrabbit.oak.spi.query.QueryIndex.IndexPlan) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) Cursor(org.apache.jackrabbit.oak.spi.query.Cursor) Test(org.junit.Test)

Example 15 with AdvancedQueryIndex

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);
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) FilterImpl(org.apache.jackrabbit.oak.query.index.FilterImpl) Query(org.apache.lucene.search.Query) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) AtomicReader(org.apache.lucene.index.AtomicReader) IOException(java.io.IOException) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) Document(org.apache.lucene.document.Document) AdvancedQueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex) ScorerProvider(org.apache.jackrabbit.oak.plugins.index.lucene.score.ScorerProvider) FullTextTerm(org.apache.jackrabbit.oak.query.fulltext.FullTextTerm) CustomScoreProvider(org.apache.lucene.queries.CustomScoreProvider) AtomicReaderContext(org.apache.lucene.index.AtomicReaderContext) Test(org.junit.Test)

Aggregations

AdvancedQueryIndex (org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex)18 FilterImpl (org.apache.jackrabbit.oak.query.index.FilterImpl)14 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)13 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)13 Test (org.junit.Test)13 IndexPlan (org.apache.jackrabbit.oak.spi.query.QueryIndex.IndexPlan)11 Cursor (org.apache.jackrabbit.oak.spi.query.Cursor)7 QueryIndex (org.apache.jackrabbit.oak.spi.query.QueryIndex)4 FullTextTerm (org.apache.jackrabbit.oak.query.fulltext.FullTextTerm)2 Function (com.google.common.base.Function)1 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ScorerProvider (org.apache.jackrabbit.oak.plugins.index.lucene.score.ScorerProvider)1 LuceneIndexHelper.newLuceneIndexDefinition (org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexHelper.newLuceneIndexDefinition)1 LuceneIndexHelper.newLucenePropertyIndexDefinition (org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexHelper.newLucenePropertyIndexDefinition)1 TraversingIndex (org.apache.jackrabbit.oak.query.index.TraversingIndex)1 SelectorExecutionPlan (org.apache.jackrabbit.oak.query.plan.SelectorExecutionPlan)1 IndexRow (org.apache.jackrabbit.oak.spi.query.IndexRow)1 OrderEntry (org.apache.jackrabbit.oak.spi.query.QueryIndex.OrderEntry)1