Lucene04-Lucene的基本使用
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--入门程序
Lucene04-Lucene的基本使用的更多相关文章
- lucene 基础知识点
部分知识点的梳理,参考<lucene实战>及网络资料 1.基本概念 lucence 可以认为分为两大组件: 1)索引组件 a.内容获取:即将原始的内容材料,可以是数据库.网站(爬虫).文本 ...
- 用lucene替代mysql读库的尝试
采用lucene对mysql中的表建索引,并替代全文检索操作. 备注:代码临时梳理很粗糙,后续修改. import java.io.File; import java.io.IOException; ...
- Lucene的评分(score)机制研究
首先,需要学习Lucene的评分计算公式—— 分值计算方式为查询语句q中每个项t与文档d的匹配分值之和,当然还有权重的因素.其中每一项的意思如下表所示: 表3.5 评分公式中的因子 评分因子 描 述 ...
- Lucene的分析资料【转】
Lucene 源码剖析 1 目录 2 Lucene是什么 2.1.1 强大特性 2.1.2 API组成- 2.1.3 Hello World! 2.1.4 Lucene roadmap 3 索引文件结 ...
- Lucene提供的条件判断查询
第一.按词条搜索 - TermQuery query = new TermQuery(new Term("name","word1"));hits = sear ...
- Lucene 单域多条件查询
在Lucene 中 BooleanClause用于表示布尔查询子句关系的类,包括:BooleanClause.Occur.MUST表示and,BooleanClause.Occur.MUST_NOT表 ...
- lucene自定义过滤器
先介绍下查询与过滤的区别和联系,其实查询(各种Query)和过滤(各种Filter)之间非常相似,可以这样说只要用Query能完成的事,用过滤也都可以完成,它们之间可以相互转换,最大的区别就是使用过滤 ...
- lucene+IKAnalyzer实现中文纯文本检索系统
首先IntelliJ IDEA中搭建Maven项目(web):spring+SpringMVC+Lucene+IKAnalyzer spring+SpringMVC搭建项目可以参考我的博客 整合Luc ...
- 全文检索解决方案(lucene工具类以及sphinx相关资料)
介绍两种全文检索的技术. 1. lucene+ 中文分词(IK) 关于lucene的原理,在这里可以得到很好的学习. http://www.blogjava.net/zhyiwww/archive/ ...
- MySQL和Lucene索引对比分析
MySQL和Lucene都可以对数据构建索引并通过索引查询数据,一个是关系型数据库,一个是构建搜索引擎(Solr.ElasticSearch)的核心类库.两者的索引(index)有什么区别呢?以前写过 ...
随机推荐
- WPF用DirectSound播放声音
示例代码: var fileName = @"D:\WindowsLogon.wav"; DevicesCollection sound_devices = new Devices ...
- 领域驱动设计(DDD)的实践经验分享之持久化透明
原文:领域驱动设计(DDD)的实践经验分享之持久化透明 前一篇文章中,我谈到了领域驱动设计中,关于ORM工具该如何使用的问题.谈了很多我心里的想法,大家也对我的观点做了一些回复,或多或少让我深深感觉到 ...
- flume本地调试
本机idea远程调试flume:https://blog.csdn.net/u012373815/article/details/60601118 遇到 [root@hadoop02 bin]# ./ ...
- MJPhotoBrowser 用法
一.使用方法: #import "MJPhotoBrowser.h" #import "MJPhoto.h" - (void)tapPhoto:(UIT ...
- 大数据基础之Kafka(1)简介、安装及使用
kafka2.0 http://kafka.apache.org 一 简介 Kafka® is used for building real-time data pipelines and strea ...
- 编译安装python
编译安装python 1.下载python3的原码包 1.1下载到到opt目录中 cd /opt 1.2下载python3的原码包 wget https://www.python.org/ftp/py ...
- Node.js实现PC端类微信聊天软件(二)
Github StackChat 用到的React-Router React-Router是React路由的解决方案之一,也可以使用别的库 安装 npm install react-router -- ...
- feign之间传递oauth2-token的问题和解决
在微服务架构里,服务与服务之间的调用一般用feign就可以实现,它是一种可视化的rpc,并且集成了ribbon的负载均衡能力,所以很受欢迎. 授权服务 在授权服务里,用户通过用户名密码,或者手机和验证 ...
- 【记录】Mysql数据库更新主键自增
语法:id从1000开始自增: ALTER TABLE 表名 AUTO_INCREMENT = 1000;
- java常用基础(一)
Java常用基础(一) 原文写于2017-12-02 输入输出 //输入 Scanner in = new Scanner(new BufferedInputStream(System.in)); i ...