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的基本使用的更多相关文章

  1. lucene 基础知识点

    部分知识点的梳理,参考<lucene实战>及网络资料 1.基本概念 lucence 可以认为分为两大组件: 1)索引组件 a.内容获取:即将原始的内容材料,可以是数据库.网站(爬虫).文本 ...

  2. 用lucene替代mysql读库的尝试

    采用lucene对mysql中的表建索引,并替代全文检索操作. 备注:代码临时梳理很粗糙,后续修改. import java.io.File; import java.io.IOException; ...

  3. Lucene的评分(score)机制研究

    首先,需要学习Lucene的评分计算公式—— 分值计算方式为查询语句q中每个项t与文档d的匹配分值之和,当然还有权重的因素.其中每一项的意思如下表所示: 表3.5 评分公式中的因子 评分因子 描 述 ...

  4. Lucene的分析资料【转】

    Lucene 源码剖析 1 目录 2 Lucene是什么 2.1.1 强大特性 2.1.2 API组成- 2.1.3 Hello World! 2.1.4 Lucene roadmap 3 索引文件结 ...

  5. Lucene提供的条件判断查询

    第一.按词条搜索 - TermQuery query = new TermQuery(new Term("name","word1"));hits = sear ...

  6. Lucene 单域多条件查询

    在Lucene 中 BooleanClause用于表示布尔查询子句关系的类,包括:BooleanClause.Occur.MUST表示and,BooleanClause.Occur.MUST_NOT表 ...

  7. lucene自定义过滤器

    先介绍下查询与过滤的区别和联系,其实查询(各种Query)和过滤(各种Filter)之间非常相似,可以这样说只要用Query能完成的事,用过滤也都可以完成,它们之间可以相互转换,最大的区别就是使用过滤 ...

  8. lucene+IKAnalyzer实现中文纯文本检索系统

    首先IntelliJ IDEA中搭建Maven项目(web):spring+SpringMVC+Lucene+IKAnalyzer spring+SpringMVC搭建项目可以参考我的博客 整合Luc ...

  9. 全文检索解决方案(lucene工具类以及sphinx相关资料)

    介绍两种全文检索的技术. 1.  lucene+ 中文分词(IK) 关于lucene的原理,在这里可以得到很好的学习. http://www.blogjava.net/zhyiwww/archive/ ...

  10. MySQL和Lucene索引对比分析

    MySQL和Lucene都可以对数据构建索引并通过索引查询数据,一个是关系型数据库,一个是构建搜索引擎(Solr.ElasticSearch)的核心类库.两者的索引(index)有什么区别呢?以前写过 ...

随机推荐

  1. 什么是MonoGame?

    MonoGame是XNA的一个开源实现.主要用于游戏开发. 官方网站:http://www.monogame.net/ 源码地址:https://github.com/MonoGame/MonoGam ...

  2. java的clone()、浅拷贝与深拷贝

    clone()方法是Object的native方法.protected native Object clone() throws CloneNotSupportedException;  声明为pro ...

  3. 高斯判别分析模型( Gaussian discriminant analysis)及Python实现

    高斯判别分析模型( Gaussian discriminant analysis)及Python实现 http://www.cnblogs.com/sumai 1.模型 高斯判别分析模型是一种生成模型 ...

  4. 使用Disk2VHD进行P2V转换需要知道的一些事

    据不可靠统计,有「无数」工具可以实现物理机到虚拟机的(P2V)转换,虽然有很多此类工具都被开发商帖上了高价标签,但至少来自微软 Sysinternals 工具集中的 Disk2VHD 还是可以免费使用 ...

  5. qt5.6 webengine兼容xp的编译方法

    http://www.qtcn.org/bbs/read-htm-tid-62470.html http://stackoverflow.com/questions/31678657/qtwebeng ...

  6. 利用Delphi实现网络监控系统

    实现的原理WINSOCK是一组API,用于在INTE.Net上传输数据和交换信息.用它编程本来是很麻烦的,但在DELPHHI中并不需要直接与WINSOCK的API打交道,因为TclientSocket ...

  7. Dependency Injection 筆記 (1)

    <.NET 依賴注入>連載 (1) 本文从一个基本的问题开始,点出软件需求变动的常态,以说明为什么我们需要学习「依赖注入」(dependency injection:简称 DI)来改善设计 ...

  8. Qt 中C++ static_cast 和 reinterpret_cast的区别(static_cast是隐式类型转换,会有数据损失,reinterpret_cast是底层二进制转换,没有数据损失)

    1.C++中的static_cast执行非多态的转换,用于代替C中通常的转换操作.因此,被做为隐式类型转换使用.比如: int i; float f = 166.7f; i = static_cast ...

  9. VM中linux和windows主机进行串口通信

    最近在做关于AIS的内容.为了对AIS电文进行解码,串口收发. 数据有PC机模拟发送,为了调试方便,不用次次将程序放到开发板上运行,所以利用pc主机和虚拟机进行串口通信模拟该过程. 首先需要用到一个软 ...

  10. uni-app $refs的基本用法

    $refs的基本用法 一个对象(Object),持有注册过 ref 特性 的所有 DOM 元素和组件实例. <template> <view class="containe ...