Search in sources :

Example 1 with FulltextQueryTermsProvider

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

the class LuceneIndexAugmentTest method nullBehavior.

//OAK-3576
@Test
public void nullBehavior() 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();
    Tree rootTree = root.getTree("/").addChild("test");
    //Note: augmentor behavior is tested elsewhere... we are just checking if default works
    int testIndex = 1;
    //both query and index augmentors are null (no exception expected)
    checkSimpleBehavior(rootTree, testIndex++);
    //Set a very sad query augmentor
    factory.fulltextQueryTermsProvider = new FulltextQueryTermsProvider() {

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

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return FulltextQueryTermsProvider.DEFAULT.getSupportedTypes();
        }
    };
    checkSimpleBehavior(rootTree, testIndex++);
    //Set query augmentor... with null query
    factory.fulltextQueryTermsProvider = new FulltextQueryTermsProvider() {

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

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return Collections.singleton(TestUtil.NT_TEST);
        }
    };
    checkSimpleBehavior(rootTree, testIndex++);
    //Set query augmentor... with some query
    factory.fulltextQueryTermsProvider = new FulltextQueryTermsProvider() {

        @Override
        public Query getQueryTerm(String text, Analyzer analyzer, NodeState indexDefinition) {
            return new TermQuery(new Term("bar", "baz"));
        }

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return Collections.singleton(TestUtil.NT_TEST);
        }
    };
    checkSimpleBehavior(rootTree, testIndex++);
    factory.fulltextQueryTermsProvider = null;
    //Set a very sad index augmentor
    factory.indexFieldProvider = IndexFieldProvider.DEFAULT;
    checkSimpleBehavior(rootTree, testIndex++);
    //Set index augmentor... with null fields
    factory.indexFieldProvider = new IndexFieldProvider() {

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

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return Collections.singleton(TestUtil.NT_TEST);
        }
    };
    checkSimpleBehavior(rootTree, testIndex++);
    //Set index augmentor... with some fields
    factory.fulltextQueryTermsProvider = null;
    factory.indexFieldProvider = new IndexFieldProvider() {

        @Nonnull
        @Override
        public Iterable<Field> getAugmentedFields(String path, NodeState document, NodeState indexDefinition) {
            List<Field> fields = Lists.newArrayList();
            fields.add(new StringField("bar", "baz", Field.Store.NO));
            return fields;
        }

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return Collections.singleton(TestUtil.NT_TEST);
        }
    };
    checkSimpleBehavior(rootTree, testIndex++);
    //setup composite query term provider with one returning null query
    factory.registerQueryTermsProvider(new FulltextQueryTermsProvider() {

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

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return Collections.singleton(TestUtil.NT_TEST);
        }
    });
    factory.useSuperBehavior = true;
    checkSimpleBehavior(rootTree, testIndex++);
}
Also used : IndexFieldProvider(org.apache.jackrabbit.oak.plugins.index.lucene.spi.IndexFieldProvider) 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) FulltextQueryTermsProvider(org.apache.jackrabbit.oak.plugins.index.lucene.spi.FulltextQueryTermsProvider) Term(org.apache.lucene.index.Term) Analyzer(org.apache.lucene.analysis.Analyzer) StringField(org.apache.lucene.document.StringField) Tree(org.apache.jackrabbit.oak.api.Tree) List(java.util.List) Test(org.junit.Test) AbstractQueryTest(org.apache.jackrabbit.oak.query.AbstractQueryTest)

Example 2 with FulltextQueryTermsProvider

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

the class LuceneIndexAugmentTest method propertyIndexUsingAugmentors.

