1.导入pom jar文件

<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>8.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>8.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>8.11.1</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>

2.

LuceneService.java类
import com.alibaba.fastjson.JSONObject;
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.TextField;
import org.apache.lucene.index.*;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory; import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import java.util.Set; public class LuceneService {
public static void main(String[] args) throws IOException, ParseException {
String indexDir = "D://temp//lucence_data//products";
//创建索引
// createIndex(indexDir); //中文搜索不出来
String jsonDoc = "{\"songName\":\"hello world is chinese\",\"songer\":\"liudehua hk\",\"lyrics\":\"lyricslyricslyricslyrics 2022\"}";
// String jsonDoc = "{\"aaa\":\"111\"}";
indexDoc(indexDir,jsonDoc,"chinese"); } //创建索引
private static void createIndex(String indexDir) throws IOException {
//准备目录
Directory dir = FSDirectory.open(Paths.get(indexDir));
//准备分词器
Analyzer analyzer = new StandardAnalyzer();
//配置
IndexWriterConfig iwConfig = new IndexWriterConfig(analyzer);
//创建索引
IndexWriter iw = new IndexWriter(dir,iwConfig);
//关闭索引
iw.close();
} private static void indexDoc(String indexDir,String jsonDoc,String queryStr) throws IOException, ParseException {
//准备目录
Directory dir = FSDirectory.open(Paths.get(indexDir));
//准备分词器
Analyzer analyzer = new StandardAnalyzer();
//配置
IndexWriterConfig iwConfig = new IndexWriterConfig(analyzer);
//创建索引
IndexWriter iw = new IndexWriter(dir,iwConfig);
//json转document对象
Document doc = Json2Doc(jsonDoc);
iw.addDocument(doc); //IndexReader实例
IndexReader ir = DirectoryReader.open(dir);
IndexSearcher searcher = new IndexSearcher(ir); //查询索引
QueryParser parser = new QueryParser("songName",analyzer);
Query query = parser.parse(queryStr); TopDocs hits = searcher.search(query,10);
String result = "结果=";
for (ScoreDoc scoreDoc : hits.scoreDocs){
//拿到文档
Document rdoc = searcher.doc(scoreDoc.doc);
List<IndexableField> fields = rdoc.getFields();
for (IndexableField f : fields){
result += f.name() + ":"+f.stringValue()+",\r\n";
}
System.out.println("result=" + result);
}
System.out.println("总的查询结果result=" + result);
ir.close(); //关闭索引
iw.close();
} /**
*输出结果
总的查询结果result=结果=songName:hello world is chinese,
lyrics:lyricslyricslyricslyrics 2022,
songer:liudehua hk,
*
*/ private static Document Json2Doc(String jsonDoc) {
Document doc = new Document();
JSONObject jsonObject = JSONObject.parseObject(jsonDoc);
Set<String> keys = jsonObject.keySet();
for (String key : keys){
doc.add(new TextField(key, jsonObject.getString(key), Field.Store.YES ));
}
return doc;
} }

3.本地目录生成的文件

