Lucene_demo00_IndexCURD

本文介绍了一个使用Lucene实现的索引创建、查询、更新和删除的基本示例。示例通过Article对象演示了如何批量创建索引库、查询索引、删除特定条目及更新索引内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[color=orange][b][size=xx-large]Lucene_demo00_IndexCURD[/size][/b][/color]

索引库创建、查询 、修改 、删除



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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值