Lucene_demo00_IndexCURD
来源:互联网 发布:网络配音兼职网站推荐 编辑:程序博客网 时间:2024/06/06 12:59
- /**
- * @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();
- }
- }
0 0
- Lucene_demo00_IndexCURD
- Android实战简易教程<六十五>(自定义控件实现数字液晶时钟Demo)
- spi总线
- Cg two-sided per-vertex lighting
- iOS包管理工具——Cocoapods详解
- 第7周 项目4 - 队列数组
- Lucene_demo00_IndexCURD
- Host文件初识
- Lucene_demo02_分词
- Logstash处理json格式日志文件的三种方法
- Lucene_demo03_索引库整理
- iOS开发——基于corelocation位置定位——工具类
- Lucene_demo04_分页
- 第七周 项目4-队列数组
- 第七周项目2—建立链队算法库