use of org.apache.lucene.analysis.Analyzer in project geode by apache.
the class LuceneIndexCommandsDUnitTest method createIndex.
private void createIndex(final VM vm1) {
vm1.invoke(() -> {
LuceneService luceneService = LuceneServiceProvider.get(getCache());
Map<String, Analyzer> fieldAnalyzers = new HashMap();
fieldAnalyzers.put("field1", new StandardAnalyzer());
fieldAnalyzers.put("field2", new KeywordAnalyzer());
fieldAnalyzers.put("field3", null);
luceneService.createIndexFactory().setFields(fieldAnalyzers).create(INDEX_NAME, REGION_NAME);
createRegion();
});
}
use of org.apache.lucene.analysis.Analyzer in project geode by apache.
the class LuceneIndexXmlParserIntegrationJUnitTest method parseIndexWithAnalyzers.
@Test
public void parseIndexWithAnalyzers() throws FileNotFoundException {
RegionCreation region = createRegionCreation("region");
// Validate expected indexes
Map<String, String[]> expectedIndexes = new HashMap<String, String[]>();
expectedIndexes.put("index", new String[] { "a", "b", "c" });
validateExpectedIndexes(region, expectedIndexes);
// Validate expected analyzers
Map<String, Map<String, Class<? extends Analyzer>>> expectedIndexAnalyzers = new HashMap<>();
Map<String, Class<? extends Analyzer>> expectedFieldAnalyzers = new HashMap<>();
expectedFieldAnalyzers.put("a", KeywordAnalyzer.class);
expectedFieldAnalyzers.put("b", SimpleAnalyzer.class);
expectedFieldAnalyzers.put("c", ClassicAnalyzer.class);
expectedIndexAnalyzers.put("index", expectedFieldAnalyzers);
validateExpectedAnalyzers(region, expectedIndexAnalyzers);
}
use of org.apache.lucene.analysis.Analyzer in project jackrabbit by apache.
the class JackrabbitAnalyzer method getAnalyzerInstance.
/**
* Returns a new instance of the named Lucene {@link Analyzer} class,
* or the default analyzer if the given class can not be instantiated.
*
* @param className name of the analyzer class
* @return new analyzer instance, or the default analyzer
*/
static Analyzer getAnalyzerInstance(String className) {
Class<?> analyzerClass;
try {
analyzerClass = Class.forName(className);
} catch (ClassNotFoundException e) {
log.warn(className + " could not be found", e);
return DEFAULT_ANALYZER;
}
if (!Analyzer.class.isAssignableFrom(analyzerClass)) {
log.warn(className + " is not a Lucene Analyzer");
return DEFAULT_ANALYZER;
} else if (JackrabbitAnalyzer.class.isAssignableFrom(analyzerClass)) {
log.warn(className + " can not be used as a JackrabbitAnalyzer component");
return DEFAULT_ANALYZER;
}
Exception cause = null;
Constructor<?>[] constructors = analyzerClass.getConstructors();
for (Constructor<?> constructor : constructors) {
Class<?>[] types = constructor.getParameterTypes();
if (types.length == 1 && types[0] == Version.class) {
try {
return (Analyzer) constructor.newInstance(Version.LUCENE_36);
} catch (Exception e) {
cause = e;
}
}
}
for (Constructor<?> constructor : constructors) {
if (constructor.getParameterTypes().length == 0) {
try {
return (Analyzer) constructor.newInstance();
} catch (Exception e) {
cause = e;
}
}
}
log.warn(className + " could not be instantiated", cause);
return DEFAULT_ANALYZER;
}
use of org.apache.lucene.analysis.Analyzer 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.lucene.analysis.Analyzer 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("));
}
Aggregations