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);
}
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);
}
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);
}
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());
}
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()));
}
Aggregations