Lucene04-Lucene的基本使用

导入的包

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;
import org.omg.CORBA.PUBLIC_MEMBER; import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

1、修改

    /**
* 修改
*
* @throws IOException
*/
@Test
public void updateDoc() throws IOException {
// 1 创建索引库对象,指定索引库的位置
//1.1 创建索引库位置
Path path = new File("D:\\lucene").toPath();
//1.2 创建索引库对象,关联索引库位置
FSDirectory directory = FSDirectory.open(path);
// 2 创建IndexWriterConfig对象并指定分词器对象
//2.1 创建分词器对象用于指定分词规则
StandardAnalyzer standardAnalyzer = new StandardAnalyzer();//标准分词器,分词规则:单字分词
//2.2 创建写出器配置对象,关联分词器对象
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(standardAnalyzer);
// 3 创建一个IndexWriter对象 &指定索引库的位置&指定一个IndexWriterConfig对象。
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
// 4 构建修改的doc文档数据.
Document document = new Document();
// 5 创建field对象,将field添加到document对象中。
document.add(new StringField("docId", "docIdOne", Field.Store.YES));
document.add(new TextField("title", "修改的是文档2", Field.Store.YES));
document.add(new TextField("content", "我的祖国是一个伟大的国家", Field.Store.YES));
document.add(new StringField("score", "100", Field.Store.YES));
document.add(new StoredField("name", "张三"));
//6 执行修改(删除一些或者一个,再新增一个),按照docId分词查询文档,查到对应文档就修改,查不到就新增
indexWriter.updateDocument(new Term("docId", "docIdTwo"), document);
//按照title分词查询文档,查到对应文档就修改,查不到就新增 第二个参数是List<Document>,批量修改
// List<Document> documents = Arrays.asList(document);
// indexWriter.updateDocuments(new Term("title", "智"), Arrays.asList(document));
//修改完提交
indexWriter.commit();
//7 关闭indexWriter
indexWriter.close();
}

2、批量新增

    /**
* 批量新增
*
* @throws IOException
*/
@Test
public void batchAddDoc() throws IOException {
//1 创建索引库对象&指定索引库的位置
FSDirectory directory = FSDirectory.open(new File("D:\\lucene").toPath());
//2 创建IndexWriterConfig对象并指定分词器对象
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
//3 创建一个IndexWriter对象 &指定索引库的位置&指定一个IndexWriterConfig对象。
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
//4 批量创建document对象。
//4.1 创建list集合用来存储Document对象
List<Document> documents = new ArrayList<>();
//4.2 创建10个Document对象
for (int i = 0; i < 10; i++) {
//4.3 创建document对象。
Document document = new Document();
//4.4 将field添加到document对象中。
document.add(new StringField("docId", "docId" + i, Field.Store.YES));
document.add(new TextField("title", "我的祖国" + i, Field.Store.YES));
document.add(new TextField("content", "我的祖国是一个伟大的国家" + i, Field.Store.YES));
document.add(new StringField("score", "100" + i, Field.Store.YES));
document.add(new StoredField("name", "张三" + i));
//4.4 添加到集合中
documents.add(document);
}
//5 使用indexwriter对象将documents对象批量写入索引库中。
indexWriter.addDocuments(documents);
//6 关闭indexwriter对象。
indexWriter.close();
}

3、删除

3.1 删除所有

    /**
* 删除所有
*
* @throws IOException
*/
@Test
public void deleteAllDoc() throws IOException {
//1 创建索引库对象,指定索引库的位置
FSDirectory directory = FSDirectory.open(new File("D:\\lucene").toPath());
// 2 创建IndexWriterConfig对象并指定分词器对象
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
// 3 创建一个IndexWriter对象 &指定索引库的位置&指定一个IndexWriterConfig对象。
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
//4 执行方法删除所有
indexWriter.deleteAll();
//5 关闭资源
indexWriter.close();
}

3.2 根据查询条件删除

    /**
* 根据查询条件删除
*
* @throws IOException
*/
@Test
public void deleteDocByQuery() throws IOException {
//1 创建索引库对象&指定索引库的位置
FSDirectory directory = FSDirectory.open(new File("D:\\lucene").toPath());
//2 创建IndexWriterConfig对象并指定分词器对象
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
//3 创建一个IndexWriter对象 &指定索引库的位置&指定一个IndexWriterConfig对象。
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
//4 执行删除 先查询然后把满足查询条件的数据进行查询 好比在mysql 先select然后delete
//4.1 指定查询条件 & 关联分词对象
TermQuery query = new TermQuery(new Term("title", "9"));
//4.2 执行删除
indexWriter.deleteDocuments(query);
//5 关闭资源
indexWriter.close();
}

3.3 根据分词删除

    /**
* 根据分词删除
*
* @throws IOException
*/
@Test
public void deleteDocByTerm() throws IOException {
//1 创建索引库对象&指定索引库的位置
FSDirectory directory = FSDirectory.open(new File("D:\\lucene").toPath());
//2 创建IndexWriterConfig对象并指定分词器对象
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
//3 创建一个IndexWriter对象 &指定索引库的位置&指定一个IndexWriterConfig对象。
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
//4 执行删除 直接删除 好比在mysql 直接delete
//4.1 指定分词
Term term = new Term("title", "8");
//4.2 执行删除
indexWriter.deleteDocuments(term);
//5 关闭资源
indexWriter.close();
}

4、查询

参考Lucene02--入门程序

最新文章

  1. Redis 3.0.5 集群的命令、使用、维护
  2. vmware网卡设置详解
  3. 如何让C#像JavaScript一样编程
  4. Github优秀java项目集合(中文版) - 涉及java所有的知识体系
  5. php strtotime函数服务器和本地不相同
  6. 确认(confirm 消息对话框)
  7. Carthage&amp;&amp;cocopads 摘抄笔记
  8. Linux命令初步了解
  9. 完全卸载oracle10G/11G步骤
  10. 第一个简单的maven项目
  11. 痞子衡嵌入式:语音处理工具Jays-PySPEECH诞生记 - 索引
  12. WPF自定义用户控件不显示
  13. 深入理解Java中的反射机制
  14. has invalid type &lt;class &#39;numpy.ndarray&#39;&gt;, must be a string or Tensor
  15. Jmeter 传值对比
  16. python学习之旅(三)
  17. Windows下安装Oracle12C(二)
  18. iptables相关操作以及简单理解端口和服务之间关系
  19. MyCat - 背景篇(1)
  20. C/C++之static

热门文章

  1. Cannot read property &#39;apply&#39; of undefined
  2. DateTimeToGreenUnix
  3. Linux下卸载QT SDK
  4. 虚拟机安装 ubuntu 后,更新源无效,以及无法联网安装软件的问题
  5. 层次关系表格,不用递归,快速检索。HierarchyId
  6. jquery事件和动画操作集锦
  7. asp.net mvc下实现微信公众号(JsApi)支付介绍
  8. oracle 和 mysql 常用语句对比汇总
  9. spring 5.x 系列第13篇 —— 整合RabbitMQ (xml配置方式)
  10. 浅入深出Vue:事件处理