大型运输行业实战_day15_1_全文检索之Lucene
1.引入
全文检索简介: 非结构化数据又一种叫法叫全文数据。从全文数据(文本)中进行检索就叫全文检索。


2.数据库搜索的弊端
案例 :
select * from product where product like ‘苹果’g
1、 使用like,会导致索引失效
(没有索引时)速度相对慢
2、 搜索效果不好
3、 没有相关度排序
3.全文解锁实现原理

4.简单使用
4.1.创建索引与搜索索引
首先导入jar包

代码:
package com.day02.lucene; 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.FieldType;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
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 org.apache.lucene.util.Version;
import org.junit.Test; import java.io.File;
import java.io.IOException; /**
* Created by Administrator on 2/10.
*/
public class HelloLucene {
//索引地址目录
private String file = "E:\\lucene\\indexOne";
//索引版本配置
private Version matchVersion = Version.LUCENE_4_10_4;
//案例文档
private String doc1 = "Hello world Hello";
private String doc2 = "Hello java world Hello Hello";
private String doc3 = "Hello lucene world"; /**
* 创建索引代码
*
* @throws IOException
*/
@Test
public void testCreateIndex() throws IOException {
System.out.println("-----测试开始------");
//创建索引目录地址对象
Directory directory = FSDirectory.open(new File(file));
//指定分词规则
Analyzer analyzer = new StandardAnalyzer();
//创建索引配置对象
IndexWriterConfig conf = new IndexWriterConfig(matchVersion, analyzer);
//创建索引对象
IndexWriter indexWriter = new IndexWriter(directory, conf);
//创建文本属性
FieldType fieldType = new FieldType();
fieldType.setStored(true);//存储数据
fieldType.setIndexed(true);//添加索引 //创建要添加的文本对象
Document document1 = new Document();
document1.add(new Field("doc", doc1, fieldType));
//添加索引
indexWriter.addDocument(document1); //创建要添加的文本对象
Document document2 = new Document();
document2.add(new Field("doc", doc2, fieldType));
//添加索引
indexWriter.addDocument(document2); //创建要添加的文本对象
Document document3 = new Document();
document3.add(new Field("doc", doc3, fieldType));
//添加索引
indexWriter.addDocument(document3); //关闭资源
indexWriter.close();
} /**
*获取索引
* 1.创建查询分析器(QueryParser),使用查询分析器得到查询对象
* 2.使用索引搜索器(IndexSearcher).search(查询对象, 获取的多少条数据),使用索引搜索器获得文档结果集(TopDocs)
* 3.遍历文档结果集获取文档id
* 4.使用IndexSearcher通过文档id获取文档对象,并获取文档具体字段值
*/
String key = "lucene"; @Test
public void testSearchIndex() throws IOException, ParseException {
System.out.println("-----测试开始------");
//1.创建索引目录地址对象
Directory directory = FSDirectory.open(new File(file));
//2.创建目录阅读器
IndexReader indexReader = DirectoryReader.open(directory);
//3.创建索引搜索器
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
//需要查询的字段
String query = "doc";
//4.创建分词器
StandardAnalyzer standardAnalyzer = new StandardAnalyzer();
//5.创建查询分析器
QueryParser queryParser = new QueryParser(query, standardAnalyzer);
//6.使用查询分析器(查询关键字)获取对应的对象
Query parse = queryParser.parse(key);
//7.获取查询结果
int n = 1000;//最大返回对象数
TopDocs topDocs = indexSearcher.search(parse, n);
//8.获取总天数
int totalHits = topDocs.totalHits;
System.out.println("totalHits=>" + totalHits);
//9.获取查询返回结果集
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
//10.遍历结果集
for (ScoreDoc scoreDoc : scoreDocs) {
//获取文档主键
int docId = scoreDoc.doc;
System.out.println("docId=" + docId);
//通过文档Id获取文档对象
Document doc = indexSearcher.doc(docId);
//获取文档值
String docValue = doc.get("doc");//根据存放的key
System.out.println("docValue=" + docValue);
}
}
}
创建索引测试结果如下:

执行索引搜索结果如下图:

5.执行流程


大型运输行业实战_day15_1_全文检索之Lucene的更多相关文章
- 大型运输行业实战_day11_2_事务理论与实际生产配置事务管理
1.什么是事务(Transaction:tx) 数据库的某些需要分步完成,看做是一个整体(独立的工作单元),不能分割,要么整体成功,要么整体生效.“一荣俱荣,一损俱损”,最能体现事务的思想.案例:银行 ...
- 大型运输行业实战_day01_1_业务分析
1.业务分析 发展历史: 上车收费-->车站买票(相当于先收钱后上车)-->站务系统--->联网售票 2.项目结构 3.开发流程分析 1.业务分析 图文并茂 ...
- 大型运输行业实战_day14_1_webserivce简单入门
1.简单使用 1.1.服务端 1.编写接口 package com.day02.sation.ws; /** * Created by Administrator on 1/12. */ public ...
- 大型运输行业实战_day13_1_定时任务spring-quartz
1.jar包 拷贝quartz-2.2.3.jar包到项目 2.编写定时任务类TicketQuart.java package com.day02.sation.task; import com.da ...
- 大型运输行业实战_day12_1_权限管理实现
1.业务分析 权限说的是不同的用户对同一个系统有不同访问权限,其设计的本质是:给先给用户分配好URL,然后在访问的时候判断该用户是否有当前访问的URL. 2.实现 2.1数据库设计标准5表权限结构 2 ...
- 大型运输行业实战_day11_1_aop理论与aop实际业务操作
1.aop概述 Spring的AOP:什么叫做AOP:Aspect oritention programming(面向切面编程)什么是切面:看图,业务方法 执行前后.AOP的目的:AOP能够将那些与业 ...
- 大型运输行业实战_day01_2_需求文档
1.文档格式 (见模板文件) 2.Axure简单使用 2.1安装Axure傻瓜式安装 2.2简单使用axure 3.总结 需求文件完成后应该包括三种文件: 1.axure文件 2.axure生成的ht ...
- 大型运输行业实战_day10_1_自定义事务管理类
1.创建事务管理类 TransactionManager.java package com.day02.sation.transaction; import com.day02.sation.uti ...
- 大型运输行业实战_day09_2_站间互售实现
1.添加站间互售入口 对应的html代码 <button onclick="otherStation()">站间互售</button> 对应的js发送函数 ...
随机推荐
- Docker Basic
1.简介 last 1.简介 1.1目的?一次编译.到处运行: 1.2.是什么? 实现[运行环境和配置文件的]软件容器,方便[持续集成]实现[整体发布]的容器虚拟化技术: 概括:整体发布的[容器虚拟化 ...
- My sql 自增 虚拟列。
在MYSQL 是没有类似MSSQL 2008 / oracle 数据库开窗函数 over() ,rank(), DENSE_RANK() ,ROW_NUMBER() 又叫窗口函数 . 当我们需要在查询 ...
- NEU(Fst Network Embedding Enhancement via High Order Proximity Approximation)
NEU(Fst Network Embedding Enhancement via High Order Proximity Approximation) NEU:通过对高阶相似性的近似,加持快速网络 ...
- ubuntu上virtualbox无法找到usb设备【解决】
How to set up USB for Virtualbox? USB in different versions of Virtual Box For use of USB in Virtual ...
- iOS保存gif动态图
- (void)saveImageToPhotos:(NSData*)gifData { /***注意先倒入库 #import <AssetsLibrary/AssetsLibrary.h> ...
- 18.os模块获取url后缀
针对文件下载文件重命名文件后缀的获取,此方式只适用url里有后缀的情况(其它的要根据情况去用类似方式获取字段后缀). import os A="http://www.铜陵市建筑工程信息网.c ...
- 学习笔记:Highcharts
(Highcharts 167K: ECharts 354K: jqChart 240K),如果用于网络,Highchart最小 Highcharts 功能强大.开源.美观.图表丰富.兼容绝大多数浏 ...
- 15.纯 CSS 创作条形图,不用任何图表库
原文代码:https://segmentfault.com/a/1190000014768534#articleHeader1 HTML代码: <html> <head> &l ...
- jdk src 学习 Threadlocal
示例: import java.io.Serializable; public class TestThreadLocal implements Serializable { /** * */ pri ...
- Set和Map数据
es6新的数据结构 1.Set:构造函数 const s = new Set ([1,2,3]); console.log(s)//Set(3){1,2,3};[...s];//[1,2,3]cons ...