目录
------------------------------------------------------------------------------------------------------------------------------------------------------
1.什么是Lucene
Lucene是一个开放源代码的全文检索工具包,它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎。
2.Lucene的安装
呃呃呃,没啥可解释的,解压到指定位置即可。
地址:https://pan.baidu.com/s/1bAKxth-1NGECnroM-ulNww 提取码:wrcv
3.Lucene实现全文检索的流程
简单来说就是两个步骤:1.创建索引库。2.查询索引
(1)创建索引库
a.获取文档 :
直接使用io流读取磁盘上的文件。
b.创建文档对象 :
对应每个原始文档创建一个Document对象, 每个document对象中包含多个域(field),域中保存就是原始 文档数据,每个文档都有一个唯一的编号,就是文档id。
c.分析文档 :
就是分词的过程
1、根据空格进行字符串拆分,得到一个单词列表
2、把单词统一转换成小写。
3、去除标点符号
4、去除停用词
停用词:无意义的词
每个关键词都封装成一个Term对象中。
Term中包含两部分内容:
关键词所在的域
关键词本身
不同的域中拆分出来的相同的关键词是不同的Term。
d.创建索引 :
基于关键词列表创建一个索引。保存到索引库中。
索引库中:
索引
document对象
关键词和文档的对应关系
通过词语找文档,这种索引的结构叫倒排索引结构。
(2)查询索引
1)用户查询接口
用户输入查询条件的地方
例如:百度的搜索框
2)把关键词封装成一个查询对象
要查询的域
要搜索的关键词
3)执行查询
根据要查询的关键词到对应的域上进行搜索。
找到关键词,根据关键词找到 对应的文档
4)渲染结果
根据文档的id找到文档对象
对关键词进行高亮显示
分页处理
展示给用户看。
4.入门程序
(1)创建索引库
A.环境搭建
创建一个空的Java project,导入图中三个jar包(在lucene的安装目录中可找到)。
B.代码实现
package com.heima.lunece;
import org.apache.commons.io.FileUtils;
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.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;
import java.io.File;
public class LuneceDirectory {
@Test
public void createIndex() throws Exception{
//1、创建一个Director对象,指定索引库保存的位置。
Directory directory = FSDirectory.open( new File("C:\\Users\\FANG2\\Desktop\\index").toPath() );
//2、基于Directory对象创建一个IndexWriter对象
IndexWriter indexWriter = new IndexWriter( directory,new IndexWriterConfig( ) );
//3、读取磁盘上的文件,对应每个文件创建一个文档对象。
File dir = new File( "C:\\Users\\FANG2\\Desktop\\Lucene\\02.参考资料\\searchsource" );
File[] files = dir.listFiles();
for (File f : files) {
//分别取文件名、文件路径、文件内容、文件大小
String fileName = f.getName();
String filePath = f.getPath();
String fileContext = FileUtils.readFileToString( f,"utf-8" );
long fileSize = FileUtils.sizeOf( f );
//创建Field
Field fieldName =new TextField( "name",fileName,Field.Store.YES );
Field fieldPath = new TextField( "path",filePath,Field.Store.YES );
Field fieldContext = new TextField( "context",fileContext,Field.Store.YES );
Field fieldSize = new TextField( "size",fileSize+"",Field.Store.YES );
//创建文档对象
Document document = new Document();
//4、向文档对象中添加域
document.add( fieldName );
document.add( fieldPath );
document.add( fieldContext );
document.add( fieldSize );
//5、把文档对象写入索引库
indexWriter.addDocument( document );
}
//6、关闭indexwriter对象
indexWriter.close();
}
}
C.使用Luke查看索引库中的内容
安装:安装包在上述分享资源中,同样解压到指定路径即可。
打开:根据安装路径,找到luke.bat批处理文件,双击打开。几秒过后回出现下图,点击Browse选择你的索引库位置,打开。
(2)查询索引
@Test
public void searchIndex() throws Exception {
//1、创建一个Director对象,指定索引库的位置
Directory directory = FSDirectory.open(new File("C:\\Users\\FANG2\\Desktop\\index").toPath());
//2、创建一个IndexReader对象
IndexReader indexReader = DirectoryReader.open(directory);
//3、创建一个IndexSearcher对象,构造方法中的参数indexReader对象。
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
//4、创建一个Query对象,TermQuery
Query query = new TermQuery(new Term("context", "spring"));
//5、执行查询,得到一个TopDocs对象
//参数1:查询对象 参数2:查询结果返回的最大记录数
TopDocs topDocs = indexSearcher.search(query, 10);
//6、取查询结果的总记录数
System.out.println("查询总记录数:" + topDocs.totalHits);
//7、取文档列表
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
//8、打印文档中的内容
for (ScoreDoc doc :
scoreDocs) {
//取文档id
int docId = doc.doc;
//根据id取文档对象
Document document = indexSearcher.doc(docId);
System.out.println(document.get("name"));
System.out.println(document.get("path"));
System.out.println(document.get("size"));
//System.out.println(document.get("content"));
System.out.println("-----------------寂寞的分割线-------------------");
}
//9、关闭IndexReader对象
indexReader.close();
}
=========================================================================================
初始效果:
=========================================================================================
查询总记录数:6
spring_README.txt
C:\Users\FANG2\Desktop\Lucene\02.参考资料\searchsource\spring_README.txt
3257
-----------------寂寞的分割线-------------------
springmvc.txt
C:\Users\FANG2\Desktop\Lucene\02.参考资料\searchsource\springmvc.txt
2124
-----------------寂寞的分割线-------------------
2.Serving Web Content.txt
C:\Users\FANG2\Desktop\Lucene\02.参考资料\searchsource\2.Serving Web Content.txt
35
-----------------寂寞的分割线-------------------
1.create web page.txt
C:\Users\FANG2\Desktop\Lucene\02.参考资料\searchsource\1.create web page.txt
47
-----------------寂寞的分割线-------------------
spring.txt
C:\Users\FANG2\Desktop\Lucene\02.参考资料\searchsource\spring.txt
82
-----------------寂寞的分割线-------------------
cxf_README.txt
C:\Users\FANG2\Desktop\Lucene\02.参考资料\searchsource\cxf_README.txt
3770
-----------------寂寞的分割线-------------------