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的更多相关文章

  1. 大型运输行业实战_day11_2_事务理论与实际生产配置事务管理

    1.什么是事务(Transaction:tx) 数据库的某些需要分步完成,看做是一个整体(独立的工作单元),不能分割,要么整体成功,要么整体生效.“一荣俱荣,一损俱损”,最能体现事务的思想.案例:银行 ...

  2. 大型运输行业实战_day01_1_业务分析

    1.业务分析 发展历史:  上车收费-->车站买票(相当于先收钱后上车)-->站务系统--->联网售票 2.项目结构 3.开发流程分析 1.业务分析            图文并茂  ...

  3. 大型运输行业实战_day14_1_webserivce简单入门

    1.简单使用 1.1.服务端 1.编写接口 package com.day02.sation.ws; /** * Created by Administrator on 1/12. */ public ...

  4. 大型运输行业实战_day13_1_定时任务spring-quartz

    1.jar包 拷贝quartz-2.2.3.jar包到项目 2.编写定时任务类TicketQuart.java package com.day02.sation.task; import com.da ...

  5. 大型运输行业实战_day12_1_权限管理实现

    1.业务分析 权限说的是不同的用户对同一个系统有不同访问权限,其设计的本质是:给先给用户分配好URL,然后在访问的时候判断该用户是否有当前访问的URL. 2.实现 2.1数据库设计标准5表权限结构 2 ...

  6. 大型运输行业实战_day11_1_aop理论与aop实际业务操作

    1.aop概述 Spring的AOP:什么叫做AOP:Aspect oritention programming(面向切面编程)什么是切面:看图,业务方法 执行前后.AOP的目的:AOP能够将那些与业 ...

  7. 大型运输行业实战_day01_2_需求文档

    1.文档格式 (见模板文件) 2.Axure简单使用 2.1安装Axure傻瓜式安装 2.2简单使用axure 3.总结 需求文件完成后应该包括三种文件: 1.axure文件 2.axure生成的ht ...

  8. 大型运输行业实战_day10_1_自定义事务管理类

    1.创建事务管理类  TransactionManager.java package com.day02.sation.transaction; import com.day02.sation.uti ...

  9. 大型运输行业实战_day09_2_站间互售实现

    1.添加站间互售入口 对应的html代码 <button onclick="otherStation()">站间互售</button> 对应的js发送函数 ...

随机推荐

  1. 可恶的 0xc0000005异常

    昨天快下班的时候自己写的代码突然跑出了这个异常,,搞的我特别的无语,因为昨天女朋友要过来了,给我出现了这么一个bug,心里很不爽,果断低下班了,另外一个原因就是公司其他人都跑了,每次基本都是我一个人最 ...

  2. 微信小程序自定义微信客服按钮

    微信小程序官方api中提到的微信客服,是一个固定的组件,图标样式固定,大小最多27px 很明显这个是不能满足我们各种奇葩需求的.下面提供一个野蛮的自定义方法. 比如做一个这样的按钮: 图标用自定义的, ...

  3. EasyuiDatagird绑定分页.NetMVC

    引入EasyUi所有需要的脚本,样式 <link href="/easyui/themes/default/easyui.css" rel="stylesheet& ...

  4. std::ios_base::fmtflags orig std::streamsize prec

  5. remote staging type or host is not specified

    idea在tomcat配置中显示此错误 配置下面选上就好了

  6. RMI non-JRMP server at remote endpoint

    又是一个jmx问题 jmx明明端口占用了, 而且相关java进程也起来了, 为什么会不能通过jconsole访问jmx呢?总是:RMI non-JRMP server at remote endpoi ...

  7. 浅谈测试驱动开发(TDD)

    测试驱动开发(TDD)是极限编程的重要特点,它以不断的测试推动代码的开发,既简化了代码,又保证了软件质量.本文从开发人员使用的角度,介绍了 TDD 优势.原理.过程.原则.测试技术.Tips 等方面. ...

  8. leetcode315

    public class Solution { public List<Integer> countSmaller(int[] nums) { List<Integer> re ...

  9. Web.xml 中 metadata-complete 介绍

    Servlet 3.0 的部署描述文件 web.xml 的顶层标签 <web-app> 有一个 metadata-complete 属性, 该属性指定当前的部署描述文件是否是完全的. 如果 ...

  10. python redis客户端使用lua脚本

    有一个需求,为一个key设置一个field存储时间戳,每当有新数据,判断新数据时间戳是否大于之前的时间戳,如果是,更新时间戳,由于依赖中间执行结果,所以使用lua减少客户端和服务端通信次数. #!/u ...