//OAK-3576
@Test
public void propertyIndexUsingAugmentors() throws Exception {
    //setup repo and index
    NodeTypeRegistry.register(root, IOUtils.toInputStream(TestUtil.TEST_NODE_TYPE), "test nodeType");
    Tree props = createIndex(TestUtil.NT_TEST);
    TestUtil.enablePropertyIndex(props, "foo1", false);
    TestUtil.enablePropertyIndex(props, "subChild/foo2", false);
    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 [foo1]='bar1'";
    executeQuery(query, SQL2);
    assertEquals("Query augmentor should not get called for property constraints", 0, queryingCounter.get());
    query = "EXPLAIN " + query;
    List<String> paths = executeQuery(query, SQL2, false);
    assertTrue("property index should have made the index selected (" + paths.get(0) + ")", paths.get(0).contains("/* lucene:test-index("));
    query = "SELECT [jcr:path] from [" + TestUtil.NT_TEST + "] WHERE [subChild/foo2]='bar2'";
    executeQuery(query, SQL2);
    assertEquals("Query augmentor should not get called for property constraints", 0, queryingCounter.get());
    query = "EXPLAIN " + query;
    paths = executeQuery(query, SQL2);
    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 3 with FulltextQueryTermsProvider

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

the class IndexAugmentorFactoryTest method compositeQueryTermsProvider.

@Test
public void compositeQueryTermsProvider() {
    final String typeA = "type:A";
    final String typeB = "type:B";
    final String typeC = "type:C";
    final String typeD = "type:D";
    final String typeE = "type:E";
    context.registerInjectActivateService(indexAugmentorFactory);
    new IdentifiableQueryTermsProvider("1", Sets.newHashSet(typeA, typeB));
    new IdentifiableQueryTermsProvider("2", Sets.newHashSet(typeC));
    new IdentifiableQueryTermsProvider("3", Sets.newHashSet(typeA, typeB));
    new IdentifiableQueryTermsProvider(null, Sets.newHashSet(typeE));
    //register an instance which would be unregistered before validation
    FulltextQueryTermsProvider unreg = new IdentifiableQueryTermsProvider("4", Sets.newHashSet(typeD));
    indexAugmentorFactory.unbindFulltextQueryTermsProvider(unreg);
    validateComposedQueryTerms(typeA, "1", "3");
    validateComposedQueryTerms(typeC, "2");
    validateComposedQueryTerms(typeD);
    validateComposedQueryTerms(typeE);
    MockOsgi.deactivate(indexAugmentorFactory, context.bundleContext(), Collections.EMPTY_MAP);
    validateDeactivatedService();
}
Also used : FulltextQueryTermsProvider(org.apache.jackrabbit.oak.plugins.index.lucene.spi.FulltextQueryTermsProvider) Test(org.junit.Test)

Example 4 with FulltextQueryTermsProvider

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

the class LucenePropertyIndex method getLuceneRequest.

/**
     * Get the Lucene query for the given filter.
     *
     * @param plan index plan containing filter details
     * @param reader the Lucene reader
     * @return the Lucene query
     */
private static LuceneRequestFacade getLuceneRequest(IndexPlan plan, IndexAugmentorFactory augmentorFactory, IndexReader reader) {
    FulltextQueryTermsProvider augmentor = getIndexAgumentor(plan, augmentorFactory);
    List<Query> qs = new ArrayList<Query>();
    Filter filter = plan.getFilter();
    FullTextExpression ft = filter.getFullTextConstraint();
    PlanResult planResult = getPlanResult(plan);
    IndexDefinition defn = planResult.indexDefinition;
    Analyzer analyzer = defn.getAnalyzer();
    if (ft == null) {
    // there might be no full-text constraint
    // when using the LowCostLuceneIndexProvider
    // which is used for testing
    } else {
        qs.add(getFullTextQuery(plan, ft, analyzer, augmentor));
    }
    //Check if native function is supported
    PropertyRestriction pr = null;
    if (defn.hasFunctionDefined()) {
        pr = filter.getPropertyRestriction(defn.getFunctionName());
    }
    if (pr != null) {
        String query = String.valueOf(pr.first.getValue(pr.first.getType()));
        QueryParser queryParser = new QueryParser(VERSION, "", analyzer);
        if (query.startsWith("mlt?")) {
            String mltQueryString = query.replace("mlt?", "");
            if (reader != null) {
                Query moreLikeThis = MoreLikeThisHelper.getMoreLikeThis(reader, analyzer, mltQueryString);
                if (moreLikeThis != null) {
                    qs.add(moreLikeThis);
                }
            }
        } else if (query.startsWith("spellcheck?")) {
            String spellcheckQueryString = query.replace("spellcheck?", "");
            if (reader != null) {
                return new LuceneRequestFacade<SpellcheckHelper.SpellcheckQuery>(SpellcheckHelper.getSpellcheckQuery(spellcheckQueryString, reader));
            }
        } else if (query.startsWith("suggest?")) {
            String suggestQueryString = query.replace("suggest?", "");
            if (reader != null) {
                return new LuceneRequestFacade<SuggestHelper.SuggestQuery>(SuggestHelper.getSuggestQuery(suggestQueryString));
            }
        } else {
            try {
                qs.add(queryParser.parse(query));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
    } else if (planResult.evaluateNonFullTextConstraints()) {
        addNonFullTextConstraints(qs, plan, reader);
    }
    if (qs.size() == 0 && plan.getSortOrder() != null) {
        //This case indicates that query just had order by and no
        //property restriction defined. In this case property
        //existence queries for each sort entry
        List<OrderEntry> orders = removeNativeSort(plan.getSortOrder());
        for (int i = 0; i < orders.size(); i++) {
            OrderEntry oe = orders.get(i);
            PropertyDefinition pd = planResult.getOrderedProperty(i);
            PropertyRestriction orderRest = new PropertyRestriction();
            orderRest.propertyName = oe.getPropertyName();
            Query q = createQuery(orderRest, pd);
            if (q != null) {
                qs.add(q);
            }
        }
    }
    if (qs.size() == 0) {
        if (reader == null) {
            //just return match all queries
            return new LuceneRequestFacade<Query>(new MatchAllDocsQuery());
        }
        //be returned (if the index definition has a single rule)
        if (planResult.evaluateNodeTypeRestriction()) {
            return new LuceneRequestFacade<Query>(new MatchAllDocsQuery());
        }
        throw new IllegalStateException("No query created for filter " + filter);
    }
    return performAdditionalWraps(qs);
}
Also used : PropertyRestriction(org.apache.jackrabbit.oak.spi.query.Filter.PropertyRestriction) 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) SuggestHelper(org.apache.jackrabbit.oak.plugins.index.lucene.util.SuggestHelper) ArrayList(java.util.ArrayList) FulltextQueryTermsProvider(org.apache.jackrabbit.oak.plugins.index.lucene.spi.FulltextQueryTermsProvider) Analyzer(org.apache.lucene.analysis.Analyzer) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) StandardQueryParser(org.apache.lucene.queryparser.flexible.standard.StandardQueryParser) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Filter(org.apache.jackrabbit.oak.spi.query.Filter) FullTextExpression(org.apache.jackrabbit.oak.query.fulltext.FullTextExpression) ParseException(org.apache.lucene.queryparser.classic.ParseException)

Example 5 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