Lucene基础入门
1. 数据的分类
结构化数据: 查询方法 数据库
非结构化数据: 查询方法 :
(1)顺序扫描法 : 一行一行的看,从头看到尾
(2)全文检索 : 将一部分信息提取出来,重新组织将其变得有一定结构 然后对其搜索 这部分信息称为索引 例如:字典
这种先建立索引然后再对其扫描的过程就叫全文检索。
虽然创建索引的过程是非常耗时的,但是索引一旦创建可以多次使用,全文检索主要是做查询的所以耗时间创建索引是非常值得的。
2. 应用场景
对于数据量大、数据结构不固定的数据可采用全文检索方式搜索,比如百度、Google等搜索引擎、论坛站内搜索、电商网站站内搜索等。
每个单词叫做一个Term,不同的域中拆分出来的相同的单词是不同的term。term中包含两部分一部分是文档的域名,另一部分是单词的内容。


3. 解释部分
(1)首先需要获取原始内容然后进行索引,在索引前需要将原始内容创建成文档,文档中包含很多域,这些域主要用于存储内容。
先上代码演示:
创建

package com.baizhi.lucence; import java.io.File;
import java.io.IOException; import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version; public class TestCreateIndexWriter { //创建索引写入器 (并且存入一篇文章)
public static void main(String[] args) {
IndexWriter indexWriter = null;
try {
//创建indexConfig对象 保存了两个主要的属性一个是 Lucene的版本 另外一个是分词器以及版本
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_44,new StandardAnalyzer(Version.LUCENE_44)); //创建内存索引目录
Directory direction = FSDirectory.open(new File("D:/lucenezhulina"));
//创建 索引写入器对象 需要两个对象一个 索引目录 封装的需要的配置
indexWriter = new IndexWriter(direction, indexWriterConfig); //创建文档
for(int i=0;i<10;i++) { Document document = new Document();
document.add(new StringField("id", String.valueOf(i), Store.YES));
document.add(new StringField("title", "背影"+i, Store.YES));
document.add(new StringField("author", "朱自清", Store.YES));
document.add(new TextField("content", "你站这里不要动,我去给你买几个橘子", Store.YES));
//添加文章
indexWriter.addDocument(document); }
indexWriter.commit();
indexWriter.close(); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
创建代码
搜索

package com.baizhi.lucence; import java.io.File;
import java.io.IOException; import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory; public class TestSearchIndex {
/*
* 八种基本数据类型 +String 类型不分词
* Text类型分词 标准分词器的规则 单字分词
*
*/
public static void main(String[] args) {
try {
//获取指定索引库
Directory direction = FSDirectory.open(new File("D:/lucenezhulina")); //将索引库放入DirectoryReader中
DirectoryReader reader = DirectoryReader.open(direction); //创建索引搜索对象
IndexSearcher indexSearcher = new IndexSearcher(reader); //搜索条件
TermQuery query = new TermQuery(new Term("content","橘")); //相关度排序
TopDocs topDocs = indexSearcher.search(query, 100);
//获取索引编号
ScoreDoc[] scoreDocs = topDocs.scoreDocs; for(int i=0;i<scoreDocs.length;i++) { System.out.println("索引编号 "+scoreDocs[i].doc);
System.out.println("文章分数 "+scoreDocs[i].score); //获取一个文章
Document document = indexSearcher.doc(scoreDocs[i].doc);
System.out.println("文章编号 "+document.get("id"));
System.out.println("文章标题 "+document.get("title"));
System.out.println("文章作者 "+document.get("author"));
System.out.println("文章内容 "+document.get("content"));
System.out.println("===================================");
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }
搜索代码
更改

1 import static org.junit.Assert.assertNotNull;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import org.apache.lucene.analysis.standard.StandardAnalyzer;
7 import org.apache.lucene.document.Document;
8 import org.apache.lucene.document.StringField;
9 import org.apache.lucene.document.TextField;
10 import org.apache.lucene.document.Field.Store;
11 import org.apache.lucene.index.IndexWriter;
12 import org.apache.lucene.index.IndexWriterConfig;
13 import org.apache.lucene.index.Term;
14 import org.apache.lucene.store.FSDirectory;
15 import org.apache.lucene.util.Version;
16 public class TestUpdateIndex {
17
18 //Lucenne 更新 先删除 再添加
19 public static void main(String[] args) {
20 IndexWriter indexWriter = null;
21 try {
22 FSDirectory directory = FSDirectory.open(new File("D:/lucenezhulina"));
23 IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_44, new StandardAnalyzer(Version.LUCENE_44));
24
25 //创建一个索引写入器对象
26 indexWriter = new IndexWriter(directory,indexWriterConfig);
27 Document document = new Document();
28
29 //document.add(new StringField("id", "9", Store.YES));
30 //document.add(new StringField("title", "背影999", Store.YES));
31
32 document.add(new StringField("author", "哈哈", Store.YES));
33 document.add(new TextField("content", "你站在这里不要动,我去给你买几个橘子橘子橘子橘子橘子橘子橘子橘子", Store.YES));
34 indexWriter.updateDocument(new Term("id", "9"), document);
35 indexWriter.commit();
36 } catch (IOException e) {
37 // TODO Auto-generated catch block
38 e.printStackTrace();
39 try {
40 indexWriter.close();
41 } catch (IOException e1) {
42 // TODO Auto-generated catch block
43 e1.printStackTrace();
44 }
45 }
46
47
48
49 }
50
51 }
更改代码
删除

1 import java.io.File;
2 import java.io.IOException;
3
4 import org.apache.lucene.analysis.standard.StandardAnalyzer;
5 import org.apache.lucene.index.IndexWriter;
6 import org.apache.lucene.index.IndexWriterConfig;
7 import org.apache.lucene.index.Term;
8 import org.apache.lucene.store.FSDirectory;
9 import org.apache.lucene.util.Version;
10
11 public class TestDeleteIndex {
12 public static void main(String[] args) {
13 IndexWriter indexWriter= null;
14 try {
15 //打开一个索引库
16 FSDirectory directory = FSDirectory.open(new File("D:/lucenezhulina"));
17 IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_44,new StandardAnalyzer(Version.LUCENE_44));
18
19 //创建一个索引写入器对象
20 indexWriter = new IndexWriter(directory, indexWriterConfig);
21
22 indexWriter.deleteDocuments(new Term("id","10"));
23 indexWriter.commit();
24
25 } catch (IOException e) {
26 // TODO Auto-generated catch block
27 e.printStackTrace();
28 try {
29 indexWriter.rollback();
30 } catch (IOException e1) {
31 // TODO Auto-generated catch block
32 e1.printStackTrace();
33 }
34 }
35
36
37
38
39
40 }
41 }
删除 这个是代码
Lucene基础入门的更多相关文章
- [全文检索]Lucene基础入门.
本打算直接来学习Solr, 现在先把Lucene的只是捋一遍. 本文内容: 1. 搜索引擎的发展史 2. Lucene入门 3. Lucene的API详解 4. 索引调优 5. Lucene搜索结果排 ...
- Lucene从入门到实战
Lucene 在了解Lucene之前,我们先了解下全文数据查询. 全文数据查询 我们的数据一般分为两种:结构化数据和非结构化数据 结构化数据:有固定格式或有限长度的数据,如数据库中的数据.元数据 非结 ...
- 【转载】Lucene.Net入门教程及示例
本人看到这篇非常不错的Lucene.Net入门基础教程,就转载分享一下给大家来学习,希望大家在工作实践中可以用到. 一.简单的例子 //索引Private void Index(){ Index ...
- Lucene 02 - Lucene的入门程序(Java API的简单使用)
目录 1 准备环境 2 准备数据 3 创建工程 3.1 创建Maven Project(打包方式选jar即可) 3.2 配置pom.xml, 导入依赖 4 编写基础代码 4.1 编写图书POJO 4. ...
- Elasticsearch 基础入门
原文地址:Elasticsearch 基础入门 博客地址:http://www.extlight.com 一.什么是 ElasticSearch ElasticSearch是一个基于 Lucene 的 ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- 「译」JUnit 5 系列:基础入门
原文地址:http://blog.codefx.org/libraries/junit-5-basics/ 原文日期:25, Feb, 2016 译文首发:Linesh 的博客:JUnit 5 系列: ...
- .NET正则表达式基础入门
这是我第一次写的博客,个人觉得十分不容易.以前看别人写的博客文字十分流畅,到自己来写却发现十分困难,还是感谢那些为技术而奉献自己力量的人吧. 本教程编写之前,博主阅读了<正则指引>这本入门 ...
- 从零3D基础入门XNA 4.0(2)——模型和BasicEffect
[题外话] 上一篇文章介绍了3D开发基础与XNA开发程序的整体结构,以及使用Model类的Draw方法将模型绘制到屏幕上.本文接着上一篇文章继续,介绍XNA中模型的结构.BasicEffect的使用以 ...
随机推荐
- 程序员作图工具和技巧,你 get 了么?
分享程序员常用的画图软件和小技巧 大家好,我是鱼皮. 说实话,我觉得做个程序员挺好的.日常工作有很多,写代码.对需求.写方案等等,但我最爱画图:流程图.架构图.交互图.功能模块图.UML 类图.部署图 ...
- 微信小程序中wx.login和wx.getUserProfile的使用
在使用微信登录时,通常会在调用wx.login获取code后再通过wx.getUserProfile获取iv和encryptedData(加密数据)一起发到后端进行登录验证 在实际使用中如果在wx.l ...
- 【网络编程】TCPIP-7-域名与网络地址
目录 前言 7. 域名与网络地址 7.1 IP 7.2 域名 7.3 DNS 7.4 IP地址与域名之间的转换 7.4.1 利用域名获取IP地址 7.4.2 利用IP地址获取域名 7.4.3 升级版的 ...
- flutter学习资料汇总
1.https://github.com/chinabrant/flutter_study 2.https://github.com/zhujian1989/flutter_study 3.https ...
- 数据结构解析-HashTable
概要 HashTable也是散列表的一种实现,我们在上一篇解析了HashMap,在这里我们与HashMap做个对比,让你能清晰的了解两者的区别: 散列表 实现方式 数据安全 数据安全实现方式 key\ ...
- WPF日积月累之文件监测与DataGrid指定Row的颜色
一.概述 关于DataGrid指定Row的颜色,我们可以使用转换器和DataGridRow的Style来实现.对于文件的检测,我们可以使用FileSystemWatcher来实现. 二.Demo Co ...
- 【java虚拟机】垃圾回收机制详解
作者:平凡希 原文地址:https://www.cnblogs.com/xiaoxi/p/6486852.html 一.为什么需要垃圾回收 如果不进行垃圾回收,内存迟早都会被消耗空,因为我们在不断的分 ...
- IO异常--缓冲流--转换流--序列化流( IO流2 )
1.IO异常的处理 JDK7前处理:使用try...catch...finally 代码块,处理异常部分 // 声明变量 FileWriter fw = null; try { //创建流对象 fw ...
- vue-bluJavascript - Vue - 插件(swiper、vue-preview)
swiper swiper是一个支持滑动效果的js插件,它也支持在vue中使用,主要用于移动端的触摸滑动操作.Swiper中文网. 安装和导入插件 npm i vue-awesome-swiper - ...
- mzy git学习, 保留现场,恢复现场,以及bug分支处理(七)
git stash 在git中有时候我们工作做了一半,但是有点急事需要离开一段时间,或者现在需要切换到另一个分支下,去维护和修改一些其它的东西,但是我们现在的工作还没有完成,提交上去的话,并不是完整的 ...