使用 Lucene 执行搜索,首先要创建 IndexSearcher 对象,然后通过 Term 和 Query 对象来封装用户输入的搜索条件,最后将结果封装在 Hits 对象中,返回给用户。
1. 创建搜索器对象 IndexSearhcer
IndexSearcher searcher = new IndexSearcher( 索引存放的路径 );
对象的 search() 方法返回一个结果集对象
Hits h = searcher.search();
关闭 IndexSearhcer 对象
search.close();
2. 封装搜索条件: Term 和 Query 对象
创建 Term 对象封装搜索条件
Term t = new Term(“ 字段名称 ”, “ 关键词 ”);
创建 Query 对象来把 Term 对象转化成可执行的查询条件, Query 对象有很多种,最常用的一种
Query q = new TermQuery( t );
3. 执行搜索
把请求传递给 IndexSearhcer 对象,执行搜索
Hits hs = searcher.search(q);
4. 提取搜索结果, Hits 对象
先从 Hits 对象中,获取 Document ,再从 Document 中获取 Field
Hits 对象的常用方法:
l Document doc(int n)
返回指定序号的 Document
l int id(int n);
返回指定序号的 Document 的 id 属性
l int length();
返回 Hits 对象的长度,也就是 Hits 对象中包含的 Document 的数量
l float score(int n);
返回指定序号的 Document 的 score 属性,即文档得分
5. 提取搜索结果: Document 对象
Document 对象的常用方法:
l Field getField(String name);
获取指定 Field 名称的 Field 对象
l List getFields()
无参数,返回值是 List 类型,包含该 Document 的所有 Field
l Enumeration fields()
无参数,返回值是 Enumeration 类型,包含该 Document 的所有 Field
l String get(String name)
参数是 Field 名称,返回该 Field 对象的字符串值
6. 提取搜索结果: Field 对象
l byte[] binaryValue()
获取指定 Field 对象的二进制值
l Reader readerValue()
获取指定 Field 对象的内容,以 Reader 形式返回
l String stringValue()
获取指定 Field 对象的字符串值,最为常用
实例:
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.document.*;
public class BasicSearcher2{
public static void main(String args[]) throws java.io.IOException{
String indexPath = "index"; // 索引存放位置
String searchField = "content"; // 搜索字段
String searchPhrase = " 禹 "; // 关键字
StringBuffer sb = new StringBuffer(); // 保留提取的字符串
// 创建 IndexSearhcer 对象
IndexSearcher searcher = new IndexSearcher(indexPath);
// 封装搜索条件
Term t = new Term(searchField, searchPhrase);
Query q = new TermQuery(t);
// 反馈搜索结果
Hits hs = searcher.search(q);
// 从 Hits 对象中提取搜索结果
int num = hs.length(); //Document 对象个数
for(int i=0; i<num; i++){
// 获取 Document
Document doc = hs.doc(i);
//field name
Field fname = doc.getField("name");
sb.append("name:"+"/n");
sb.append(fname.stringValue()+"/n");
//field content
Field fcontent = doc.getField("content");
sb.append("content"+"/n");
sb.append(fcontent.stringValue().substring(0,50)+"/n"); // 提取前 50 作为摘要
sb.append("------------------- " + "/n");
}
searcher.close();
System.out.print(sb);
}
}
结果:
中文分词问题:
上述程序的搜索关键词是“禹”,有 3 条结果。
如果把关键词改为“大禹”的话,虽然文本中有“大禹”这个词,但 Lucene 无法检索到结果,这是因为 Lucene 默认情况下(即使用 StandardAnalyzer 的时候)对中文进行单字分词,即为每个字建立索引,因此在索引中没有和“大禹”相对应的。