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 ...
随机推荐
- 课外知识----ini
ini 初始化英文单词的缩写,用来初始化参数 ini文件配置 [小节] 键=值 [小节] 键=值
- C++ Primer 笔记——OOP
1.基类通常都应该定义一个虚析构函数,即使该函数不执行任何实际操作也是如此. 2.任何构造函数之外的非静态函数都可以是虚函数,关键字virtual只能出现在类内部的声明语句之前而不能用于类外部的函数定 ...
- js两种写法执行速度比较
记录 function test1(){ this.say = function(){} } function test2(){ this.say = function(){} return this ...
- 专注笔试算法20年(C语言版)
1.C语言实现链表数据的反转({1,2,3,4}->{4,3,2,1}). int trav(PNode *head){ PNode p_1,p_2,tmp; //判断参数是否有效 if(*he ...
- 插件使用一树形插件---zTree一zTree异步加载
zTree 可以实现异步加载.异步加载可以让初次加载速度快,带来好的用户体验. 异步加载 官方源码中的demo提示了例子.例子是采用php语言. 在java语言中,zTree如何与Servlet结合呢 ...
- 微信小程序--代码构成---WXML 模板
WXML 模板 从事过网页编程的人知道,网页编程采用的是 HTML + CSS + JS 这样的组合,其中 HTML 是用来描述当前这个页面的结构,CSS 用来描述页面的样子,JS 通常是用来处理这个 ...
- NodeJs——router报错原因
rout.js var http = require('http'); var url = require('url'); var router = require('./models/router. ...
- SQL查询数据并插入新表
SQL查询数据并插入新表 --如果接受数据导入的表已经存在 insert into 表 select * from tablename --如果导入数据并生成表 select * into 表 fro ...
- autofac 创建实例方法总结
1.InstancePerDependency 对每一个依赖或每一次调用创建一个新的唯一的实例.这也是默认的创建实例的方式. 官方文档解释:Configure the component so tha ...
- 修改Tomcat默认连接数
<Connector port=" protocol="HTTP/1.1" connectionTimeout=" redirectPort=" ...