浏览 6101 次
锁定老帖子 主题:lucene 多关键字中文搜索的问题
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2007-04-26
import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.PhraseQuery; public class PhraseQueryTest { public static void main(String[] args) throws Exception { Document doc1 = new Document(); doc1.add(Field.Text("content", "david mary 计算机基础教程smith robert")); doc1.add(Field.Keyword("title", "doc1")); IndexWriter writer = new IndexWriter("e:\\index", new StandardAnalyzer(), true); writer.setUseCompoundFile(true); writer.addDocument(doc1); writer.close(); IndexSearcher searcher = new IndexSearcher("e:\\index"); Term word1 = new Term("content", "计算机"); Term word3 = new Term("content", "教程"); Hits hits = null; PhraseQuery query = null; query = new PhraseQuery(); query.add(word1); query.add(word3); query.setSlop(100); hits = searcher.search(query); printResult(hits, "'david'与'mary'紧紧相隔的Document"); } public static void printResult(Hits hits, String key) throws Exception { System.out.println("查找 \"" + key + "\" :"); if (hits != null) { if (hits.length() == 0) { System.out.println("没有找到任何结果"); System.out.println(); } else { System.out.print("找到"); for (int i = 0; i < hits.length(); i++) { Document d = hits.doc(i); String dname = d.get("title"); System.out.print(dname + " "); } System.out.println(); System.out.println(); } } } } 查询不到结果 如果改成查找“计”和“教”可以查询到结果,英文没有问题 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |