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. Windows实用小工具–Windows远程协助

    在企业里,有的公司办公区域比较大,电脑有问题一般都是通过远程.徒步.电话等方式来解决,对于远程协助解决问题,我们首先想到的会是如何连接对方的电脑,相信大家都已经使用过很多的软件了吧!当然还有Micro ...

  2. CSS3 Generator提供了13个CSS3较为常用的属性代码生成工具,而且可以通过这款工具除了在线生成效果代码之外,还可以实时看到你修改的效果,以及浏览器的兼容性。

    CSS3 Generator提供了13个CSS3较为常用的属性代码生成工具,而且可以通过这款工具除了在线生成效果代码之外,还可以实时看到你修改的效果,以及浏览器的兼容性. CSS3 Generator ...

  3. Dependency Injection 筆記 (2)

    续上集,接着要说明如何运用 DI 来让刚才的范例程序具备执行时期切换实现类型的能力. (本文摘自電子書<.NET 依賴注入>) 入门范例—DI 版本 为了让 AuthenticationS ...

  4. VS2013编译Qt5.6.0静态库,并提供了百度云下载(乌合之众)good

    获取qt5.6.0源码包 直接去www.qt.io下载就好了,这里就不详细说了. 这里是我已经编译好的** 链接:http://pan.baidu.com/s/1pLb6wVT 密码: ak7y ** ...

  5. 浅析C#代理

    delegate 是委托声明的基础,是.net 的委托的声明的关键字action 是基于delegate实现的代理 有多个参数(无限制个数)无返回值的代理 func 是基于delegate实现的代理 ...

  6. hadoop 数据抽取

    #!/bin/bash if [ ! -z $2 ]; then start_time=$1 end_time=$2 else starttime=`date +%Y%m%d%H%M -d '-15 ...

  7. HTML连载15-文本属性&颜色控制属性

    一.文本装饰的属性 1.格式:text-decoration:underline; 2.取值: (1)underline代表下划线 (2)line-through代表删除线 (3)overline代表 ...

  8. MyBatis中二级缓存和延时加载同时开启的问题

    首先,二级缓存默认不开启! 要配置 <setting name="cacheEnabled" value="true"/> 在MyBatis中:一级 ...

  9. Spring boot中Spring-Data-JPA操作MySQL数据库时遇到的错误(一)

    执行遇到如下错误: 看错误时要注意两点: 1.控制台报错情况,一般情况下红色第一行很重要,举例:上图info之下,蓝底标出的部分. 2.这种一般是以堆栈形式描述的,也就是重点在栈底的最后的一个完整的句 ...

  10. Python自学day-14

    一.默认创建的HTML5文件 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...