SearchDoucment public class SearchDocument {@SuppressWarnings({ "resource", "static-access" })public static void main(String[] args) throws Exception {// 目录Directory directory = FSDirectory.open(new File("index"));// 目录阅读器Inde
public class SearchDocument {
@SuppressWarnings({ "resource", "static-access" })
public static void main(String[] args) throws Exception {
// 目录
Directory directory = FSDirectory.open(new File("index"));
// 目录阅读器
IndexReader indexReader = DirectoryReader.open(directory);
// 搜索对象indexSearch
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
// 使用标准分词器
Analyzer analyzer = new StandardAnalyzer();
// Query 搜索条件
QueryParser queryParser = new QueryParser("title", analyzer);
// 设置分词
Query query = queryParser.parse("客户");
// 搜索文档 (搜索条件,命中记录)
TopDocs topDocs = indexSearcher.search(query, 10);
// 存储文档的数据信息
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
System.out.println("命中的记录数:" + topDocs.totalHits);
// 遍历结果
for (ScoreDoc scoreDoc : scoreDocs) {
System.out.println("文档得分:" + scoreDoc.score);
Document document = indexSearcher.doc(scoreDoc.doc);
System.out.println("id=" + document.get("id"));
System.out.println("title=" + document.get("title"));
System.out.println("sellpoint=" + document.get("sellpoint"));
}
}
}
