Search in sources :

Example 6 with FulltextQueryTermsProvider

use of org.apache.jackrabbit.oak.plugins.index.lucene.spi.FulltextQueryTermsProvider in project jackrabbit-oak by apache.

the class LuceneIndexAugmentTest method fulltextIndexUsingAugmentors.

//OAK-3576
@Test
public void fulltextIndexUsingAugmentors() throws Exception {
    //setup repo and index
    NodeTypeRegistry.register(root, IOUtils.toInputStream(TestUtil.TEST_NODE_TYPE), "test nodeType");
    Tree props = createIndex(TestUtil.NT_TEST);
    TestUtil.enableForFullText(props, "foo1");
    TestUtil.enableForFullText(props, "subChild/foo2");
    root.commit();
    //setup augmentors
    final AtomicInteger indexingCounter = new AtomicInteger(0);
    factory.indexFieldProvider = new IndexFieldProvider() {

        @Nonnull
        @Override
        public Iterable<Field> getAugmentedFields(String path, NodeState document, NodeState indexDefinition) {
            indexingCounter.incrementAndGet();
            return IndexFieldProvider.DEFAULT.getAugmentedFields(path, document, indexDefinition);
        }

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return Collections.singleton(TestUtil.NT_TEST);
        }
    };
    final AtomicInteger queryingCounter = new AtomicInteger(0);
    factory.fulltextQueryTermsProvider = new FulltextQueryTermsProvider() {

        @Override
        public Query getQueryTerm(String text, Analyzer analyzer, NodeState indexDefinition) {
            queryingCounter.set(1);
            return null;
        }

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return Collections.singleton(TestUtil.NT_TEST);
        }
    };
    //add content
    Tree node1 = createNodeWithType(root.getTree("/"), "node1", TestUtil.NT_TEST);
    node1.setProperty("foo1", "bar1");
    node1.addChild("subChild").setProperty("foo2", "bar2");
    root.commit();
    //indexing assertions
    assertEquals("Indexing augment should get called once", 1, indexingCounter.get());
    String query = "SELECT [jcr:path] from [" + TestUtil.NT_TEST + "] WHERE CONTAINS(*, 'bar1')";
    executeQuery(query, SQL2);
    assertEquals("Query augmentor should get called for full text constraints", 1, queryingCounter.get());
    queryingCounter.set(0);
    query = "EXPLAIN " + query;
    List<String> paths = executeQuery(query, SQL2, false);
    assertEquals("Query augmentor should get called for full text constraints", 1, queryingCounter.get());
    assertTrue("property index should have made the index selected (" + paths.get(0) + ")", paths.get(0).contains("/* lucene:test-index("));
    queryingCounter.set(0);
    query = "SELECT [jcr:path] from [" + TestUtil.NT_TEST + "] WHERE CONTAINS(*, 'bar2')";
    executeQuery(query, SQL2);
    assertEquals("Query augmentor should get called for full text constraints", 1, queryingCounter.get());
    queryingCounter.set(0);
    query = "EXPLAIN " + query;
    paths = executeQuery(query, SQL2, false);
    assertEquals("Query augmentor should get called for full text constraints", 1, queryingCounter.get());
    assertTrue("property index should have made the index selected (" + paths.get(0) + ")", paths.get(0).contains("/* lucene:test-index("));
}
Also used : IndexFieldProvider(org.apache.jackrabbit.oak.plugins.index.lucene.spi.IndexFieldProvider) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) Set(java.util.Set) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) Nonnull(javax.annotation.Nonnull) FulltextQueryTermsProvider(org.apache.jackrabbit.oak.plugins.index.lucene.spi.FulltextQueryTermsProvider) Analyzer(org.apache.lucene.analysis.Analyzer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Tree(org.apache.jackrabbit.oak.api.Tree) Test(org.junit.Test) AbstractQueryTest(org.apache.jackrabbit.oak.query.AbstractQueryTest)

Example 7 with FulltextQueryTermsProvider

use of org.apache.jackrabbit.oak.plugins.index.lucene.spi.FulltextQueryTermsProvider in project jackrabbit-oak by apache.

the class LuceneIndexAugmentTest method queryAugmentorMismatchedNodeType.

@Test
public void queryAugmentorMismatchedNodeType() throws Exception {
    //setup repo and index
    NodeTypeRegistry.register(root, IOUtils.toInputStream(TestUtil.TEST_NODE_TYPE), "test nodeType");
    Tree props = createIndex(TestUtil.NT_TEST);
    TestUtil.enableForFullText(props, "foo1", false);
    root.commit();
    //setup augmentors
    final AtomicInteger indexingCounter1 = new AtomicInteger(0);
    final AtomicInteger indexingCounter2 = new AtomicInteger(0);
    factory.registerQueryTermsProvider(new FulltextQueryTermsProvider() {

        @Override
        public Query getQueryTerm(String text, Analyzer analyzer, NodeState indexDefinition) {
            indexingCounter1.set(1);
            return null;
        }

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return Collections.singleton(JcrConstants.NT_BASE);
        }
    });
    factory.registerQueryTermsProvider(new FulltextQueryTermsProvider() {

        @Override
        public Query getQueryTerm(String text, Analyzer analyzer, NodeState indexDefinition) {
            indexingCounter2.set(1);
            return null;
        }

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return Collections.singleton(TestUtil.NT_TEST);
        }
    });
    factory.useSuperBehavior = true;
    executeQuery("SELECT [jcr:path] FROM [" + TestUtil.NT_TEST + "] WHERE CONTAINS(*, 'test')", SQL2, false);
    assertEquals("Mismatching node type should not let index augmentor called", 0, indexingCounter1.get());
    assertEquals("Matching node type should get augmentor called", 1, indexingCounter2.get());
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) Set(java.util.Set) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Nonnull(javax.annotation.Nonnull) Tree(org.apache.jackrabbit.oak.api.Tree) FulltextQueryTermsProvider(org.apache.jackrabbit.oak.plugins.index.lucene.spi.FulltextQueryTermsProvider) Analyzer(org.apache.lucene.analysis.Analyzer) Test(org.junit.Test) AbstractQueryTest(org.apache.jackrabbit.oak.query.AbstractQueryTest)

