Lucene_demo00_IndexCURD
索引库创建、查询 、修改 、删除
/**
* @see 创建索引库,把article对象加入到索引库中
* @see 查询 、修改 、删除
*/
public class ArticleIndexCUDR {
/**
* 创建索引库
* @throws Exception
*/
@Test
public void testCreateIndex() throws Exception {
IndexWriter indexWriter = new IndexWriter(LuceneUtils.directory, LuceneUtils.analyzer, MaxFieldLength.LIMITED);
Article article = new Article();
article.setId(1L);
article.setTitle("NBA总冠军");
article.setContent("LBJ和韦德能带领热火在2013赛季拿到NBA总冠军吗");
Document doc = DocumentUtils.article2Document(article);
indexWriter.addDocument(doc);
indexWriter.close();// 每次都要关闭
}
/**
* 批量创建索引库
* @throws Exception
*/
@Test
public void testCreateIndexBatch() throws Exception {
IndexWriter indexWriter = new IndexWriter(LuceneUtils.directory, LuceneUtils.analyzer, MaxFieldLength.LIMITED);
for (int i = 1; i <= 25; i++) {
Article article = new Article();
article.setId(Long.parseLong("" + i));
article.setTitle("NBA总冠军");
article.setContent("LBJ和韦德能带领热火在2013赛季拿到NBA总冠军吗");
Document doc = DocumentUtils.article2Document(article);
indexWriter.addDocument(doc);
}
indexWriter.close();
}
/**
* 查找索引
* @throws Exception
*/
@Test
public void testSearchIndex() throws Exception {
IndexSearcher indexSearcher = new IndexSearcher(LuceneUtils.directory);
QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_30, new String[] { "title", "content" }, LuceneUtils.analyzer);
Query query = queryParser.parse("NBA");
TopDocs topDocs = indexSearcher.search(query, 25);
int count = topDocs.totalHits;// 总的抓取命中的记录数
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
List<Article> articleList = new ArrayList<Article>();
// 将查询到的索引添加到articleList中
for (int i = 0; i < scoreDocs.length; i++) {
int index = scoreDocs[i].doc;
Document document = indexSearcher.doc(index);
Article article = DocumentUtils.document2Article(document);
articleList.add(article);
}
// 输出查询到的内容
for (Article article : articleList) {
System.out.println(article.getId());
System.out.println(article.getTitle());
System.out.println(article.getContent());
}
}
/**
* 删除操作是针对关键词对象进行删除的,封装的关键词在目录库中必须存在才能删除
* @throws Exception
*/
@Test
public void deleteIndex() throws Exception {
IndexWriter indexWriter = new IndexWriter(LuceneUtils.directory, LuceneUtils.analyzer, MaxFieldLength.LIMITED);
Term term = new Term("title", "总冠军");// Term 关键词对象
indexWriter.deleteDocuments(term);
indexWriter.close();
}
/**
* 修改是先删除后增加
* @throws Exception
*/
@Test
public void testUpdateIndex() throws Exception {
IndexWriter indexWriter = new IndexWriter(LuceneUtils.directory, LuceneUtils.analyzer, MaxFieldLength.LIMITED);
Term term = new Term("title", "总冠军");
Article article = new Article();
article.setId(1L);
article.setTitle("热火总冠军");
article.setContent("热火在2013赛季拿到NBA总冠军");
Document doc = DocumentUtils.article2Document(article);
indexWriter.updateDocument(term, doc);// 第一个参数:term作用是删除、第二个参数:documet作用是增加
indexWriter.close();
}
}
分享到:
相关推荐
可用lucene demo 已经有入门级pdf学习
lucene_in_action中文版lucene_in_action中文版lucene_in_action中文版lucene_in_action中文版lucene_in_action中文版lucene_in_action中文版
Lucene资料大全,包括:Lucene_in_Action书,尚学堂科技_张志宇_lucene.ppt等
大型网站站内搜索(Lucene_In_Action).pdf 大型网站站内搜索(Lucene_In_Action).pdf
lucene_jar包
Lucene_3.0_原理与代码分析
一个vb.net2.0做的Lucene索引测试 vb.net的东西比较少吧,我上一个
Lucene_in_Action 中文版
JAVA_Lucene_in_Action教程完整版
这个压缩包包含lucene_3.6.1_API,后缀格式为chm. .;...
实现的的索引建立 以及全文检索等功能……
Lucene_in_Action english版
Lucene_in_Action(中文版).pdf Lucene in Action(中文版) ----------------------------------------- Lucene in Action 中文版 第一部分 Lucene 核心 1. 接触 Lucene 2. 索引 3. 为程序添加搜索 4. ...
对clucene0.9.21版本在vc6下调试demo希望对你的开发有帮助
Lucene_in_ Action java search engine.PDF 搜索引擎教程
c#中文分词类库+可用来做搜索引擎分词+准确率90 +支持Lucene.net.rar
Lucene_in_Action(中文版).pdf
compass_lucene_source