Search in sources :

Example 1 with IndexingRule

use of org.apache.jackrabbit.oak.plugins.index.search.IndexDefinition.IndexingRule 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<>();
    Filter filter = plan.getFilter();
    FullTextExpression ft = filter.getFullTextConstraint();
    PlanResult planResult = getPlanResult(plan);
    LuceneIndexDefinition defn = (LuceneIndexDefinition) 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) {
                List<PropertyDefinition> sp = new LinkedList<>();
                for (IndexingRule r : defn.getDefinedRules()) {
                    sp.addAll(r.getSimilarityProperties());
                }
                if (sp.isEmpty()) {
                    Query moreLikeThis = MoreLikeThisHelper.getMoreLikeThis(reader, analyzer, mltQueryString);
                    if (moreLikeThis != null) {
                        qs.add(moreLikeThis);
                    }
                } else {
                    Query similarityQuery = SimSearchUtils.getSimilarityQuery(sp, reader, mltQueryString);
                    if (similarityQuery != null) {
                        qs.add(similarityQuery);
                    }
                }
            }
        } else if (query.startsWith("spellcheck?")) {
            String spellcheckQueryString = query.replace("spellcheck?", "");
            if (reader != null) {
                return new LuceneRequestFacade<>(SpellcheckHelper.getSpellcheckQuery(spellcheckQueryString, reader));
            }
        } else if (query.startsWith("suggest?")) {
            String suggestQueryString = query.replace("suggest?", "");
            if (reader != null) {
                return new LuceneRequestFacade<>(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(oe.getPropertyName(), 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 : PlanResult(org.apache.jackrabbit.oak.plugins.index.search.spi.query.FulltextIndexPlanner.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) PrefixQuery(org.apache.lucene.search.PrefixQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) ArrayList(java.util.ArrayList) FulltextQueryTermsProvider(org.apache.jackrabbit.oak.plugins.index.lucene.spi.FulltextQueryTermsProvider) Analyzer(org.apache.lucene.analysis.Analyzer) IndexingRule(org.apache.jackrabbit.oak.plugins.index.search.IndexDefinition.IndexingRule) PropertyRestriction(org.apache.jackrabbit.oak.spi.query.Filter.PropertyRestriction) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) PropertyDefinition(org.apache.jackrabbit.oak.plugins.index.search.PropertyDefinition) LinkedList(java.util.LinkedList) 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.spi.query.fulltext.FullTextExpression) ParseException(org.apache.lucene.queryparser.classic.ParseException)

Example 2 with IndexingRule

use of org.apache.jackrabbit.oak.plugins.index.search.IndexDefinition.IndexingRule in project jackrabbit-oak by apache.

the class LuceneIndexDefinitionTest method relativePropertyConfig.

@Test
public void relativePropertyConfig() throws Exception {
    builder.child(PROP_NODE).child("foo1").child("bar").setProperty(FulltextIndexConstants.PROP_TYPE, PropertyType.TYPENAME_DATE);
    builder.child(PROP_NODE).child("foo2").child("bar2").child("baz").setProperty(FulltextIndexConstants.PROP_TYPE, PropertyType.TYPENAME_LONG);
    builder.setProperty(createProperty(INCLUDE_PROPERTY_NAMES, of("foo", "foo1/bar", "foo2/bar2/baz"), STRINGS));
    LuceneIndexDefinition defn = new LuceneIndexDefinition(root, builder.getNodeState(), "/foo");
    IndexingRule rule = defn.getApplicableIndexingRule(asState(newNode("nt:folder")));
    assertNotNull(rule.getConfig("foo1/bar"));
    assertEquals(PropertyType.DATE, rule.getConfig("foo1/bar").getType());
    assertEquals(PropertyType.LONG, rule.getConfig("foo2/bar2/baz").getType());
    assertTrue(rule.getConfig("foo1/bar").relative);
    assertArrayEquals(new String[] { "foo2", "bar2" }, rule.getConfig("foo2/bar2/baz").ancestors);
}
Also used : IndexingRule(org.apache.jackrabbit.oak.plugins.index.search.IndexDefinition.IndexingRule) LuceneIndexHelper.newLuceneIndexDefinition(org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexHelper.newLuceneIndexDefinition) Test(org.junit.Test)

Example 3 with IndexingRule

use of org.apache.jackrabbit.oak.plugins.index.search.IndexDefinition.IndexingRule in project jackrabbit-oak by apache.

the class LuceneIndexDefinitionTest method indexRuleWithPropertyRegEx.

@Test
public void indexRuleWithPropertyRegEx() throws Exception {
    NodeBuilder rules = builder.child(INDEX_RULES);
    rules.child("nt:folder");
    TestUtil.child(rules, "nt:folder/properties/prop1").setProperty(FulltextIndexConstants.FIELD_BOOST, 3.0);
    TestUtil.child(rules, "nt:folder/properties/prop2").setProperty(PROP_NAME, "foo.*").setProperty(FulltextIndexConstants.PROP_IS_REGEX, true).setProperty(FulltextIndexConstants.FIELD_BOOST, 4.0);
    LuceneIndexDefinition defn = new LuceneIndexDefinition(root, builder.getNodeState(), "/foo");
    IndexingRule rule1 = defn.getApplicableIndexingRule(asState(newNode("nt:folder")));
    assertNotNull(rule1);
    assertTrue(rule1.isIndexed("prop1"));
    assertFalse(rule1.isIndexed("prop2"));
    assertTrue(rule1.isIndexed("fooProp"));
    PropertyDefinition pd = rule1.getConfig("fooProp2");
    assertEquals(4.0f, pd.boost, 0);
}
Also used : IndexingRule(org.apache.jackrabbit.oak.plugins.index.search.IndexDefinition.IndexingRule) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) PropertyDefinition(org.apache.jackrabbit.oak.plugins.index.search.PropertyDefinition) LuceneIndexHelper.newLuceneIndexDefinition(org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexHelper.newLuceneIndexDefinition) Test(org.junit.Test)

Example 4 with IndexingRule

use of org.apache.jackrabbit.oak.plugins.index.search.IndexDefinition.IndexingRule in project jackrabbit-oak by apache.

the class LuceneIndexDefinitionTest method nodeFullTextIndexed_Aggregates.

@Test
public void nodeFullTextIndexed_Aggregates() throws Exception {
    NodeBuilder rules = builder.child(INDEX_RULES);
    rules.child("nt:folder");
    TestUtil.child(rules, "nt:folder/properties/prop1").setProperty(PROP_NAME, "foo").setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
    NodeBuilder aggregates = builder.child(FulltextIndexConstants.AGGREGATES);
    NodeBuilder aggFolder = aggregates.child("nt:folder");
    aggFolder.child("i1").setProperty(FulltextIndexConstants.AGG_PATH, "*");
    LuceneIndexDefinition defn = new LuceneIndexDefinition(root, builder.getNodeState(), "/foo");
    IndexingRule rule = defn.getApplicableIndexingRule(asState(newNode("nt:folder")));
    assertNotNull(rule);
    assertTrue(rule.isNodeFullTextIndexed());
    assertTrue(rule.indexesAllNodesOfMatchingType());
}
Also used : IndexingRule(org.apache.jackrabbit.oak.plugins.index.search.IndexDefinition.IndexingRule) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) LuceneIndexHelper.newLuceneIndexDefinition(org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexHelper.newLuceneIndexDefinition) Test(org.junit.Test)

