Lucene

全文检索:
  检索数据,数据的分类:
  在计算机当中,比如说存在磁盘的文本文档,HTML页面,Word文档等等......
结构化数据
  格式固定,长度固定,数据类型固定等等,我们称之为结构化数据,比如说数据库中的数据

非结构化数据
  word文档,HTML文件,pdf文档,文本文档等等,格式不固定,长度不固定,数据类型不固定,成为非结构化数据

数据的查询
  结构化数据查询
    SQL语句 select * from user where userid=1
  非结构化数据的查询
    非结构化数据查询有一些难度,比如我们在一个文本文件当中找到spring关键字
      目测 一个一个查找文件....
      使用程序将文件读取到内存当中,然后匹配字符串spring,这种方式被称为顺序扫描
      将我们非结构化数据转换为结构化数据
全文检索
  创建索引,然后查询索引的过程我们称之为全文检索

  索引一次创建可以多次使用,这样就不用了每一次都进行文件数据查分,比较快
全文检索框架Lucene
  Lucene:

    apache Lucene基于Java开发开源全文检索项目工具包,http://lucene.apache.org/,除Lucene外,Solr,ES等都是全文检索框架,以及Lucene
  Lucene全文检索流程:
    创建索引
    获取文档
    获得原始文档,要基于那些数据进行搜索,这些数据就是原始文档
如何获取原始文档:
  搜索引擎:根据爬虫获得原始文档
  站内搜索:基本上都是从数据库当中获取的,JDBC获取
  磁盘文件:利用IO流读取
构建文档对象
  每一个原始文档都是一个文档对象

    比如磁盘文件,每一个文件就是一个文档对象,京东搜索,每一个商品就是一个文档对象
  每一个文档对象当中包含多个域

    域中存储文档信息,存储文档名称,文档路径,文档大小,文档内容,域中有两大部分:
    域的名称name,域的值spring.txt key=value,每一个文档都有一个文档编号ID
创建索引
  基于关键词列表创建一个索引,保存到索引库当中,一次创建多次使用
索引库:
  索引
  document对象
  关键词和文档的对应关系,采用倒排索引结构
查询索引
  用户查询接口:用户输入查询条件的地方,比如百度搜索框,jd商品搜索框
  获取到关键词,获取到关键词后封装成一个查询对象要查询的域,和要搜索的关键词
  执行查询,根据查询的关键词和对应的域去查询
  根据关键词和文档的对应关系,利用倒排索引结构,找到文档id,找到id后就能定位到文档
  渲染结果
Lucene入门程序环境搭建

<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>7.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-analyzers-common -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>7.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>


创建索引

public static void main(String[] args) throws Exception {
//创建Directory对象,用于指定索引库的位置 RAMDirectory内存
Directory directory = FSDirectory.open(new File("D:\Y3\0224\luceneindex").toPath());
//创建一个IndexWriter对象,用于写索引
IndexWriter indexWriter=new IndexWriter(directory,new IndexWriterConfig());
//读取磁盘中文件,对应每一个文件创建一个文档对象
File file=new File("D:\Y3\0224\index");
//获取文件列表
File[] files = file.listFiles();
for (File item:files) {
//获取文件数据,封装域 参数三:是否存储
Field fieldName=new TextField("fieldName",item.getName(), Field.Store.YES);
Field fieldPath=new TextField("fieldPath",item.getPath(), Field.Store.YES);
Field fieldSize=new TextField("fieldSize", FileUtils.sizeOf(item)+"", Field.Store.YES);
Field fieldContent=new TextField("fieldContent", FileUtils.readFileToString(item,"UTF-8"), Field.Store.YES);
//创建文档对象,向文档对象中添加域
Document document=new Document();
document.add(fieldName);
document.add(fieldPath);
document.add(fieldSize);
document.add(fieldContent);

//创建索引,将文档对象写入到索引库
indexWriter.addDocument(document);
}
//关闭资源
indexWriter.close();
}

查看索引

ndexSearcher indexSearcher=new IndexSearcher(indexReader);
//创建Query查询对象
Query query=new TermQuery(new Term("fieldContent","spring"));
//执行查询,获取到文档对象
TopDocs topDocs = indexSearcher.search(query, 10);
System.out.println(topDocs.totalHits+"个文档");
//获取文档列表
ScoreDoc[] scoreDocs=topDocs.scoreDocs;
for (ScoreDoc item:scoreDocs) {
//获取文档ID
int docId=item.doc;
//取出文档
Document doc = indexSearcher.doc(docId);
//获取到文档域中数据
System.out.println("fieldName:"+doc.get("fieldName"));
System.out.println("fieldPath:"+doc.get("fieldPath"));
System.out.println("fieldSize:"+doc.get("fieldSize"));
System.out.println("fieldContent:"+doc.get("fieldContent"));
System.out.println("==============================================================");
}
//关闭资源
indexReader.close();
}
原文地址:https://www.cnblogs.com/whtt/p/12357670.html