Lucene入门实例-CRUD
1、导入jar包
lucene-analyzers-common-7.6.0.jar
lucene-analyzers-smartcn-7.6.0.jar
lucene-core-7.6.0.jar
2、代码
package org.longIt.Lucene_app;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import java.nio.file.Paths;
public class LuceneIndex {
public static void main(String[] args) {
addIndex();
//searchIndex();
//deleteIndex();
//updateIndex();
}
private static void updateIndex() {
// TODO Auto-generated method stub
try {
//指定索引库的目录
Directory directory = FSDirectory.open(Paths.get("D:\\study\\lucene\\lucene_index\\article_tb"));
//创建分词器 暂时使用 单字分词器 后期再改善
Analyzer analyzer = new StandardAnalyzer();
//创建IndexWriterConfig实例,通过IndexWriterConfig实例来指定创建索引的相关信息,比如指定分词器
IndexWriterConfig config = new IndexWriterConfig(analyzer);
//指定索引的创建方式
config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
//创建索引 更新索引 删除索引都是IndexWriter来实现
IndexWriter indexWriter = new IndexWriter(directory,config);
//一个Document实例代表一条记录
Document document = new Document();
/**
* StringField不会对关键字进行分词
* Store.YES:会对数据进行存储并分词,如果为NO则不会对数据进行存储,索引还是会创建
*
* */
document.add(new StringField("articleId", "0001", Field.Store.YES));
document.add(new TextField("title", "幽幽而来", Field.Store.YES));
document.add(new TextField("content", "这世间,必有一种懂得是精神,穿越灵魂", Field.Store.YES));
/**
* 通过indexWriter将数据写入至索引库
* 更新的原理是先删除之前的索引,再创建新的索引,相当于更新是 删除与添加两个动作的合集
* **/
indexWriter.updateDocument(new Term("articleId","0001"), document);
//提交事务
indexWriter.commit();
//关闭流资源
indexWriter.close();
System.out.println("=======索引更新成功======");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void addIndex() {
try {
Directory directory = FSDirectory.open(Paths.get("D:\\study\\lucene\\lucene_index\\article_tb"));
//创建IndexWriterConfig实例,通过IndexWriterConfig实例来指定创建索引的相关信息,比如指定分词器
//创建分词器 暂时使用 单字分词器 后期再改善
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
//指定索引的创建方式
config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
//创建索引 更新索引 删除索引都是IndexWriter来实现
IndexWriter indexWriter = new IndexWriter(directory, config);
//一个Document实例代表一条记录
Document document = new Document();
/**
* StringField不会对关键字进行分词
* Store.YES:会对数据进行存储并分词,如果为NO则不会对数据进行存储,索引还是会创建
*
* */
document.add(new StringField("articleId", "0001", Field.Store.YES));
document.add(new TextField("title", "懂得人生0001", Field.Store.YES));
document.add(new TextField("content", "一生一世", Field.Store.YES));
//通过indexWriter将数据写入至索引库
indexWriter.addDocument(document);
//提交事务
indexWriter.commit();
//关闭流资源
indexWriter.close();
System.out.println("=======索引创建成功======");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void searchIndex() {
try {
Directory directory = FSDirectory.open(Paths.get("D:\\study\\lucene\\lucene_index\\article_tb"));
//DirectoryReader的open方法指定需要读取的索引库信息,并返回相应的实例
IndexReader indexReader = DirectoryReader.open(directory);
//创建IndexSearcher实例,通过IndexSearcher实例进行全文检索
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
/*
通过indexSearcher进行检索并指定两个参数
第一个参数:封装查询的相关信息,比如说查询的关键字,是否需要分词或者需要分词的话采取什么分词器
第二个参数:最多只要多少条记录
TermQuery:中指定了查询的关键字以及查询哪一个字段
TermQuery不会对关键字进行分词
*/
Query query = new TermQuery(new Term("title","幽"));
//查询索引表,最终数据都被封装在 TopDocs的实例中
TopDocs topDocs = indexSearcher.search(query,10);
//通过topDocs获取匹配全部记录
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
System.out.println("获取到的记录数:"+scoreDocs.length);
for (int i = 0; i < scoreDocs.length; i++) {
//获取记录的id
int id = scoreDocs[i].doc;
//文章的得分
float score = scoreDocs[i].score;
System.out.println("id:"+id+" 分章的得分:"+score);
//查询数据表
Document document = indexSearcher.doc(id);
String articleId = document.get("articleId");
String title = document.get("title");
String content = document.get("content");
System.out.println("articleId:"+articleId+" title:"+title+" content:"+content);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void deleteIndex() {
// TODO Auto-generated method stub
try {
//指定索引库的目录
Directory directory = FSDirectory.open(Paths.get("D:\\study\\lucene\\lucene_index\\article_tb"));
//创建分词器 暂时使用 单字分词器 后期再改善
Analyzer analyzer = new StandardAnalyzer();
//创建IndexWriterConfig实例,通过IndexWriterConfig实例来指定创建索引的相关信息,比如指定分词器
IndexWriterConfig config = new IndexWriterConfig(analyzer);
//指定索引的创建方式
config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
//创建索引 更新索引 删除索引都是IndexWriter来实现
IndexWriter indexWriter = new IndexWriter(directory,config);
//删除指定的索引
indexWriter.deleteDocuments(new Term("articleId","0001"));
//删除索引库中全部的索引
//indexWriter.deleteAll();
//提交事务
indexWriter.commit();
//关闭流资源
indexWriter.close();
System.out.println("=======索引删除成功======");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
Lucene入门实例-CRUD的更多相关文章
- Lucene建立索引搜索入门实例
第一部分:Lucene建立索引 Lucene建立索引主要有以下两步:第一步:建立索引器第二步:添加索引文件准备在f盘建立lucene文件夹,然后 ...
- springboot + mybatisPlus 入门实例 入门demo
springboot + mybatisPlus 入门实例 入门demo 使用mybatisPlus的优势 集成mybatisplus后,简单的CRUD就不用写了,如果没有特别的sql,就可以不用ma ...
- React 入门实例教程(转载)
本人转载自: React 入门实例教程
- struts入门实例
入门实例 1 .下载struts-2.3.16.3-all .不摆了.看哈就会下载了. 2 . 解压 后 找到 apps 文件夹. 3. 打开后将 struts2-blank.war ...
- Vue.js2.0从入门到放弃---入门实例
最近,vue.js越来越火.在这样的大浪潮下,我也开始进入vue的学习行列中,在网上也搜了很多教程,按着教程来做,也总会出现这样那样的问题(坑啊,由于网上那些教程都是Vue.js 1.x版本的,现在用 ...
- wxPython中文教程入门实例
这篇文章主要为大家分享下python编程中有关wxPython的中文教程,分享一些wxPython入门实例,有需要的朋友参考下 wxPython中文教程入门实例 wx.Window 是一个基类 ...
- Omnet++ 4.0 入门实例教程
http://blog.sina.com.cn/s/blog_8a2bb17d01018npf.html 在网上找到的一个讲解omnet++的实例, 是4.0下面实现的. 我在4.2上试了试,可以用. ...
- Spring中IoC的入门实例
Spring中IoC的入门实例 Spring的模块化是很强的,各个功能模块都是独立的,我们可以选择的使用.这一章先从Spring的IoC开始.所谓IoC就是一个用XML来定义生成对象的模式,我们看看如 ...
- Node.js入门实例程序
在使用Node.js创建实际“Hello, World!”应用程序之前,让我们看看Node.js的应用程序的部分.Node.js应用程序由以下三个重要组成部分: 导入需要模块: 我们使用require ...
随机推荐
- 异常小结:上一张图搞清楚Java的异常机制
下面是Java异常类的组织结构,红色区域的异常类表示是程序需要显示捕捉或者抛出的. Throwable Throwable是Java异常的顶级类,所有的异常都继承于这个类. Error,Excepti ...
- 使用python调用wps v9转换office文件到pdf
#!/usr/bin/python2.6 # -*- coding: utf-8 -*- # pip install timeout-decorator import os import win32c ...
- java 使用jdbc连接Greenplum数据库和Postgresql数据库
1.公司使用的Greenplum和Postgresql,确实让我学到不少东西.简单将使用jdbc连接Greenplum和Postgresql数据库.由于使用maven仓库,不能下载Greenplum的 ...
- 统计各个数据库的各个数据表的总数,然后写入到excel中
1.最近项目基本进入最后阶段了,然后会统计一下各个数据库的各个数据表的数据量,开始使用的报表工具,report-designer,开源的,研究了两天,发现并不是很好使,最后自己下班回去,晚上思考,想着 ...
- [转] Meida视频加密二-Blob对象
2. blob 1 <video src="blob:http://www.bilibili.com/d0823f0f-2b2a-4fd6-a93a-e4c82173c107" ...
- lojround6
花团 线段树分治裸题 给出了结束时间跟离线没区别 「LibreOJ Round #6」花火 首先在第一次使用交换是显然的 然后统计逆序对暴力是n^2的(前缀和优化) 因为交换两个点改变的只有x< ...
- centos 6.5升级内核到3.1
1.查看本机内核版本 [root@localhost ~]# uname -r 2.6.32-358.el6.x86_64 2.安装含有内核软件的源 步骤一:导入证书 [root@localhost ...
- asp.net core 2.0 webapi集成signalr
asp.net core 2.0 webapi集成signalr 在博客园也很多年了,一直未曾分享过什么东西,也没有写过博客,但自己也是汲取着博客园的知识成长的: 这两天想着不能这么无私,最近.N ...
- sparkStreaming消费kafka-0.8方式:direct方式(存储offset到zookeeper)
生产中,为了保证kafka的offset的安全性,并且防止丢失数据现象,会手动维护偏移量(offset) 版本:kafka:0.8 其中需要注意的点: 1:获取zookeeper记录的分区偏移量 2: ...
- net core体系-web应用程序-4net core2.0大白话带你入门-9asp.net core服务的生命周期
asp.net core服务的生命周期 Transient:每一次GetService都会创建一个新的实例 Scoped:在同一个Scope内只初始化一个实例 ,可以理解为( 每一个request ...