论坛首页 入门技术论坛

lucene 多关键字中文搜索的问题

浏览 6101 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-04-26  
package ch11;

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();
}
}
}
}

查询不到结果

如果改成查找“计”和“教”可以查询到结果,英文没有问题
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics