热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Lucene(全文检索)入门

Lucene实现全文检索的(一部分为索引过程,一部分为搜索过程):创建索引的过程:一、获得原始文档原始文档是

Lucene实现全文检索的(一部分为索引过程,一部分为搜索过程):


创建索引的过程:


一、获得原始文档

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



二、创建文档对象

    获取原始内容的目的是为了索引,在索引前需要将原始内容创建成文档(Document),文档中包括一个一个的域(Field),域中存储内容。

    这里我们可以将磁盘上的一个文件当成一个document,Document中包括一些Field(file_name文件名称、file_path文件路径、file_size文件大小、file_content文件内容)。



三、分析文档

    将原始内容创建为包含域(Field)的文档(document),需要再对域中的内容进行分析,分析的过程是经过对原始文档提取单词、将字母转为小写、去除标点符号、去除停用词等过程生成最终的语汇单元,可以将语汇单元理解为一个一个的单词。



四、创建索引

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



查询索引的过程:


一、用户查询接口

    用于输入查询内容的载体。



二、创建查询

    用户输入查询关键字执行搜索之前需要先构建一个查询对象,查询对象中可以指定查询要搜索的Field文档域、查询关键字等,查询对象会生成具体的查询语法。



三、执行查询

    根据查询语法在倒排索引词典表中分别找出对应搜索词的索引,从而找到索引所链接的文档链表。



四、渲染结果

    以一个友好的界面将查询结果展示给用户,用户根据搜索结果找自己想要的信息,为了帮助用户很快找到自己的结果,提供了很多展示的效果,比如搜索结果中将关键字高亮显示,百度提供的快照等。




Lucene使用


一、导包



二、创建索引库以及查询索引库

创建索引库时,域对象相关的子类介绍:


Field子类介绍
Field类
数据类型

Analyzed

是否分析

Indexed

是否索引

Stored

是否存储
说明
StringField(FieldName, FieldValue,Store.YES))
字符串
N
Y
YN

这个Field用来构建一个字符串Field,但是不会进行分析,会将整个串存储在索引中,比如(订单号,姓名等)

是否存储在文档中用Store.YES或Store.NO决定

LongField(FieldName,FieldValue,Store.YES)
Long
Y
Y
YN

这个Field用来构建一个Long数字型Field,进行分析和索引,比如(价格)

是否存储在文档中用Store.YES或Store.NO决定
StoredField(FieldName,FieldValue) 
重载方法,支持多种类型
N
N
Y

这个Field用来构建不同类型Field

不分析,不索引,但要Field存储在文档中

TextField(FieldName, FieldValue, Store.NO)

TextField(FieldName, reader)


字符串


Y
Y
YN
如果是一个Reader,lucene猜测内容比较多,会采用Unstored的策略



查询索引库时,索引搜索器的搜索方法介绍:


方法说明
indexSearcher.search(query, n)根据Query搜索,返回评分最高的n条记录
indexSearcher.search(query,filter,n)根据Query搜索,添加过滤规则,返回评分最高的n条记录
indexSearcher.search(query,n,sort)根据Query搜索,添加排序规则,返回评分最高的n条记录
indexSearcher.search(booleanQuery,filter,n,sort)根据Query搜索,添加过滤该规则,添加排序规则,返回评分最高的n条记录


代码:

