How do I add an EdgeNGramTokenFilter to a Compass

2019-09-16 14:05发布

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?

1条回答
闹够了就滚
2楼-- · 2019-09-16 14:21

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 {
    }

}
查看更多
登录 后发表回答