Lucene 6.5.0 入门Demo(2)
参考文档:http://lucene.apache.org/core/6_5_0/core/overview-summary.html#overview.description

对于path路径不是很清楚,参考了:http://blog.csdn.net/zhangweiwtmdbf/article/details/7099988

package com.cf.first; 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.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test; import java.io.File;
import java.io.IOException;
import java.nio.file.Path; /**
* 参考文件:http://lucene.apache.org/core/6_5_0/core/overview-summary.html#overview.description
* <p>
* Created by Administrator on 2017/4/26.
*/
public class TestLucence {
// 创建索引
@Test
public void createIndex() throws IOException {
//分词器 (对文本进行分词...)
Analyzer analyzer = new StandardAnalyzer();
File file = new File("temp/");
Path path = file.toPath();
Directory directory = FSDirectory.open(path); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
//构建用于操作索引的类
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
Document document = new Document();
IndexableField filed = new TextField("id", Integer.toString(1), Field.Store.YES);
IndexableField title = new StringField("title", "Lucene_百度百科", Field.Store.YES);
IndexableField content = new TextField("content", "Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的...", Field.Store.YES); document.add(filed);
document.add(title);
document.add(content);
indexWriter.addDocument(document);
indexWriter.close();
} @Test
public void searchIndex() throws IOException, ParseException { Analyzer analyzer = new StandardAnalyzer(); File file = new File("temp/");
Path path = file.toPath();
//索引存放的位置....
Directory directory = FSDirectory.open(path);
IndexReader indexReader = DirectoryReader.open(directory);
//通过indexSearcher 去检索索引目录...
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
//这个是一个搜索条件..,通过定义条件来进行查找
QueryParser parser = new QueryParser("content", analyzer);
Query query = parser.parse("apache");
//搜索先搜索索引目录..
//找到符合query 条件的前面N条记录...
TopDocs topDocs = indexSearcher.search(query, 100);
System.out.println("总记录数是====:" + topDocs.totalHits);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
//返回一个击中
for (ScoreDoc scoreDoc : scoreDocs) {
int docId = scoreDoc.doc;
Document document = indexSearcher.doc(docId);
System.out.println(document.get("id"));
System.out.println(document.get("title"));
System.out.println(document.get("content"));
} }
//删除索引
@Test
public void deleteIndex() throws IOException {
// 创建标准分词器
Analyzer analyzer = new StandardAnalyzer(); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
File file = new File("temp/"); Path path = file.toPath();
Directory directory = FSDirectory.open(path);
// 创建indexWriter
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
// 删除全部
indexWriter.deleteAll();
indexWriter.close();
}
}
--

Lucene 6.5.0 入门Demo(2)的更多相关文章
- Lucene 6.5.0 入门Demo
Lucene 6.5.0 要求jdk 1.8 1.目录结构: 2.数据库环境: private int id; private String name; private float price; pr ...
- vue入门 0 小demo (挂载点、模板、实例)
vue入门 0 小demo (挂载点.模板) 用直接的引用vue.js 首先 讲几个基本的概念 1.挂载点即el:vue 实例化时 元素挂靠的地方. 2.模板 即template:vue 实例化时挂 ...
- Omnet++ 4.0 入门实例教程
http://blog.sina.com.cn/s/blog_8a2bb17d01018npf.html 在网上找到的一个讲解omnet++的实例, 是4.0下面实现的. 我在4.2上试了试,可以用. ...
- spring web flow 2.0入门(转)
Spring Web Flow 2.0 入门 一.Spring Web Flow 入门demo(一)简单页面跳转 附源码(转) 二.Spring Web Flow 入门demo(二)与业务结合 附源码 ...
- 【SSH系列】初识spring+入门demo
学习过了hibernate,也就是冬天,经过一个冬天的冬眠,当春风吹绿大地,万物复苏,我们迎来了spring,在前面的一系列博文中,小编介绍hibernate的相关知识,接下来的博文中,小编将继续介绍 ...
- 基于springboot构建dubbo的入门demo
之前记录了构建dubbo入门demo所需的环境以及基于普通maven项目构建dubbo的入门案例,今天记录在这些的基础上基于springboot来构建dubbo的入门demo:众所周知,springb ...
- lua入门demo(HelloWorld+redis读取)
1. lua入门demo 1.1. 入门之Hello World!! 由于我习惯用docker安装各种软件,这次的lua脚本也是运行在docker容器上 openresty是nginx+lua的各种模 ...
- netty入门demo(一)
目录 前言 正文 代码部分 服务端 客服端 测试结果一: 解决粘包,拆包的问题 总结 前言 最近做一个项目: 大概需求: 多个温度传感器不断向java服务发送温度数据,该传感器采用socket发送数据 ...
- canal入门Demo
关于canal具体的原理,以及应用场景,可以参考开发文档:https://github.com/alibaba/canal 下面给出canal的入门Demo (一)部署canal服务器 可以参考官方文 ...
随机推荐
- 结合浅层高层特征的paper总结
1.ION:在conv3.conv4.conv5和context features上分别进行roi_pooling,在channel那一维进行concat 2.Hypernet:在较浅层max_poo ...
- 线程锁(互斥锁Mutex)
线程锁(互斥锁Mutex) 一个进程下可以启动多个线程,多个线程共享父进程的内存空间,也就意味着每个线程可以访问同一份数据,此时,如果2个线程同时要修改同一份数据,会出现什么状况? # -*- cod ...
- 测试框架 Mocha 实例教程(转载:来自阮一峰的一篇文章)
Mocha(发音"摩卡")诞生于2011年,是现在最流行的JavaScript测试框架之一,在浏览器和Node环境都可以使用. 所谓"测试框架",就是运行测试的 ...
- visibilitychange 标签可见性
var pageVisibility = document.visibilityState;// 监听 visibility change 事件document.addEventListener('v ...
- 转: opencv4.0.0 +opencv_contrib在vs2015中编译
https://blog.csdn.net/baidu_40691432/article/details/84957737
- 编写testplan
编写验证计划是验证工作核心技能.衡量标准是完备性.可是写一个完备的验证计划,才开始不是一件容易的事情,需要不断的练习实践. 1.验证计划主要从设计的futurelist中提取. 复杂的futu ...
- Day07 数据类型(列表,元组,字典,集合)常用操作和内置方法
数据类型 列表list: 用途:记录多个值(同种属性) 定义方式:[]用逗号分隔开多个任意类型的值 list()造出来的是列表,参数是可迭代对像,也就是可以使用for循环的对像 传入字典,出来的列表元 ...
- errno的定义
./include/asm-generic/errno-base.h -->包含errno=~ ./arch/arm/include/asm/errno.h -->包含/include/a ...
- find_element——By 元素定位
• find_element(By.ID,”loginName”)• find_element(By.NAME,”SubjectName”)• find_element(By.CLASS_NAME,” ...
- bootshiro---开源的后台管理框架--基于springboot2+ shiro+jwt的真正rest api资源无状态认证权限管理框架,开发人员无需关注权限问题,后端开发完api,前端页面配置即可
https://gitee.com/tomsun28/bootshiro