lucene-近音词查询
分析器通过Metaphone算法实现近音词查询,也可采用类似于Soundex的其它可行的算法
public void testKoolKat() throws Exceptoin{
RAMDirectory directory=new RAMDirectory();
Analyzer analyzer=new MetaphoneReplacementAnalyzer();
IndexWriter writer=new IndexWriter(directory,analyzer,true);
Document doc=new Document();
doc.add(Field.Text("contents","cool cat"));
writer.addDocument(doc);
writer.close();
IndexSearcher searcher=new IndexSearcher(directory);
Query query=QueryParser.parse("kool kat","contents",analyzer);
Hits hits=searcher.search(query);
}
public class MetaPhoneReplacementAnalyzer extends Anaylyer{
public TokenStream tokenStream(String fieldName,Reader reader){
return new MetaphoneReplacementFilter{
new LetterTokenizer(reader));
}
}
}
public class MetaphoneReplacementFilter extends TokenFilter{
publicstatic final String METAPHONE="METAPHONE";
privateMetaphone metaphoner=new Metaphone();
publicMetaphoneReplacementFilter(TokenStream input){
super(input);
}
publicToken next() throws IOException{
Token t=input.next();
if (t==null) return null;
try{
return newToken(metaphoner.encode(t.termText()),t.startOffset().t.endOffset(),METAPHONE);//设置语汇单元的token类型。
}
catch(EncodeException e){
return t;
}
}
}
上一篇: lucene-Nutch分析
下一篇: lucene-wildcardQuery
推荐阅读