package com.xushuai.lucene;import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
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;/*** Lucene初次使用* Author: xushuai* Date: 2018/5/6* Time: 15:08* Description:*/
public class LuceneDemo {/** 创建索引库的步骤* 第一步:创建一个java工程,并导入jar包。* 第二步:创建一个indexwriter对象。* 1)指定索引库的存放位置Directory对象* 2)指定一个分析器,对文档内容进行分析。* 第三步:创建document对象。* 第四步:创建field对象,将field添加到document对象中。* 第五步:使用indexwriter对象将document对象写入索引库,此过程进行索引创建。并将索引和document对象写入索引库。* 第六步:关闭IndexWriter对象。*//*** 创建索引库* @auther: xushuai* @date: 2018/5/6 15:12* @throws: IOException*/@Testpublic void luceneCreateIndexRepository() throws IOException {//存放索引库的路径Directory directory = FSDirectory.open(new File("D:\\lucene&solr\\lucene\\index"));//创建分析器(使用其子类,标准分析器类)Analyzer analyzer = new StandardAnalyzer();IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LATEST, analyzer);//使用索引库路径和分析器构造索引库写入流IndexWriter indexWriter = new IndexWriter(directory,indexWriterConfig);//读取原始文档创建相应文档对象,并设置相关field域File dir = new File("D:\\lucene&solr\\lucene\\searchsource");//遍历该dir下所有的文件创建文档对象for(File file:dir.listFiles()){//获取其文件名、文件大小、文件位置、文件内容String file_name = file.getName();String file_path = file.getPath();Long file_size = FileUtils.sizeOf(file);String file_content = FileUtils.readFileToString(file);//为获取到的文件属性创建相应域(参数分别为:域名城、域值以及是否保存)Field fileNameField = new TextField("filename",file_name, Field.Store.YES);Field filePathField = new StoredField("filepath",file_path);Field fileSizeField = new LongField("filesize",file_size, Field.Store.YES);Field fileContentField = new TextField("filecontent",file_content,Field.Store.YES);//创建document对象Document document = new Document();//保存域对象document.add(fileContentField);document.add(fileNameField);document.add(filePathField);document.add(fileSizeField);//将document绑定给写入流indexWriter.addDocument(document);}//释放资源indexWriter.close();}/** 查询索引* 第一步:创建一个Directory对象,也就是索引库存放的位置。* 第二步:创建一个indexReader对象,需要指定Directory对象。* 第三步:创建一个indexsearcher对象,需要指定IndexReader对象* 第四步:创建一个TermQuery对象,指定查询的域和查询的关键词。* 第五步:执行查询。* 第六步:返回查询结果。遍历查询结果并输出。* 第七步:关闭IndexReader对象*//*** 查询索引* @auther: xushuai* @date: 2018/5/6 15:48* @throws: IOException*/@Testpublic void luceneSearchIndexRepository() throws IOException {//指定索引库位置Directory directory = FSDirectory.open(new File("D:\\lucene&solr\\lucene\\index"));//创建索引库读取流IndexReader indexReader = DirectoryReader.open(directory);//创建索引搜索器对象IndexSearcher indexSearcher = new IndexSearcher(indexReader);//创建查询条件对象,第一个参数为:域名称 第二个参数为:域值 这里查询条件为:文件名中含有apache的文档Query query = new TermQuery(new Term("filename","apache"));//执行查询,第一个参数为:查询条件 第二个参数为:结果返回最大个数TopDocs topDocs = indexSearcher.search(query, 10);//打印结果集长度System.out.println("查询结果总条数:" + topDocs.totalHits);//遍历结果集for (ScoreDoc doc:topDocs.scoreDocs) {//获取其查询到的文档对象,ScoreDoc对象的doc属性可以获取document的id值Document document = indexSearcher.doc(doc.doc);//打印文件名System.out.println("文件名: " + document.get("filename"));//打印文件大小System.out.println("文件大小:" + document.get("filesize"));//打印文件路径System.out.println("文件路径:" + document.get("filepath"));//打印文件内容System.out.println(document.get("filecontent"));//分割线System.out.print("------------------------------------------------------------------------------");}//释放资源indexReader.close();}
}



创建索引库的结果(可使用工具查看索引库):


索引数据库的结果:

查询结果总条数:2
文件名: apache lucene.txt
文件大小:724
文件路径:D:\lucene&solr\lucene\searchsource\apache lucene.txt
# Apache Lucene README file## IntroductionLucene is a Java full-text search engine. Lucene is not a complete
application, but rather a code library and API that can easily be used
to add search capabilities to applications.* The Lucene web site is at: http://lucene.apache.org/* Please join the Lucene-User mailing list by sending a message to:java-user-subscribe@lucene.apache.org## Files in a binary distributionFiles are organized by module, for example in core/:* `core/lucene-core-XX.jar`:The compiled core Lucene library.To review the documentation, read the main documentation page, located at:
`docs/index.html`To build Lucene or its documentation for a source distribution, see BUILD.txt------------------------------------------------------------------------------
文件名: Welcome to the Apache Solr project.txt
文件大小:5464
文件路径:D:\lucene&solr\lucene\searchsource\Welcome to the Apache Solr project.txt
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.Welcome to the Apache Solr project!
-----------------------------------Solr is the popular, blazing fast open source enterprise search platform
from the Apache Lucene project.For a complete description of the Solr project, team composition, source
code repositories, and other details, please see the Solr web site at
http://lucene.apache.org/solrGetting Started
---------------See the "example" directory for an example Solr setup. A tutorial
using the example setup can be found athttp://lucene.apache.org/solr/tutorial.html
or linked from "docs/index.html" in a binary distribution.
Also, there are Solr clients for many programming languages, see http://wiki.apache.org/solr/IntegratingSolrFiles included in an Apache Solr binary distribution
----------------------------------------------------example/A self-contained example Solr instance, complete with a sampleconfiguration, documents to index, and the Jetty Servlet container.Please see example/README.txt for information about running thisexample.dist/solr-XX.warThe Apache Solr Application. Deploy this WAR file to any servletcontainer to run Apache Solr.dist/solr--XX.jarThe Apache Solr libraries. To compile Apache Solr Plugins,one or more of these will be required. The core library isrequired at a minimum. (see http://wiki.apache.org/solr/SolrPluginsfor more information).docs/index.htmlThe Apache Solr Javadoc API documentation and TutorialInstructions for Building Apache Solr from Source
-------------------------------------------------1. Download the Java SE 7 JDK (Java Development Kit) or later from http://java.sun.com/You will need the JDK installed, and the $JAVA_HOME/bin (Windows: %JAVA_HOME%\bin) folder included on your command path. To test this, issue a "java -version" command from your shell (command prompt) and verify that the Java version is 1.7 or later.2. Download the Apache Ant binary distribution (1.8.2+) from http://ant.apache.org/ You will need Ant installed and the $ANT_HOME/bin (Windows: %ANT_HOME%\bin) folder included on your command path. To test this, issue a "ant -version" command from your shell (command prompt) and verify that Ant is available. You will also need to install Apache Ivy binary distribution (2.2.0) from http://ant.apache.org/ivy/ and place ivy-2.2.0.jar file in ~/.ant/lib -- if you skip this step, the Solr build system will offer to do it for you.3. Download the Apache Solr distribution, linked from the above web site. Unzip the distribution to a folder of your choice, e.g. C:\solr or ~/solrAlternately, you can obtain a copy of the latest Apache Solr source codedirectly from the Subversion repository:http://lucene.apache.org/solr/versioncontrol.html4. Navigate to the "solr" folder and issue an "ant" command to see the available optionsfor building, testing, and packaging Solr.NOTE: To see Solr in action, you may want to use the "ant example" command to buildand package Solr into the example/webapps directory. See also example/README.txt.Export control
-------------------------------------------------
This distribution includes cryptographic software. The country in
which you currently reside may have restrictions on the import,
possession, use, and/or re-export to another country, of
encryption software. BEFORE using any encryption software, please
check your country's laws, regulations and policies concerning the
import, possession, or use, and re-export of encryption software, to
see if this is permitted. See for more
information.The U.S. Government Department of Commerce, Bureau of Industry and
Security (BIS), has classified this software as Export Commodity
Control Number (ECCN) 5D002.C.1, which includes information security
software using or performing cryptographic functions with asymmetric
algorithms. The form and manner of this Apache Software Foundation
distribution makes it eligible for export under the License Exception
ENC Technology Software Unrestricted (TSU) exception (see the BIS
Export Administration Regulations, Section 740.13) for both object
code and source code.The following provides more details on the included cryptographic
software:Apache Solr uses the Apache Tika which uses the Bouncy Castle generic encryption libraries forextracting text content and metadata from encrypted PDF files.See http://www.bouncycastle.org/ for more details on Bouncy Castle.------------------------------------------------------------------------------





推荐阅读
  • r2dbc配置多数据源
    R2dbc配置多数据源问题根据官网配置r2dbc连接mysql多数据源所遇到的问题pom配置可以参考官网,不过我这样配置会报错我并没有这样配置将以下内容添加到pom.xml文件d ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 第四章高阶函数(参数传递、高阶函数、lambda表达式)(python进阶)的讲解和应用
    本文主要讲解了第四章高阶函数(参数传递、高阶函数、lambda表达式)的相关知识,包括函数参数传递机制和赋值机制、引用传递的概念和应用、默认参数的定义和使用等内容。同时介绍了高阶函数和lambda表达式的概念,并给出了一些实例代码进行演示。对于想要进一步提升python编程能力的读者来说,本文将是一个不错的学习资料。 ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
  • Python爬虫中使用正则表达式的方法和注意事项
    本文介绍了在Python爬虫中使用正则表达式的方法和注意事项。首先解释了爬虫的四个主要步骤,并强调了正则表达式在数据处理中的重要性。然后详细介绍了正则表达式的概念和用法,包括检索、替换和过滤文本的功能。同时提到了re模块是Python内置的用于处理正则表达式的模块,并给出了使用正则表达式时需要注意的特殊字符转义和原始字符串的用法。通过本文的学习,读者可以掌握在Python爬虫中使用正则表达式的技巧和方法。 ... [详细]
  • 数组的排序:数组本身有Arrays类中的sort()方法,这里写几种常见的排序方法。(1)冒泡排序法publicstaticvoidmain(String[]args ... [详细]
  • 面向对象之3:封装的总结及实现方法
    本文总结了面向对象中封装的概念和好处,以及在Java中如何实现封装。封装是将过程和数据用一个外壳隐藏起来,只能通过提供的接口进行访问。适当的封装可以提高程序的理解性和维护性,增强程序的安全性。在Java中,封装可以通过将属性私有化并使用权限修饰符来实现,同时可以通过方法来访问属性并加入限制条件。 ... [详细]
  • 实现一个通讯录系统,可添加、删除、修改、查找、显示、清空、排序通讯录信息
    本文介绍了如何实现一个通讯录系统,该系统可以实现添加、删除、修改、查找、显示、清空、排序通讯录信息的功能。通过定义结构体LINK和PEOPLE来存储通讯录信息,使用相关函数来实现各项功能。详细介绍了每个功能的实现方法。 ... [详细]
  • 大数据Hadoop生态(20)MapReduce框架原理OutputFormat的开发笔记
    本文介绍了大数据Hadoop生态(20)MapReduce框架原理OutputFormat的开发笔记,包括outputFormat接口实现类、自定义outputFormat步骤和案例。案例中将包含nty的日志输出到nty.log文件,其他日志输出到other.log文件。同时提供了一些相关网址供参考。 ... [详细]
author-avatar
我负天下人0
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有