cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to add custom Lucene analyzer?

wergeland
Node Clone

According to the Neo4j 3.5.0 announcement, it should be possible to add custom Lucene analyzer, but I am not able to find more information on this in the documentation or the forum.

The analyzer we need should actually be included in Lucene (the SimpleAnalyzer - case insensitive without stop words), but it is not listed by call db.index.fulltext.listAvailableAnalyzers. As I understand it, all we need to do is to register it in the list of available analyzers. Another option would be to override the stopwords for the standard analyzer

I would be very happy if someone can point me in the correct direction or provide an example!

Best regards,

Øyvind Wergeland

1 ACCEPTED SOLUTION

Registration of custom analyzers is simple. Just create a java class extending AnalyzerProvider that provides the internal name and implementation of the custom analyzer.
As an example take a look at https://github.com/neo4j/neo4j/blob/3.5/community/fulltext-index/src/main/java/org/neo4j/kernel/api/...

Note that the @Service.Implementation annotation might not work without some tweaks to your build system. Instead make sure you have a service loader file in place:
META-INF/services/org.neo4j.graphdb.index.fulltext.AnalyzerProvider listing your analyzer provider classes and bundle this with your implementation into a jar file.
That jar file needs to be dropped into neo4j's /plugins folder.

View solution in original post

4 REPLIES 4

Registration of custom analyzers is simple. Just create a java class extending AnalyzerProvider that provides the internal name and implementation of the custom analyzer.
As an example take a look at https://github.com/neo4j/neo4j/blob/3.5/community/fulltext-index/src/main/java/org/neo4j/kernel/api/...

Note that the @Service.Implementation annotation might not work without some tweaks to your build system. Instead make sure you have a service loader file in place:
META-INF/services/org.neo4j.graphdb.index.fulltext.AnalyzerProvider listing your analyzer provider classes and bundle this with your implementation into a jar file.
That jar file needs to be dropped into neo4j's /plugins folder.

That worked like a charm - thank you very much!

I noted that @org.neo4j.helpers.Service.Implementation is deprecated. We use @com.google.auto.service.AutoService to create service loader configuration.

Stephan, since we're working with 'stock' Lucene, would it be possible to use this convenience class from Apache?

https://lucene.apache.org/core/5_5_0/analyzers-common/org/apache/lucene/analysis/custom/CustomAnalyz...

If so, would I need two classes then, or this one class with that also extends AnalyzerProvider?

Yes, I'm learning Java.

neo4joe, you can use that analyzer, but you still need to

  1. Configure your CustomAnalyzer instance.
  2. Register it in Neo4j.

Both can be accomplished with a single class CustomAnalyzerProvider that extends AnalyzerProvider.