Updating a Solr Analysis Plugin from 1.4.1 (Lucene 2.9) to Solr / Lucene 4.0 (current trunk)

Three years and a couple of weeks ago I wrote a post about how to get started writing a simple Solr Analysis Plugin to handle incoming tokens and modifying them in place when an update is requested.

Since then the whole version number structure of Solr has changed (and is now in sync with the underlying Lucene version), and not surprisingly, the current API has also been updated. This means that a few small changes are required to get your analysis plugins running on the current trunk of Lucene and Solr.

The main change is that the previously named TermAttribute is now named CharTermAttribute, this means that any imports will have to change:

  1. - import org.apache.lucene.analysis.tokenattributes.TermAttribute;
  2. + import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;

Any declarations of TermAttributes will need to be CharTermAttributes instead:

  1. - private TermAttribute termAtt;
  2. + private CharTermAttribute termAtt;
  1.   public NorwegianNameFilter(TokenStream input)
  2.   {
  3.       super(input);
  4. -     termAtt = (TermAttribute) addAttribute(TermAttribute.class);
  5. +     termAtt = input.getAttribute(CharTermAttribute.class);
  6.   }

We now fetch the attribute from the current TokenStream (not sure if the old way I did it has been deprecated, but this seems to be the suggested way now). We also change any references to TermAttribute.class to CharTermAttribute.class.

The actual TermAttribute interface has also changed, meaning we’ll have to change a few of the old method calls:

  1. - termAtt.setTermLength(this.parseBuffer(termAtt.termBuffer(), termAtt.termLength()));
  2. + termAtt.setLength(this.parseBuffer(termAtt.buffer(), termAtt.length()));

.setTermLength() => .setLength()
.termBuffer => .buffer()
.termLength => .length()

The methods will behave in the same manner as in the previous API, .buffer() will retrieve a char array (char[]) which is the current buffer of the actual term which can you modify in place, while length() and setLength() retrieves the current length of the buffer (the buffer can be larger than the part used) and sets the new length of the buffer (if you’re collapsing characters).

The new implementation of our analysis filter skeleton:

  1. package no.derdubor.solr.analysis;
  2.  
  3. import java.io.IOException;
  4. import org.apache.lucene.analysis.Token;
  5. import org.apache.lucene.analysis.TokenFilter;
  6. import org.apache.lucene.analysis.TokenStream;
  7. import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
  8.  
  9. public class NorwegianNameFilter extends TokenFilter
  10. {
  11.     private CharTermAttribute termAtt;
  12.  
  13.     public NorwegianNameFilter(TokenStream input)
  14.     {
  15.         super(input);
  16.         termAtt = input.getAttribute(CharTermAttribute.class);
  17.     }
  18.  
  19.     public boolean incrementToken() throws IOException
  20.     {
  21.         if (this.input.incrementToken())
  22.         {
  23.             termAtt.setLength(this.parseBuffer(termAtt.buffer(), termAtt.length()));
  24.             return true;
  25.         }
  26.        
  27.         return false;
  28.     }
  29.    
  30.     protected int parseBuffer(char[] buffer, int bufferLength)
  31.     {
  32.  
  33.     }
  34. }

Tags: , , , , , , , , , ,

2 Responses to “Updating a Solr Analysis Plugin from 1.4.1 (Lucene 2.9) to Solr / Lucene 4.0 (current trunk)”

  1. Mats Lindh » Blog Archive » Writing a Solr Analysis Filter Plugin Says:

    [...] Updating a Solr Analysis Plugin from 1.4.1 (Lucene 2.9) to Solr / Lucene 4.0 (current trunk) [...]

  2. Arian Says:

    Awesome, but how do you implemented parseBuffer()?

Leave a Reply