欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

lucene-近音词查询

程序员文章站 2022-05-15 16:05:21
...

分析器通过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 算法