索引和搜索流程图:

1、绿色表示索引过程,对要搜索的原始内容进行索引构建一个索引库,索引过程包括:
确定原始内容即要搜索的内容->采集文档->创建文档->分析文档->素引文档
2、红色表示搜索过程,从索弓库中搜索内容,
搜索过程包括:
用户通过搜索界面->创建查询子执行搜索,从索引库搜索->渲染搜索结果

索引和搜索操作的对象为:索引库。

索引库中包含的部分:索引、原始文档。

原始文档:要索引和搜索的内容。原始内容包括互联网上的网页、数据库中的数据、磁盘上的文件等。

创建文档对象

获取原始内容的目的是为了索引,在索引前需要将原始內容创建成文档(Document),
文档中包括一个一个的域(Field),域中存储内容。
这里我们可以将磁盘上的-一个文件当成一个document,Document 中包括-一些Field
(file_mame文件名称、file_path文件路径、file_size 文件大小、file_content文件内容),如下图:

注意:

每个文档可以有多个Field,

不同的文档可以有不同的Field, —— 对于数据库办不到,每一行看作是一个document(文档),每一列看作是一个Filed.数据库的每一行的字段是固定的。

同一个 文档可以有相同的Field (域名和域值都相同)。—— 数据库中也不能有重复的字段

每个文档都有一个唯一的编号,就是文档id。—— 不同数据库的 id,该id不是域(对应于数据库的字段),无法进行操作,由系统维护。

域:是可以被我们操作的。

分析文档

将原始内容创建为包含域(Field) 的文档(document),需要再对域中的内容进行分析,
分析的过程是经过对原始文档提取单词、将字母转为小写、去除标点符号、去除停用词等过
程生成最终的语汇单元, 可以将语汇单元理解为一个一个的单词。
 
比如下面的文档经过分析之后。
原文档内容:
Lucene is a Java full-text search engine.Lucene is not a completer
application,but rather a code library and API that can easily be used
to add search capabilities to applications.

分析后得到的语汇单元。
lucene、java、full、search、engine。。。
每个单词叫做一个Term,不同的域中拆分出来的相同的单词是不同的term。
含两部分一部分是文档的域名,另一部分是单词的内容。。
例如: 文件名中包含apache和文件内容中包含的apache是不同的term.

 
创建索引
 
对所有文档分析得出的语汇单元进行索引,索引的目的是为了搜索,最终要实现值搜索被索引的语汇单元,从而找到文档(document)

注意: 创建索引是对语汇单元索引,通过词语找文档,这种索引的结构叫倒排索引结构
传统方法是根据文件找到该文件的内容,在文件内容中匹配搜索关键字,这种方法是顺
序扫描方法,数据量大、搜索慢。

倒排索引结构是通过内容找文档,如下图:

倒排索引结构也叫反向索引结构,包括索引和文档两个部分,索引即词汇表,它的规模较小,而文档集合较大。

入门代码实现

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.chen</groupId>
<artifactId>lucene</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <name>lucene</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>7.2.1</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-queryparser -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>7.2.1</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.2.1</version>
</dependency> <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

创建索引

Field类

注在该版本中已经抛弃了LongField方法

  @Test
public void createIndex() throws Exception{
// 第一步; 创建一个java工程,并导入jar包。
// 第二步: 创建一个indexwriter对象。
// 1) 指定索引库的存放位置Directory对象 Directory directory = FSDirectory.open(Paths.get("F:\\lucene\\indexDatabase")); // 2) 指定一个分听器,对文档内容进行分析。
IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer());
IndexWriter indexWriter = new IndexWriter(directory,config);
// 第四步: 创建field对象,将field添加到document对象中。
File filedir = new File("F:\\lucene\\document");
if(filedir.exists() && filedir.isDirectory()){
File[] files = filedir.listFiles();
for (File file:files) {
// 第三步,创|建document对象。
Document document = new Document();
//获取文件的名称
String fileName = file.getName();
//创建textfield,保存文件名(key,value,是否存储)
TextField fileNameField = new TextField("fileName",fileName, Field.Store.YES);
//文件大小
long fileSize = FileUtils.sizeOf(file);
// new NumericDocValuesField("fileSize",fileSize);
SortedNumericDocValuesField fileSizeField = new SortedNumericDocValuesField("fileSize", fileSize);
//文件路径
String filePath = file.getPath();
StoredField filePathField = new StoredField("filePath", filePath);
//文件内容
String fileContent = FileUtils.readFileToString(file,"gbk");
TextField fileContentField = new TextField("fileContent", fileContent, Field.Store.YES);
document.add(fileNameField);
document.add(fileSizeField);
document.add(filePathField);
document.add(fileContentField); // 第五步: 使用indexwriter对象将document对象写入索引库,此过程进行索引创建。并将索引和document对象写) 索引库。
indexWriter.addDocument(document);
}
}
// 第六步: 关闭IndexWriter对象。
indexWriter.close(); }

查询索引

 搜索索引过程:
根据查询语法在倒排索引词典表中分别找出对应搜索词的索引,从而找到索引所链接的文档链表。
比如搜索语法为“fileName:lucee  表示搜索出fileName 域中包含Lucene 的文档。
搜索过程就是在索引上查找域为fileName,并且关键字为Llucene 的term,并根据term 找到文档id 列表。

