Lucene 6.5.0 要求jdk 1.8

1.目录结构;

2.数据库环境;

private int id;
private String name;
private float price;
private String pic;
private String description

3.

Lucene是Apache的一个全文检索引擎工具包,它不能独立运行,不能单独对外提供服务。

/**
* Created by on 2017/4/25.
*/
public class IndexManager {
@Test
public void createIndex() throws Exception {
// 采集数据
BookDao dao = new BookDaoImpl();
List<Book> list = dao.queryBooks(); // 将采集到的数据封装到Document对象中
List<Document> docList = new ArrayList<Document>();
Document document;
for (Book book : list) {
document = new Document();
// store:如果是yes,则说明存储到文档域中
// 图书ID
// Field id = new TextField("id", book.getId().toString(), Store.YES); Field id = new TextField("id", Integer.toString(book.getId()), Field.Store.YES);
// 图书名称
Field name = new TextField("name", book.getName(), Field.Store.YES);
// 图书价格
Field price = new TextField("price", Float.toString(book.getPrice()), Field.Store.YES);
// 图书图片地址
Field pic = new TextField("pic", book.getPic(), Field.Store.YES);
// 图书描述
Field description = new TextField("description", book.getDescription(), Field.Store.YES); // 将field域设置到Document对象中
document.add(id);
document.add(name);
document.add(price);
document.add(pic);
document.add(description); docList.add(document);
}
//JDK 1.7以后 open只能接收Path///////////////////////////////////////////////////// // 创建分词器,标准分词器
Analyzer analyzer = new StandardAnalyzer(); // 创建IndexWriter
// IndexWriterConfig cfg = new IndexWriterConfig(Version.LUCENE_6_5_0,analyzer);
IndexWriterConfig cfg = new IndexWriterConfig(analyzer); // 指定索引库的地址
// File indexFile = new File("D:\\L\a\Eclipse\\lecencedemo\\");
// Directory directory = FSDirectory.open(indexFile);
Directory directory = FSDirectory.open(FileSystems.getDefault().getPath("D:\\Lpj\\JetBrains\\lucenceIndex1\\")); IndexWriter writer = new IndexWriter(directory, cfg);
      writer.deleteAll(); //清除以前的index
// 通过IndexWriter对象将Document写入到索引库中
for (Document doc : docList) {
writer.addDocument(doc);
} // 关闭writer
writer.close();
} }

  

