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. 【码云周刊第 23 期】Web 高效开发必备的 PHP 框架(从这里学起)good

    码云项目推荐 1.项目名称:多功能 THinkPHP 开源框架 项目简介:使用 THinkPHP 开发项目的过程中把一些常用的功能或者第三方 sdk 整合好,开源供亲们参考,如 Auth 权限管理.支 ...

  2. CWnd和HWND的区别(hWnd只是CWnd对象的一个成员变量,代表与这个对象绑定的窗口)

            所有控件类都是CWnd类的派生类,CWnd的所有成员函数在控件类中都可以使用.在MFC中,CWnd类是一个很重要的类,它封装了Windows的窗口句柄HWND.在Windows编程中, ...

  3. Bootstrap3.0学习(一)

    Bootstrap是Twitter退出的一个开源的用于前端开发的工具包.它由Twitter的设计师Mark Otto和Jacob Thornton合作开发,是一个CSS/HTML框架.Bootstra ...

  4. Realm_King 之 .NET 打包详细教程(B)

    上篇(Realm_King 之 .NET 打包详细教程(A))给大家讲述了打包基本的操作,接下来帮助大家如何覆盖安装,希望大家仔细阅读... (一)看到你的解决方案,选中你的安装程序,点击F4会弹出改 ...

  5. ABAP中SPLIT关键字 当分隔符位于字符串首尾时

    使用SPLIT关键字将一个字符串按某个分隔符拆分,如果分隔符穿插在字符串中间(即首尾字符均不是分隔符的情况),我们很容易知道拆分后的结果,但如果分隔符恰好位于字符串首或者末尾呢? 如下面的代码所示,在 ...

  6. Linux下python多版本多环境介绍

     一.python多版本配置说明 安装python相关依赖 [root@centos6 ~]# yum install -y gcc make patch gdbm-devel openssl-dev ...

  7. localstorage实现带过期时间的缓存功能

    前言 一般可以使用cookie,localstorage,sessionStorage来实现浏览器端的数据缓存,减少对服务器的请求. 1.cookie数据存放在本地硬盘中,只要在过期时间之前,都是有效 ...

  8. spark 2.x在windows环境使用idea本地调试启动了kerberos认证的hive

    1 概述 开发调试spark程序时,因为要访问开启kerberos认证的hive/hbase/hdfs等组件,每次调试都需要打jar包,上传到服务器执行特别影响工作效率,所以调研了下如何在window ...

  9. 视频技术详解:RTMP H5 直播流技术解析

    本文聚焦 RTMP 协议的最精华的内容,接进行实际操作 Buffer 的练习和协议的学习. RTMP 是什么 RTMP 全称即是 Real-Time Messaging Protocol.顾名思义就是 ...

  10. Scala 学习之路(十)—— 函数 & 闭包 & 柯里化

    一.函数 1.1 函数与方法 Scala中函数与方法的区别非常小,如果函数作为某个对象的成员,这样的函数被称为方法,否则就是一个正常的函数. // 定义方法 def multi1(x:Int) = { ...