Lucene_demo01_FirstProject
lucene版本:3.0.1
导入下面四个包
Junit4测试类:
/**
* 创建一个索引库,把一个信息加入到索引库中、把信息从索引库中检索出来
*/
public class HelloLuceneWorld {
/**
* 创建索引
*/
@Test
public void createIndex() throws Exception {
// 1创建article对象
Article article = new Article();
article.setId(1L);
article.setTitle("NBA总决赛");
article.setContent("LBJ和韦德能带领热火在2013赛季拿到NBA总冠军吗?");
// 创建索引库,创建IndexWriter对象
Directory directory = FSDirectory.open(new File("./indexDir"));
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
IndexWriter indexWriter = new IndexWriter(directory, analyzer, MaxFieldLength.LIMITED);
// 把article对象转化成document、把article对象加入到索引库中
Document document = new Document();
Field idField = new Field("id", article.getId().toString(), Store.YES, Index.NOT_ANALYZED);
Field titleField = new Field("title", article.getTitle(), Store.YES, Index.ANALYZED);
Field contentField = new Field("content", article.getContent(), Store.YES, Index.ANALYZED);
document.add(idField);
document.add(titleField);
document.add(contentField);
indexWriter.addDocument(document);// 信息放入到索引库
indexWriter.close();// 关闭indexWriter
}
/**
* 进行检索
*/
@Test
public void searchIndex() throws Exception {
// 创建IndexSearch对象
Directory directory = FSDirectory.open(new File("./indexDir"));
IndexSearcher indexSearcher = new IndexSearcher(directory);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
QueryParser queryParser = new QueryParser(Version.LUCENE_30, "title", analyzer);
Query query = queryParser.parse("总决赛");// 关键词
TopDocs topDocs = indexSearcher.search(query, 5);// 第二个参数为n,提取前n条记录
int count = topDocs.totalHits;// 根据关键词检索出来的总的记录数
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
List<Article> articleList = new ArrayList<Article>();
for (int i = 0; i < scoreDocs.length; i++) {
int index = scoreDocs[i].doc;
Document document = indexSearcher.doc(index);
Article article = new Article();
article.setId(Long.parseLong(document.get("id")));
article.setTitle(document.get("title"));
article.setContent(document.get("content"));
articleList.add(article);
}
// 输出查询到的信息
for (Article article : articleList) {
System.out.println(article.getId());
System.out.println(article.getTitle());
System.out.println(article.getContent());
}
}
}
分享到:
相关推荐
可用lucene demo 已经有入门级pdf学习
lucene_in_action中文版lucene_in_action中文版lucene_in_action中文版lucene_in_action中文版lucene_in_action中文版lucene_in_action中文版
Lucene+Heritrix搜索引擎的一个成功案例 市值30000万 只需下载,用Eclipse-import为web工程就可以了 需要安装mysql 5.5 同时由于此工程为web工程所以假如您的Eclipse没有安装tomcatPlugin的话,请也同时安装tomcat...
Lucene资料大全,包括:Lucene_in_Action书,尚学堂科技_张志宇_lucene.ppt等
lucene_jar包
大型网站站内搜索(Lucene_In_Action).pdf 大型网站站内搜索(Lucene_In_Action).pdf
Lucene_3.0_原理与代码分析
Lucene_in_Action 中文版
这个压缩包包含lucene_3.6.1_API,后缀格式为chm. .;...
一个vb.net2.0做的Lucene索引测试 vb.net的东西比较少吧,我上一个
JAVA_Lucene_in_Action教程完整版
实现的的索引建立 以及全文检索等功能……
对clucene0.9.21版本在vc6下调试demo希望对你的开发有帮助
Lucene_in_Action english版
compass_lucene_source
Lucene_in_Action(中文版).pdf Lucene in Action(中文版) ----------------------------------------- Lucene in Action 中文版 第一部分 Lucene 核心 1. 接触 Lucene 2. 索引 3. 为程序添加搜索 4. ...
Lucene_in_Action(中文版).pdf
c#中文分词类库+可用来做搜索引擎分词+准确率90 +支持Lucene.net.rar