Search code examples
lucenecompass-lucene

How do I add an EdgeNGramTokenFilter to a Compass Query?


I am building some auto-complete functionality using compass and I need to add an EdgeNGramTokenFilter to the compass query but I cannot see how I can add it.

Is this possible?


Solution

  • I managed to add the EdgeNGramTokenFilter filter by creating a provider class adding a reference to it in the compass.config.xml file by adding the following line within the <searchEngine> tags

    <analyzerFilter name="lower" type="EdgeNGramTokenFilterProvider"/>
    

    Here is the class:

    import org.apache.lucene.analysis.TokenStream;
    import org.apache.lucene.analysis.ngram.EdgeNGramTokenFilter;
    import org.apache.lucene.analysis.ngram.EdgeNGramTokenFilter.Side;
    import org.compass.core.CompassException;
    import org.compass.core.config.CompassSettings;
    import org.compass.core.lucene.engine.analyzer.LuceneAnalyzerTokenFilterProvider;
    
    public class EdgeNGramTokenFilterProvider implements LuceneAnalyzerTokenFilterProvider {
    
        public TokenStream createTokenFilter(TokenStream tokenStream) {
            return new EdgeNGramTokenFilter(tokenStream, Side.FRONT, 1, 20);
        }
    
        public void configure(CompassSettings settings) throws CompassException {
        }
    
    }