/**
* Created by mhm on 2019/6/24.
*/
@RunWith(SpringJUnit4ClassRunner.class)
public class LuceneTest { @Test
public void test1() throws IOException {
/**
* 1.准备数据 文档对象
* Document 封装数据 要创建索引的数据必须先放着Document
*
* id 1
* title 背影
* author 朱自清
* content 你站在这里不要动,我去给你买几个橘子
*
* Field 封装数据 实体类中一个属性数据封装在一个Field对象中
* 八种基本类型 StringField 数据不会做分词处理
* TextField 文本Field 数据会分词
*
* 参数1:name 属性名
* 参数2:value 属性值
* 参数3:Field.Store.YES
*/
Document document = new Document();
document.add(new IntField("id",1, Field.Store.YES));
document.add(new TextField("title","背影",Field.Store.YES));
document.add(new StringField("author","朱自清",Field.Store.YES));
document.add(new TextField("content","你站在这里不要动,我去给你买几个橘子",Field.Store.YES)); /**
* 2.扫描数据 创建索引
*
* IndexWriter 索引写出对象
* 作用
* 1.将索引持久化 实际上就是流
* 2.定义分词规则 分词器(别人封装好的分词规则)
*
* 参数1:Directory 指明索引保存的位置 这个文件夹的位置就是索引库
* 参数2:IndexWriterConfig 索引写出配置对象 定义分词规则
* IndexWriterConfig
* 参数1:Lucene的版本
* 参数2:分词器对象
*/ FSDirectory fsDirectory = FSDirectory.open(new File("E://Lucene"));
/**
* StandardAnalyzer 标准分词器
*/
StandardAnalyzer standardAnalyzer = new StandardAnalyzer(Version.LUCENE_44);
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_44,standardAnalyzer);
IndexWriter indexWriter = new IndexWriter(fsDirectory,indexWriterConfig); /**
* 3.addDocument 把原始数据给写出对象
* indexWriter 会把原始数据写出到索引库 在写出的同时indexWriter会自动的扫描原始数据创建索引
*/
indexWriter.addDocument(document);
/**
* 4.将索引提交到索引库
*/
indexWriter.commit();
/**
* 5.释放资源
*/
indexWriter.close();
}
/**
* 查询
*/
@Test
public void test2() throws ParseException, IOException {
/**
* 1. 准备关键词
*/
String keyword = "你站在这里不要动";
/**
* 2. 处理关键词(拆分) Query对象(封装了拆分好的关键词)
* MultiFieldQueryParser 关键词处理对象 可以将关键词处理为Query对象
* 参数1:版本
* 参数2:属性名的数组(被查询的属性)
* 参数3:分词器对象 拆分词语 必须和创建的索引时候的分词器一致
*
* parse 方法处理关键词
*/
String[] fileds = {"title","content"};
MultiFieldQueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_44,fileds,new StandardAnalyzer(Version.LUCENE_44));
Query query = queryParser.parse(keyword);
/**
* 3. 用处理好的关键词 查索引
*
* 把索引库读取到JVM里面(内存)
* IndexSearcher 索引查询对象
* 参数1:索引读取对象
*
* DirectoryReader.open() 读取索引库
* 参数1:FSDirectory File System 文件系统 指的就是磁盘
*
*/
FSDirectory directory = FSDirectory.open(new File("E://Lucene"));
DirectoryReader directoryReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
/**
* 4. 通过查到的索引中的位置信息 获取数据
*
* search()
* 参数1:处理好的关键词
* 参数2:期望结果条数 你想要查到最多多少条信息
*
* 返回值 TopDocs 封装了结果的信息(索引 分数等待)
*
*/
TopDocs topDocs = indexSearcher.search(query, 10);
/**
* ScoreDoc 分数和文档对象的数组
* 封装了分数和Document的docId(Lucene在创建索引的时候自动生成的 就是位置信息)
*
* Lucene在创建索引库的时候,不仅创建了索引,还把原始数据Document对象也放在了索引库中
*/
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
System.out.println(Arrays.toString(scoreDocs));
/**
* 5.通过位置信息docId去索引库中获取原始数据
*/
for (int i = 0; i < scoreDocs.length; i++) {
ScoreDoc scoreDoc = scoreDocs[i];
// 获取docId
int docId = scoreDoc.doc;
// 通过docId获取索引库中对应的Document对象(对象中封装了原始数据)
Document document = indexSearcher.doc(docId);
System.out.println(document);
String id = document.get("id");
String title = document.get("title");
String author = document.get("author");
String content = document.get("content");
System.out.println(id+" "+title+" "+author+" "+content);
}
}
}