Example 8 with FulltextQueryTermsProvider

use of org.apache.jackrabbit.oak.plugins.index.lucene.spi.FulltextQueryTermsProvider in project jackrabbit-oak by apache.

the class LuceneIndexAugmentTest method queryHook.

//OAK-3576
@Test
public void queryHook() throws Exception {
    //setup repo and index
    NodeTypeRegistry.register(root, IOUtils.toInputStream(TestUtil.TEST_NODE_TYPE), "test nodeType");
    Tree props = createIndex(TestUtil.NT_TEST);
    TestUtil.enableForFullText(props, "foo");
    root.commit();
    //setup query augmentor
    final String testSearchText = "search this text";
    final String realSearchText = "bar";
    factory.fulltextQueryTermsProvider = new FulltextQueryTermsProvider() {

        @Override
        public Query getQueryTerm(String text, Analyzer analyzer, NodeState indexDefinition) {
            assertEquals("Full text term passed to provider isn't same as the one passed in query", testSearchText, text);
            return new TermQuery(new Term(":fulltext", realSearchText));
        }

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return Collections.singleton(TestUtil.NT_TEST);
        }
    };
    //add content
    Tree test = root.getTree("/").addChild("test");
    Tree node = createNodeWithType(test, "item", TestUtil.NT_TEST);
    node.setProperty("foo", realSearchText);
    root.commit();
    //query (testSearchText doesn't have 'bar'... our augment would search for :fulltext:bar
    String query = "select [jcr:path] from [oak:TestNode] where CONTAINS(*, '" + testSearchText + "')";
    List<String> paths = executeQuery(query, SQL2);
    assertEquals("Augmented query wasn't used to search", 1, paths.size());
    assertEquals("/test/item", paths.get(0));
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) Set(java.util.Set) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) Nonnull(javax.annotation.Nonnull) Tree(org.apache.jackrabbit.oak.api.Tree) FulltextQueryTermsProvider(org.apache.jackrabbit.oak.plugins.index.lucene.spi.FulltextQueryTermsProvider) Term(org.apache.lucene.index.Term) Analyzer(org.apache.lucene.analysis.Analyzer) Test(org.junit.Test) AbstractQueryTest(org.apache.jackrabbit.oak.query.AbstractQueryTest)

Example 9 with FulltextQueryTermsProvider

use of org.apache.jackrabbit.oak.plugins.index.lucene.spi.FulltextQueryTermsProvider in project jackrabbit-oak by apache.

the class LuceneIndexAugmentTest method skipDefaultOnlyUsingAugmentors.