@Test
public void testSearcher() throws IOException {
// 第一步: 创建一个Directory 对象,也就是索引库存放的位置。
Directory directory = FSDirectory.open(Paths.get("F:\\lucene\\indexDatabase"));
// 第二步: 创建一个indexReader 对象,需要指定Directory 对象。
IndexReader indexReader = DirectoryReader.open(directory);
// 第三步: 创建一个indexsearcher 对象,需要指定InclexReader 对象。
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
// 第四步: 创建一个TermQuery对象,指定查询的域和查询的关键词。
// Term term = new Term("fileName", "java");
Term term = new Term("fileContent", "store");
Query query = new TermQuery(term);
// 第五步: 执行查询。
TopDocs topDocs = indexSearcher.search(query, 13);
// 第六步: 返回查询结果。遍历查询结果并输出。
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc doc : scoreDocs) {
int docIndex = doc.doc;
Document document = indexSearcher.doc(docIndex);
String fileName = document.get("fileName");
System.out.println(fileName);
String fileSize = document.get("fileSize");
System.out.println(fileSize);
String filePath = document.get("filePath");
System.out.println(filePath);
String fileContent = document.get("fileContent");
System.out.println(fileContent);
System.out.println("==========================");
}
// 第七步: 关闭IndexReader 对象。
indexReader.close(); }

lucene_01_入门程序的更多相关文章

  1. mybatis入门_mybatis基本原理以及入门程序

    一.传统jdbc存在的问题 1.创建数据库的连接存在大量的硬编码, 2.执行statement时存在硬编码. 3.频繁的开启和关闭数据库连接,会严重影响数据库的性能,浪费数据库的资源. 4.存在大量的 ...

  2. 1.struts2原理和入门程序

    Struts2是一个MVC的Web应用框架,是在Struts1和WebWork发展起来的,以WebWork为核心,采取拦截器机制来处理用户请求. 原理图: 分析步骤: 1.用户发送一个请求 2.请求的 ...

  3. springMVC2 1入门程序

    1入门程序 .1需求 实现商品列表查询 .2需要的jar包 使用spring3.2.0(带springwebmvc模块) .1前端控制器 在web.xml中配置: <?xml version=& ...

  4. struts2入门程序

    struts2入门程序 1.示例 搭建编程环境就先不说了,这里假设已经搭建好了编程环境,并且下好了strut2的jar包,接下来程序. 1.1 新建web项目 点击File->New->D ...

  5. Spring+SpringMVC+MyBatis深入学习及搭建(十二)——SpringMVC入门程序(一)

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6999743.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十一)——S ...

  6. springmvc(一) springmvc框架原理分析和简单入门程序

    springmvc这个框架真的非常简单,感觉比struts2还更简单,好好沉淀下来学习~ --WH 一.什么是springmvc? 我们知道三层架构的思想,并且如果你知道ssh的话,就会更加透彻的理解 ...

  7. python web入门程序

    python2.x web入门程序 #!/usr/bin/python # -*- coding: UTF-8 -*- # 只在python2.x 有效 import os #Python的标准库中的 ...

  8. Maven01——简介、安装配置、入门程序、项目构建和依赖管理

    1 Maven的简介 1.1 什么是maven 是apache下的一个开源项目,是纯java开发,并且只是用来管理java项目的 Svn eclipse   maven量级 1.2 Maven好处 同 ...

  9. ssm整合快速入门程序(一)

    整合基础说明 spring 是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用.Spring是于2003 年兴起的一个轻量级的Jav ...

随机推荐

  1. Struts2 自己定义下拉框标签Tag

    自己定义标签主要包含三个步骤: 1.编写java类,继承TagSupport类. 2.创建tld文件,影射标签名和标签的java类. 3.jsp页面引入tld. 样例:自己定义下拉框标签 假设页面上有 ...

  2. linux select函数:Linux下select函数的使用详解【转】

    本文转载自;http://www.bkjia.com/article/28216.html Linux下select函数的使用 Linux下select函数的使用 一.Select 函数详细介绍 Se ...

  3. svn for vs

    现在官方下载需要注册一堆的东西,为方便群众使用在这里提供一个新版的下载. http://files.cnblogs.com/wfcfan/AnkhSvn-2.5.12266.rar

  4. E20170915-hm

    client n. 顾客; 当事人; 诉讼委托人; [计算机] 客户端; seal  n. 密封; 印章; 海豹; 封条;  v. 密封; 盖章; 决定; 封上(信封); sheet  n. 纸; 被 ...

  5. Cordova 开发环境搭建及创建第一个app

    整理记录使用cordova创建app应用程序并将其部署至Android系统移动设备上操作过程,具体如下: 一.前期安装环境 1. 安装JDK(java开发工具包) 2. 安装gradle 3. 安装A ...

  6. 进击的Python【第二十二章】

    day22 知识点概要 - Session - CSRF - Model操作 - Form验证(ModelForm) - 中间件 - 缓存 - 信号 内容详细: 1. Session 基于Cookie ...

  7. 3.0 Windows和Linux双系统安装(3)

    3.0 Windows和Linux双系统安装(3) 3.1 精简的安装步骤如下:(如果已经有了前面两篇教程的安装经验,推荐看完3.1即可动手了) 双系统很多开发新人会用到,而且比起虚拟机好处是运行效率 ...

  8. Visual Studio UI Automation 学习(一)

    这几天需要研究自动化测试工具,因为团队开发使用visual studio,所以需要研究一下Visual studio自带的框架. 刚开始安装的时候,没有选自定义安装,所以安装完成后没有找到UI Aut ...

  9. NLP:单词嵌入Word Embeddings

    深度学习.自然语言处理和表征方法 原文链接:http://blog.jobbole.com/77709/ 一个感知器网络(perceptron network).感知器 (perceptron)是非常 ...

  10. CNN结构:SPP-Net为CNNs添加空间尺度卷积-神经元层

    前几个CNN检测的框架要求网络的图像输入为固定长宽,而SPP-Net在CNN结构中添加了一个实现图像金字塔功能的卷积层SPP层,用于在网络中实现多尺度卷积,由此对应多尺度输入,以此应对图像的缩放变换和 ...