/**
* Created by on 2017/4/25.
*/
public class IndexSearch {
private void doSearch(Query query) {
// 创建IndexSearcher
// 指定索引库的地址
try {
// File indexFile = new File("D:\\Lpj\\Eclipse\\lecencedemo\\");
// Directory directory = FSDirectory.open(indexFile);
// 1、创建Directory
//JDK 1.7以后 open只能接收Path
Directory directory = FSDirectory.open(FileSystems.getDefault().getPath("D:\\Lpj\\JetBrains\\lucenceIndex1\\"));
IndexReader reader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
// 通过searcher来搜索索引库
// 第二个参数:指定需要显示的顶部记录的N条
TopDocs topDocs = searcher.search(query, 10); // 根据查询条件匹配出的记录总数
int count = topDocs.totalHits;
System.out.println("匹配出的记录总数:" + count);
// 根据查询条件匹配出的记录
ScoreDoc[] scoreDocs = topDocs.scoreDocs; for (ScoreDoc scoreDoc : scoreDocs) {
// 获取文档的ID
int docId = scoreDoc.doc; // 通过ID获取文档
Document doc = searcher.doc(docId);
System.out.println("商品ID:" + doc.get("id"));
System.out.println("商品名称:" + doc.get("name"));
System.out.println("商品价格:" + doc.get("price"));
System.out.println("商品图片地址:" + doc.get("pic"));
System.out.println("==========================");
// System.out.println("商品描述:" + doc.get("description"));
}
// 关闭资源
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
} @Test
public void indexSearch() throws Exception {
// 创建query对象
Analyzer analyzer = new StandardAnalyzer();
// 使用QueryParser搜索时,需要指定分词器,搜索时的分词器要和索引时的分词器一致
// 第一个参数:默认搜索的域的名称
QueryParser parser = new QueryParser("description", analyzer); // 通过queryparser来创建query对象
// 参数:输入的lucene的查询语句(关键字一定要大写)
Query query = parser.parse("description:java AND lucene"); doSearch(query); }

  

Lucene 6.5.0 入门Demo的更多相关文章

  1. Lucene 6.5.0 入门Demo(2)

    参考文档:http://lucene.apache.org/core/6_5_0/core/overview-summary.html#overview.description 对于path路径不是很 ...

  2. vue入门 0 小demo (挂载点、模板、实例)

    vue入门 0 小demo  (挂载点.模板) 用直接的引用vue.js 首先 讲几个基本的概念 1.挂载点即el:vue 实例化时 元素挂靠的地方. 2.模板 即template:vue 实例化时挂 ...

  3. Omnet++ 4.0 入门实例教程

    http://blog.sina.com.cn/s/blog_8a2bb17d01018npf.html 在网上找到的一个讲解omnet++的实例, 是4.0下面实现的. 我在4.2上试了试,可以用. ...

  4. spring web flow 2.0入门(转)

    Spring Web Flow 2.0 入门 一.Spring Web Flow 入门demo(一)简单页面跳转 附源码(转) 二.Spring Web Flow 入门demo(二)与业务结合 附源码 ...

  5. 【SSH系列】初识spring+入门demo

    学习过了hibernate,也就是冬天,经过一个冬天的冬眠,当春风吹绿大地,万物复苏,我们迎来了spring,在前面的一系列博文中,小编介绍hibernate的相关知识,接下来的博文中,小编将继续介绍 ...

  6. 基于springboot构建dubbo的入门demo

    之前记录了构建dubbo入门demo所需的环境以及基于普通maven项目构建dubbo的入门案例,今天记录在这些的基础上基于springboot来构建dubbo的入门demo:众所周知,springb ...

  7. lua入门demo(HelloWorld+redis读取)

    1. lua入门demo 1.1. 入门之Hello World!! 由于我习惯用docker安装各种软件,这次的lua脚本也是运行在docker容器上 openresty是nginx+lua的各种模 ...

  8. netty入门demo(一)

    目录 前言 正文 代码部分 服务端 客服端 测试结果一: 解决粘包,拆包的问题 总结 前言 最近做一个项目: 大概需求: 多个温度传感器不断向java服务发送温度数据,该传感器采用socket发送数据 ...

  9. canal入门Demo

    关于canal具体的原理,以及应用场景,可以参考开发文档:https://github.com/alibaba/canal 下面给出canal的入门Demo (一)部署canal服务器 可以参考官方文 ...

随机推荐

  1. scss引入的问题

    导入.sass或.scss文件 css有一个不太常用的特性,即@import 导入功能,它允许在一个css文件中导入其他css文件.然而,结果是只有执行到@import 规则时,浏览器才会去下载其他c ...

  2. ping 不通。无法访问目标主机

    台式机 使用无线网卡  又登录了VPN 有时候访问不了局域网内的主机 解决方案  添加路由即可 window+R 打开运行 输入cmd  然后输入 route add 10.16.1.89 10.13 ...

  3. A. Pride (emmmm练习特判的好题)

    题目连接 : http://codeforces.com/problemset/problem/891/A You have an array a with length n, you can per ...

  4. 使用gcc -g编译,gdb调试时仍然存在“no debug symbols found”的错误

    今天为调试一段代码,使用gcc将程序用-g选项重新编译.但是使用gdb进行debug时,仍然出现“no debug symbols found”的错误.仔细检查了一下Makefile,原来后面定义的连 ...

  5. shell脚本,一个经典题目。

    [root@localhost wyb]# cat zhuijiu.sh #!/bin/bash #.写一个脚本执行后,输入名字,产生随机数01-99之间的数字. #.如果相同的名字重复输入,抓到的数 ...

  6. UVa-156-反片语

    这题比较精妙的是,我们对于单词重排,实际上是进行了标准化的处理,即按照字典序排序. 这样的话,就很方便地处理了单词的重排问题,我们不需要使用全排列函数进行排列尝试,我们直接化简为一,然后进行比较就可以 ...

  7. 【数学 exgcd】bzoj1407: [Noi2002]Savage

    exgcd解不定方程时候$abs()$不能乱加 Description Input 第1行为一个整数N(1<=N<=15),即野人的数目. 第2行到第N+1每行为三个整数Ci, Pi, L ...

  8. (29)zabbix执行远程命令

    概述 监控,有的人只把他当做报警使用,出现问题之后打开跑回家打开电脑,巴拉巴拉的处理掉,大多数时候都是一些小问题,为何不让zabbix帮你把这些事情处理掉呢?和朋友具体,收到xx硬盘空间慢了.xx服务 ...

  9. 主DNS服务-正向解析

    环境 准备最少两台主机 一台当DNS服务器,一台当客户机 如:192.168.43.7这台主机当DNS服务器,192.168.43.6这台主机当客户机 安装DNS服务 yum install -y b ...

  10. CSS3中制作倒影box-reflect

    目前仅在Chrome.Safari和Opera浏览器下支持 box-reflect:none | <direction> <offset>? <mask-box-imag ...