How to add custom Lucene analyzer?

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

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 neo4j/Swedish.java at 3.5 · neo4j/neo4j · GitHub

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.

1 Like

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.

1 Like

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/CustomAnalyzer.html

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

Yes, I'm learning Java. :slight_smile:

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.

1 Like