Lucene demo演示搜索查询歌手,歌名,歌词的更多相关文章

  1. 使用Lucene.net+盘古分词实现搜索查询

    这里我的的Demo的逻辑是这样的:首先我基本的数据是储存在Sql数据库中,然后我把我的必需的数据推送到MongoDB中,这样再去利用Lucene.net+盘古创建索引:其中为什么要这样把数据推送到Mo ...

  2. Lucene 的四大索引查询 ——bool 域搜索 通配符 范围搜索

    Lucene 的四大索引查询  清单1:使用布尔操作符 Java代码      //Test boolean operator blic void testOperator(String indexD ...

  3. ASP.NET MVC搭建项目后台UI框架—5、Demo演示Controller和View的交互

    目录 ASP.NET MVC搭建项目后台UI框架—1.后台主框架 ASP.NET MVC搭建项目后台UI框架—2.菜单特效 ASP.NET MVC搭建项目后台UI框架—3.面板折叠和展开 ASP.NE ...

  4. Apache Lucene(全文检索引擎)—搜索

    目录 返回目录:http://www.cnblogs.com/hanyinglong/p/5464604.html 本项目Demo已上传GitHub,欢迎大家fork下载学习:https://gith ...

  5. Apache Solr采用Java开发、基于Lucene的全文搜索服务器

    http://docs.spring.io/spring-data/solr/ 首先介绍一下solr: Apache Solr (读音: SOLer) 是一个开源.高性能.采用Java开发.基于Luc ...

  6. Lucene 06 - 使用Lucene的Query API查询数据

    目录 1 Query对象的创建(方式一): 使用子类对象 1.1 常用的Query子类对象 1.2 常用的Query子类对象使用 1.2.1 使用TermQuery 1.2.2 使用NumericRa ...

  7. Lucene的其他搜索(三)

    生成索引: package com.wp.search; import java.nio.file.Paths; import org.apache.lucene.analysis.Analyzer; ...

  8. Graylog日志管理系统---搜索查询方法使用简介

    Elasticsearch 是一个基于 Lucene 构建的开源.分布式.提供 RESTful 接口的全文搜索引擎 一.Search页面的各位置功能介绍: 1.日志搜索的时间范围 为了使用方便,预设有 ...

  9. 基于 Lucene 的桌面文件搜索

    开源2010年,自己在学习 Lucene 时开发的一款桌面文件搜索工具,这么多年过去了,代码一直静静存放在自己的硬盘上,与其让其沉睡,不如分享出来. 这款工具带有明显的模仿 Everything 的痕 ...

  10. 整合hibernate的lucene大数据模糊查询

      大数据模糊查询lucene 对工作单使用 like模糊查询时,实际上 数据库内部索引无法使用 ,需要逐条比较查询内容,效率比较低在数据量很多情况下, 提供模糊查询性能,我们可以使用lucene全文 ...

随机推荐

  1. 阿里云贾扬清:大数据+AI工程化,让数据从「成本」变为「资产」

    简介: 近年来,数字经济发展迅速,企业转型背后频频涌现「数字力量」的身影.云计算.大数据.人工智能的快速融合形成了数字经济的新基建,也为数字经济发展带来了新的机遇. 5 月 20 日,阿里巴巴副总裁. ...

  2. [Go] go build 减小二进制文件大小的几种方式

    第一种 是去除不需要的调试信息: go build -ldflags "-s -w" main.go 实测 19M 减小为 15M,幅度 2% 第二种 压缩 UPX: the Ul ...

  3. MyBatis源码之MyBatis中SQL语句执行过程

    MyBatis源码之MyBatis中SQL语句执行过程 SQL执行入口 我们在使用MyBatis编程时有两种方式: 方式一代码如下: SqlSession sqlSession = sqlSessio ...

  4. Soluton Set - ZJOI历年真题

    upd:不考浙江省选了.这个题解贴应该不会再更新了. upd:进省队了.再做点,再写点. ZJOI2022 Day1T1 Link&Submission. tag:组合计数,容斥 假设固定了第 ...

  5. phpstudy8.1安装与配置

    环境: window10 phpstudy8.1.1.3 Vmware安装centos7.6 使用场景 window10安装mysql和redis 在Vmware安装centos7.6 桥接模式上网 ...

  6. kibana-6.2.4-amd64的安装

    ubuntu系统 kibana: https://mirrors.huaweicloud.com/kibana/?C=N&O=D 找到6.2.4的下载连接 方法一: 下载tar包,解压即可: ...

  7. docker 安装 kafka+zookeeper,golang操作kafka

    目录 docker-compose.yml go操作kafka producer 消费者 consumer 消费者 结合gin框架操作kafka go-queue操作kafka 环境: centos8 ...

  8. C#语言:散修笔记

    文章目录 前言 数组的几种定义方法 out 和 ref 的区别 可变参数params 静态方法与非静态方法 >❀什么时候使用静态和非静态 构造函数 >❀类中方法的重载 >❀在类中输出 ...

  9. Gin 框架是怎么使用 net http 包的(gin.go)

    Gin 框架是基于 Go 语言的标准库 net/http 构建的,它使用 net/http 提供的基础功能来构建自己的高性能 Web 应用框架. 具体来说,Gin 使用 net/http 的以下方面: ...

  10. 基于 ESP8266_RTOS_SDK 驱动 DHT11

    概述 DHT11模块使用一根data线实现信号触发以及数据反馈,信号格式参考如下 https://zhuanlan.zhihu.com/p/347904660 本文使用GPIO中断的方式采集反馈数据 ...