//OAK-3576
@Test
public void skipDefaultOnlyUsingAugmentors() throws Exception {
    //setup repo and index
    NodeTypeRegistry.register(root, IOUtils.toInputStream(TestUtil.TEST_NODE_TYPE), "test nodeType");
    Tree props = createIndex(TestUtil.NT_TEST);
    Tree prop = props.addChild("foo1");
    prop.setProperty(LuceneIndexConstants.PROP_INDEX, true);
    prop = props.addChild("foo2");
    prop.setProperty(LuceneIndexConstants.PROP_NAME, "subChild/foo2");
    prop.setProperty(LuceneIndexConstants.PROP_INDEX, true);
    root.commit();
    //setup augmentors
    final AtomicInteger indexingCounter = new AtomicInteger(0);
    factory.indexFieldProvider = new IndexFieldProvider() {

        @Nonnull
        @Override
        public Iterable<Field> getAugmentedFields(String path, NodeState document, NodeState indexDefinition) {
            indexingCounter.incrementAndGet();
            return IndexFieldProvider.DEFAULT.getAugmentedFields(path, document, indexDefinition);
        }

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return Collections.singleton(TestUtil.NT_TEST);
        }
    };
    final AtomicInteger queryingCounter = new AtomicInteger(0);
    factory.fulltextQueryTermsProvider = new FulltextQueryTermsProvider() {

        @Override
        public Query getQueryTerm(String text, Analyzer analyzer, NodeState indexDefinition) {
            queryingCounter.set(1);
            return null;
        }

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return Collections.singleton(TestUtil.NT_TEST);
        }
    };
    //add content
    Tree node1 = createNodeWithType(root.getTree("/"), "node1", TestUtil.NT_TEST);
    node1.setProperty("foo1", "bar1");
    node1.addChild("subChild").setProperty("foo2", "bar2");
    root.commit();
    //indexing assertions
    assertEquals("Indexing augment should get called once", 1, indexingCounter.get());
    assertEquals("No docs should get indexed (augmentor hasn't added any field)", 0, getSearcher().getIndexReader().numDocs());
    String query = "EXPLAIN SELECT [jcr:path] from [" + TestUtil.NT_TEST + "] WHERE [foo1]='bar1'";
    List<String> paths = executeQuery(query, SQL2);
    assertTrue("indexed prop name shouldn't decide query plan (" + paths.get(0) + ")", paths.get(0).contains("/* no-index "));
    query = "EXPLAIN SELECT [jcr:path] from [" + TestUtil.NT_TEST + "] WHERE [subChild/foo2]='bar2'";
    paths = executeQuery(query, SQL2);
    assertTrue("indexed prop name shouldn't decide query plan (" + paths.get(0) + ")", paths.get(0).contains("/* no-index "));
}
Also used : IndexFieldProvider(org.apache.jackrabbit.oak.plugins.index.lucene.spi.IndexFieldProvider) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) Set(java.util.Set) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) Nonnull(javax.annotation.Nonnull) FulltextQueryTermsProvider(org.apache.jackrabbit.oak.plugins.index.lucene.spi.FulltextQueryTermsProvider) Analyzer(org.apache.lucene.analysis.Analyzer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Tree(org.apache.jackrabbit.oak.api.Tree) Test(org.junit.Test) AbstractQueryTest(org.apache.jackrabbit.oak.query.AbstractQueryTest)

Example 10 with FulltextQueryTermsProvider

use of org.apache.jackrabbit.oak.plugins.index.lucene.spi.FulltextQueryTermsProvider in project jackrabbit-oak by apache.

the class IndexAugmentorFactory method refreshFulltextQueryTermsProviders.

private void refreshFulltextQueryTermsProviders() {
    ListMultimap<String, FulltextQueryTermsProvider> providerMultimap = LinkedListMultimap.create();
    for (FulltextQueryTermsProvider provider : fulltextQueryTermsProviders) {
        Set<String> supportedNodeTypes = provider.getSupportedTypes();
        for (String nodeType : supportedNodeTypes) {
            providerMultimap.put(nodeType, provider);
        }
    }
    Map<String, CompositeFulltextQueryTermsProvider> providerMap = Maps.newHashMap();
    for (String nodeType : providerMultimap.keySet()) {
        List<FulltextQueryTermsProvider> providers = providerMultimap.get(nodeType);
        CompositeFulltextQueryTermsProvider compositeFulltextQueryTermsProvider = new CompositeFulltextQueryTermsProvider(nodeType, providers);
        providerMap.put(nodeType, compositeFulltextQueryTermsProvider);
    }
    fulltextQueryTermsProviderMap = ImmutableMap.copyOf(providerMap);
}
Also used : FulltextQueryTermsProvider(org.apache.jackrabbit.oak.plugins.index.lucene.spi.FulltextQueryTermsProvider)

Aggregations

FulltextQueryTermsProvider (org.apache.jackrabbit.oak.plugins.index.lucene.spi.FulltextQueryTermsProvider)10 Query (org.apache.lucene.search.Query)8 TermQuery (org.apache.lucene.search.TermQuery)8 Analyzer (org.apache.lucene.analysis.Analyzer)7 Test (org.junit.Test)7 Set (java.util.Set)6 Nonnull (javax.annotation.Nonnull)6 Tree (org.apache.jackrabbit.oak.api.Tree)6 AbstractQueryTest (org.apache.jackrabbit.oak.query.AbstractQueryTest)6 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 IndexFieldProvider (org.apache.jackrabbit.oak.plugins.index.lucene.spi.IndexFieldProvider)4 Term (org.apache.lucene.index.Term)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 PlanResult (org.apache.jackrabbit.oak.plugins.index.lucene.IndexPlanner.PlanResult)1 SuggestHelper (org.apache.jackrabbit.oak.plugins.index.lucene.util.SuggestHelper)1 FullTextExpression (org.apache.jackrabbit.oak.query.fulltext.FullTextExpression)1 Filter (org.apache.jackrabbit.oak.spi.query.Filter)1