Solr__luence(一)简述_入门
来源:互联网 发布:天书世界兵神八卦数据 编辑:程序博客网 时间:2024/04/27 09:35
简介
Lucene4它是非常优秀的开源的全文检索框架,但是不是一个引擎,与搜索引擎是有差异的,最少需要爬虫以及对数据的储存管理等。它主要用于解决一些sql代码无法完成或者完成需要许多like、or的sql语句问题,也就是全文检索:将要查询的目标文档中的词提取出来,组成索引,通过查询索引达到搜索目标文档的目的。这种先建立索引,再对索引进行搜索的过程就叫全文检索(Full-text Search)
全文检索获取的数据称为非结构化数据:
结构化数据:指具有固定格式或有限长度的数据,如数据库,元数据等。
非结构化数据: 指不定长或无固定格式的数据,如邮件,word文档等。
Lucene4框架的主要应用场景:
1. 购物商城: 物品信息的检索. 2. 论坛系统: 帖子信息的检索. 3. 新闻系统: 新闻内容的检索. 4. 搜索引擎: 信息的检索. (如:百度、谷歌)环境准备-Maven
Lucene 官网下载网址http://lucene.apache.org
本文事例为了同一版本:lucene-4.10.3.zip (学习的版本) 2014.12.10发布的。
luke查看索引库工具下载地址:https://github.com/DmitryKey/luke/releases
项目为打包类型为jar的项目
pom.xml
<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.linge</groupId><artifactId>lucene_test</artifactId><version>0.0.1-SNAPSHOT</version><!-- 配置依赖关系 --><dependencies><!-- junit4 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><!-- lucene-core --><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-core</artifactId><version>4.10.3</version></dependency><!-- lucene-analyzers-common --><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-analyzers-common</artifactId><version>4.10.3</version></dependency><!-- lucene-queryparser --><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-queryparser</artifactId><version>4.10.3</version><exclusions><exclusion><groupId>org.apache.lucene</groupId><artifactId>lucene-sandbox</artifactId></exclusion><exclusion><groupId>org.apache.lucene</groupId><artifactId>lucene-queries</artifactId></exclusion></exclusions></dependency><!-- lucene-analyzers-smartcn --><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-analyzers-smartcn</artifactId><version>4.10.3</version></dependency><!-- lucene-highlighter --><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-highlighter</artifactId><version>4.10.3</version></dependency><!-- IKAnalyzer分词器 --><dependency><groupId>org.wltea</groupId><artifactId>IKAnalyzer</artifactId><version>2012FF</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>1.7</source><target>1.7</target><encoding>utf-8</encoding></configuration></plugin></plugins></build></project>
入门Demo-创建索引
步骤分析:
基本思路:
采集数据——>构建文档对象——>分析文档(分词)——>创建索引
如何采集数据?(3)
1、 对于互联网上网页采用http将网页抓取到本地生成html文件。
2、 如果数据在数据库中就连接数据库读取表中的数据。
3、 如果数据是文件系统中的某个文件,就通过文件系统读取文件的内容。
思路实现流程
四个基本步骤可以分开为七步
1、 配置依赖jar包(lucene-core-4.10.3.jar、lucene-analyzers-common-4.10.3.jar)2、 创建IndexWriter对象(CUD)
3、 创建Document对象
4、 创建Field对象
5、 文档对象循环添加字段
6、 IndexWriter对象不断循环添加文档到索引库
7、 IndexWriter对象提交数据并关闭
/** * 写索引到索引库 * @author LinGe * @email lg625740749@outlook.com * @version 1.0 */public class IndexWriterTest {@Testpublic void test() throws Exception{/** 创建索引库存储目录 */Directory directory = FSDirectory.open(new File("D:\\Lucene4\\lucene_index"));/** 创建分词器(单字分词器) */Analyzer analyzer = new StandardAnalyzer();/** 创建写索引需要的配置信息对象 */IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);/** * 设置打开索引库的模式 * OpenMode.CREATE": 每次都重新创建索引库 * OpenMode.APPEND: 追加模式(不会创建索引库) * OpenMode.CREATE_OR_APPEND: 如果没有创建,第一次创建索引库,后面都是追加模式 * */indexWriterConfig.setOpenMode(OpenMode.CREATE);/** * 创建IndexWriter对象(对索引库做CUD操作) * 第一个参数:索引库存储目录 * 第二个参数:写索引需要的配置信息对象 * */IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);/** 获取文件目录 */File dir = new File("D:\\Lucene4\\file");int cursor = 1;/** 迭代所有的文件,写入索引库 */for (File file : dir.listFiles()){/** 一个文件对应一个文档 */Document doc = new Document();/** 添加字段 */doc.add(new StringField("id", String.valueOf(cursor++), Store.YES));doc.add(new TextField("name", file.getName(), Store.YES));doc.add(new TextField("filePath", file.getPath(), Store.YES));doc.add(new TextField("fileContent", new InputStreamReader(new FileInputStream(file), "gbk")));doc.add(new TextField("content", readFile(file), Store.YES));doc.add(new IntField("int", cursor * 10, Store.YES));doc.add(new FloatField("float", cursor * 50.5f, Store.YES));doc.add(new DoubleField("double", cursor * 100.5d, Store.YES));doc.add(new LongField("long", cursor * 1000l, Store.YES));doc.add(new StoredField("stored", "只存储: " + cursor));/** 定义字段类型 */FieldType ft = new FieldType();ft.setStored(true); // 是否存储ft.setIndexed(true); // 是否创建索引ft.setTokenized(true); // 是否分词Field field = new Field("field", "传智" + cursor, ft);doc.add(field);indexWriter.addDocument(doc);indexWriter.commit();}indexWriter.close();}private String readFile(File file) throws Exception{BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));String line = null;String res = "";while((line = br.readLine()) != null){res += line;}br.close();return res;}}
Luke工具
打开Luke方法:
l 命令运行:cmd运行:java -jar lukeall-4.10.3.jar
l 手动执行:双击lukeall-4.10.3.jar。在“资料\luke”中双击“lukeall-4.10.3.jar”或者“star.bat”启动luke查看。
这样就可以方便查看索引库了
阅读全文
0 0
- Solr__luence(一)简述_入门
- java-spring个人总结(一) 简述概念_入门
- Solr__luence(二)检索索引,分页搜索
- Solr__luence(四)Analyzer分词器
- python入门(一)_前言
- 异常简述(一)
- Solr__luence(五)索引维护(crud),以及Query查询对象
- Solr__luence(六)高亮器、提升权重(相关度)
- Servlet(一)_入门实例
- Windows可执行文件简述(一)
- Windows可执行文件简述(一)
- Windows可执行文件简述(一)
- Windows可执行文件简述(一)
- Windows可执行文件简述(一)
- C#多线程简述(一)
- Java集合简述(一)
- MCU锁相环简述(一)
- Struts2入门(一)_使用Filter实现MVC
- Sip 响应状态码
- android 动画全解
- Kmeans 算法理论和Python实现(一)——简单K-Means实现
- iOS 上传App Store报错------This action could not be completed.Try again.(-22421)
- css3中单位px,em,rem,vh,vw,vmin,vmax的区别及浏览器的支持
- Solr__luence(一)简述_入门
- 视频播放框架vitamio播放视频屏幕花屏的问题
- IDEA启动spring-boot项目时报Error:java: 无效的源发行版: 8
- Mp4文件中提取H264码流
- CEF3研究(一)
- clCreateBuffer中cl_mem_flags参数解释
- oracle redact数据编译后 查询报错 不是selected表达式
- 检测OOM之LeakCanary
- sort()函数与qsort()函数