Example 5 with IndexingRule

use of org.apache.jackrabbit.oak.plugins.index.search.IndexDefinition.IndexingRule in project jackrabbit-oak by apache.

the class LuceneIndexDefinitionTest method regexAllProps.

@Test
public void regexAllProps() {
    LuceneIndexDefinitionBuilder builder = new LuceneIndexDefinitionBuilder();
    builder.indexRule("nt:base").property("p");
    builder.indexRule("nt:base").property("all", FulltextIndexConstants.REGEX_ALL_PROPS, true);
    IndexDefinition def = IndexDefinition.newBuilder(root, builder.build(), "/foo").build();
    IndexingRule rule = def.getApplicableIndexingRule(root);
    assertNotNull(rule);
    PropertyDefinition pd = rule.getConfig("p");
    assertNotNull(pd);
    assertFalse(pd.isRegexp);
    assertFalse(pd.relative);
    assertEquals(0, pd.ancestors.length);
    pd = rule.getConfig("all");
    assertNotNull(pd);
    assertTrue(pd.isRegexp);
    assertFalse(pd.relative);
    assertEquals(0, pd.ancestors.length);
    assertThat(rule.getAggregate().getIncludes(), is(empty()));
    assertFalse(rule.getAggregate().hasNodeAggregates());
    List<Aggregate.Matcher> matchers = rule.getAggregate().createMatchers(new TestRoot("/"));
    assertThat(matchers, is(empty()));
    assertThat(def.getRelativeNodeNames(), is(empty()));
}
Also used : IndexingRule(org.apache.jackrabbit.oak.plugins.index.search.IndexDefinition.IndexingRule) IndexDefinition(org.apache.jackrabbit.oak.plugins.index.search.IndexDefinition) LuceneIndexHelper.newLucenePropertyIndexDefinition(org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexHelper.newLucenePropertyIndexDefinition) LuceneIndexHelper.newLuceneIndexDefinition(org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexHelper.newLuceneIndexDefinition) LuceneIndexDefinitionBuilder(org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexDefinitionBuilder) PropertyDefinition(org.apache.jackrabbit.oak.plugins.index.search.PropertyDefinition) Test(org.junit.Test)

Aggregations

IndexingRule (org.apache.jackrabbit.oak.plugins.index.search.IndexDefinition.IndexingRule)49 Test (org.junit.Test)43 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)26 LuceneIndexHelper.newLuceneIndexDefinition (org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexHelper.newLuceneIndexDefinition)24 IndexHelper.newFTIndexDefinition (org.apache.jackrabbit.oak.plugins.index.search.util.IndexHelper.newFTIndexDefinition)19 IndexHelper.newFTPropertyIndexDefinition (org.apache.jackrabbit.oak.plugins.index.search.util.IndexHelper.newFTPropertyIndexDefinition)19 PropertyDefinition (org.apache.jackrabbit.oak.plugins.index.search.PropertyDefinition)10 IndexDefinition (org.apache.jackrabbit.oak.plugins.index.search.IndexDefinition)7 LuceneIndexDefinitionBuilder (org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexDefinitionBuilder)6 LuceneIndexHelper.newLucenePropertyIndexDefinition (org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexHelper.newLucenePropertyIndexDefinition)6 IndexDefinitionBuilder (org.apache.jackrabbit.oak.plugins.index.search.util.IndexDefinitionBuilder)6 PropertyRestriction (org.apache.jackrabbit.oak.spi.query.Filter.PropertyRestriction)4 PrefixQuery (org.apache.lucene.search.PrefixQuery)4 TermQuery (org.apache.lucene.search.TermQuery)4 WildcardQuery (org.apache.lucene.search.WildcardQuery)4 LinkedList (java.util.LinkedList)3 BooleanQuery (org.apache.lucene.search.BooleanQuery)3 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)3 NumericRangeQuery (org.apache.lucene.search.NumericRangeQuery)3 Query (org.apache.lucene.search.Query)3