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. 轻松解决oracle11g 空表不能exp导出的问题。

    解决方法: 1插入一条数据(或者再删除),浪费时间,有时几百张表会累死的.2创建数据库之前使用代码: Sql代码 alter system set  deferred_segment_creation ...

  2. System.Data.DbType映射关系

    有如下类型的映射对照: System.Data.SqlClient.SqlDbType  System.Data.OleDb.OleDbType System.Data.Odbc.OdbcType S ...

  3. 关于Access导入Oracle会产生双引号的问题

    把Access2007的数据导入到oracle 10g xe中,成功了,可是在写sql语句时必须加双引号 ,如select “name” from “Product”,貌似是因为access为了防止列 ...

  4. Java课程作业之动手动脑(二)

    纯随机数发生器 编写一个方法,使用以下算法生成指定数目(比如1000个)的随机整数. import java.util.Scanner; public class test { public stat ...

  5. iOS 第三方应用调用safari

    一,直接调用safari打开url [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www. ...

  6. SAP生产机该不该开放Debuger权限(转)

    前段时间公司定制系统在调用SAP RFC接口的时候报错了,看错误消息一时半会儿也不知道是哪里参数数据错误,就想着进到SAP系统里面对这个接口做远程Debuger,跟踪一下参数变量的变化,结果发现根本就 ...

  7. vs2008 "不安全代码只会在使用 /unsafe 编译的情况下出现"的解决方法

    原因是:在编译的代码里面有不安全类型unsafe方法或类! 解决方法:将项目的“可编译不安全代码”属性设置为true就可以了,方法如下:项目属性对话框->配置属性->生成->允许不安 ...

  8. 登录之md5加密

    语句: password = hex_md5(password); 引入js文件: md5.js: /* * A JavaScript implementation of the RSA Data S ...

  9. leetcode1009

    class Solution: def bitwiseComplement(self, N: int) -> int: if N==0: return 1 elif N==1: return 0 ...

  10. 【Source Insight 】之marco学习笔记1

    我们学习编程语言都是从Hello World!,现在我们学习marco也不例外 打开C:\Users\%USERPROFILE%\Documents\Source Insight 4.0\Projec ...