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 ...
随机推荐
- Python模拟人猜数过程(折半查找)
import random# (0,1000)随机产生一个数key = random.randint(1,1000)# 用来统计猜的次数count = 0 # 定义一个折半查找的函数def BinSe ...
- 计蒜客 X的平方根(二分法)
设计函数int sqrt(int x),计算 xx 的平方根. 输入格式 输入一个 整数 xx,输出它的平方根.直到碰到文件结束符(EOF)为止. 输出格式 对于每组输入,输出一行一个整数,表示输入整 ...
- spring boot引入json,jsonobject,需要指定jdk15
spring boot引入json,需要指定jdk15 <dependency> <groupId>net.sf.json-lib</groupId> <ar ...
- Pymysql-总结
背景:工作需要大量链接数据库进行一些操作查询,但是也会有出现异常情况 1.添加字段 1 ALTER TABLE app01_student ADD COLUMN Relation VARCHAR(25 ...
- Spark核心RDD、什么是RDD、RDD的属性、创建RDD、RDD的依赖以及缓存、
1:什么是Spark的RDD??? RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素可并行 ...
- [转] 对Array.prototype.slice.call()方法的理解
在看别人代码时,发现有这么个写法:[].slice.call(arguments, 0),这到底是什么意思呢? 1.基础 1)slice() 方法可从已有的数组中返回选定的元素. start:必需.规 ...
- google 插件
鼠标手势 crxMouse Chrome™ Gestures Google翻译 json格式化 JSONView github展开文件夹 Octotree axure 原型 Axur ...
- alpha冲刺7/10
目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:冲刺7 团队部分 后敬甲(组长) 过去两天完成了哪些任务 界面设计.图标设计 写博客 接下来的计划 准备下周答辩 跟进进 ...
- 【转载】Python and Mysql Andy Dustman
0. http://mysql-python.sourceforge.net/ Python and MySQL: This is a presentation I did a couple year ...
- 用groovy脚本进行每日工作的自动化【groovy】
我们可以用groovy编写日常的批处理脚本,类似windows下的bat或者unix下的shell.其具体的编写方式非常简单,比如我们想要执行一个dir的命令,只要编写一个test.groovy,其中 ...