LuceneTest的更多相关文章

  1. MVC+MQ+WinServices+Lucene.Net Demo

    前言: 我之前没有接触过Lucene.Net相关的知识,最近在园子里看到很多大神在分享这块的内容,深受启发.秉着“实践出真知”的精神,再结合公司项目的实际情况,有了写一个Demo的想法,算是对自己能力 ...

  2. lucene4.7学习总结

    花了一段时间学习lucene今天有时间把所学的写下来,网上有很多文章但大部分都是2.X和3.X版本的(当前最新版本4.9),希望这篇文章对自己和初学者有所帮助. 学习目录 (1)什么是lucene ( ...

  3. lucene之排序、设置权重、优化、分布式搜索(转)

    lucene之排序.设置权重.优化.分布式搜索(转) 1. 基本应用 using System;using System.Collections.Generic;using System.Text;u ...

  4. lucene创建索引简单示例

    利用空闲时间写了一个使用lucene创建索引简单示例, 1.使用maven创建的项目 2.需要用到的jar如下: 废话不多说,直接贴代码如下: 1.创建索引的类(HelloLucene): packa ...

  5. apache pdfbox

    转 http://www.blogjava.net/sxyx2008/archive/2010/07/23/326890.html 轻松使用apache pdfbox将pdf文件生成图 近期在项目中使 ...

  6. MVC+MQ+WinServices+Lucene.Net

    MVC+MQ+WinServices+Lucene.Net Demo 前言: 我之前没有接触过Lucene.Net相关的知识,最近在园子里看到很多大神在分享这块的内容,深受启发.秉着“实践出真知”的精 ...

  7. Spring AOP中的动态代理

    0  前言 1  动态代理 1.1 JDK动态代理 1.2 CGLIB动态代理 1.2.1 CGLIB的代理用法 1.2.2 CGLIB的过滤功能 2  Spring AOP中的动态代理机制 2.1  ...

  8. 使用Lucene全文检索并使用中文版和高亮显示

    使用Lucene全文检索并使用中文版和高亮显示 中文分词需要引入 中文分词发的jar 包,咱们从maven中获取 <!-- lucene中文分词器 --> <dependency&g ...

  9. Lucene 搜索的初步探究

    搜索应用程序和 Lucene 之间的关系 一般的搜索引擎都会采用这样的 Lucene 采用的是一种称为反向索引(inverted index)的机制.反向索引就是说我们维护了一个词 / 短语表,对于这 ...

随机推荐

  1. 源代码实现一个binary例子

    一.源代码实现一个binary例子 1.例子描述 (1) 数据描述 输入数据X是二进制的一串序列, 在t时刻,有50%的概率是1,50%的概率是0,比如:X=[1,1,0,0,1,0.....]输出数 ...

  2. Eclipse中新建Maven Web项目报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

    在maven web项目中的index.jsp中的错误信息如下: The superclass "javax.servlet.http.HttpServlet" was not f ...

  3. 判断当前用户是否在某个SharePoint组内

    /// <summary> /// 判断当前登录人是否在sharepoint组中 /// </summary> /// <param name="current ...

  4. BestCoder Round #86 1002

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5805 题意:删除数列中一个数,找出相邻之差绝对值最大,求依次删除最大值的和 解法:删除边缘位置的数字需要注 ...

  5. 034 Search for a Range 搜索范围

    给定一个已经升序排序的整形数组,找出给定目标值的开始位置和结束位置.你的算法时间复杂度必须是 O(log n) 级别.如果在数组中找不到目标,返回 [-1, -1].例如:给出 [5, 7, 7, 8 ...

  6. python import error:no module named yaml

    解决办法: python2 : sudo apt-get install python-yaml python3 : sudo apt-get install python3-yaml

  7. FTP连接报530 User 用户名 cannot log in home directory inaccessible的解决方法

    在server 2003新建ftp用户并开启IIS的Ftp功能之后,有时在连接这个ftp的时候会出现530 User 用户名 cannot log in home directory inaccess ...

  8. <Linux系统isosize指令用法>

    isosize命令:iso9660文件系统大小显示 isosize命令用于显示iso9660文件系统的大小,还文件可以使普通文件,也可以是块设备,如/dev/sr0或者/dev/sda.如果没有相关选 ...

  9. Asp.net中的ViewState用法

    Session,ViewState用法基本理论:session值是保存在服务器内存上,那么,可以肯定,大量的使用session将导致服务器负担加重. 而viewstate由于只是将数据存入到页面隐藏控 ...

  10. linux服务器安装nodejs运行环境

    安装nodejs运行环境 第一步:到node官网下载相应版本的安装包,将安装包放置服务器上,路径为 usr/local/node(可根据自身情况进行修改) 第二步:解压 ***.tar.xz格式文件需 ...