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