Lucence是Apache的一个全文检索引擎工具包。可以将采集的数据存储到索引库中,然后在根据查询条件从索引库中取出结果。索引库可以存在内存中或者存在硬盘上。

本文主要是参考了这篇博客进行学习的,原博客地址https://blog.csdn.net/bskfnvjtlyzmv867/article/details/80914156

主要开发流程是:采集数据,将数据转化成索引文档,然后存储在索引库中,索引库可以保存在内存中,或者保存在硬盘上。在查询的时候通过索引库查询结果,返回数据。

下面的例子主要是将Product表中的数据存储到索引库中,并通过索引库进行查询。项目依赖的jar包可以参考原博客,我用的Lucence版本是4.7。

新建实体类Product,其代码如下:

public class Product {
private Long id;
private String title;
private String sellPoint;
}

将Product实体转化成索引库中Document,并存到索引库中。Product数据可以从数据库中查询,然后通过此方法转化成索引库中的Document,此处省略从数据库查询Product的逻辑。

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
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.util.Version; import entity.Product; public class ProductRepository { public void createIndex(Product product) {
Field id = new StringField("id", product.getId().toString(), Field.Store.YES);
Field title = new TextField("title", product.getTitle().toString(), Field.Store.YES);
Field sellPoint = new TextField("sellPoint", product.getSellPoint().toString(), Field.Store.YES); Document document = new Document();
document.add(id);
document.add(title);
document.add(sellPoint);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_47);
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_47, analyzer);
     
Path path = Paths.get("D:/develop/workspace/slem_compass/data");
try {
Directory directory = FSDirectory.open(path.toFile());
IndexWriter indexWriter = new IndexWriter(directory, config);
indexWriter.addDocument(document);
indexWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
} }

其中上面的代码中Path是索引库在硬盘上的位置,我这里是放在D盘上的某个文件夹内。

下面如何从索引库中查询数据呢?我写了一个Servlet,用户提交查询关键字,request获取到后,根据关键字从索引库中查询数据。当然也可以用Main方法或者test测试类。

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
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.apache.lucene.util.Version; @WebServlet("/search")
public class SearchServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public SearchServlet() {
super();
} protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_47);
QueryParser parser = new QueryParser(Version.LUCENE_47, "title", analyzer);
String title = request.getParameter("title");
System.out.println("");
System.out.println("title: " + title);
try {
Query query = parser.parse(title);
Path path = Paths.get("D:/develop/workspace/slem_compass/data");
Directory directory = FSDirectory.open(path.toFile());
IndexReader reader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(reader); TopDocs topDocs = indexSearcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs; for (ScoreDoc scoreDoc : scoreDocs) {
int docID = scoreDoc.doc; Document doc = indexSearcher.doc(docID);
System.out.println(doc.get("id") + " " + doc.get("title") + " " + doc.get("sellPoint"));
}
System.out.println("");
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
response.setContentType("text/html;charset=utf-8");
response.getWriter().append("Served at: ").append(request.getContextPath());
} protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }

查询的时候也是从D盘上的索引库中读取相应的信息,然后根据关键字进行查询。

这样就完成了索引库的存储和查询。索引的查询很复杂,上面的demo只是一个比较简单的例子,说明大致的原理,后面继续补充索引的查询。

Lucene学习笔记:基础的更多相关文章

  1. jQuery学习笔记 - 基础知识扫盲入门篇

    jQuery学习笔记 - 基础知识扫盲入门篇 2013-06-16 18:42 by 全新时代, 11 阅读, 0 评论, 收藏, 编辑 1.为什么要使用jQuery? 提供了强大的功能函数解决浏览器 ...

  2. Lucene学习笔记(更新)

    1.Lucene学习笔记 http://www.cnblogs.com/hanganglin/articles/3453415.html    

  3. Python学习笔记基础篇——总览

    Python初识与简介[开篇] Python学习笔记——基础篇[第一周]——变量与赋值.用户交互.条件判断.循环控制.数据类型.文本操作 Python学习笔记——基础篇[第二周]——解释器.字符串.列 ...

  4. 数论算法 剩余系相关 学习笔记 (基础回顾,(ex)CRT,(ex)lucas,(ex)BSGS,原根与指标入门,高次剩余,Miller_Rabin+Pollard_Rho)

    注:转载本文须标明出处. 原文链接https://www.cnblogs.com/zhouzhendong/p/Number-theory.html 数论算法 剩余系相关 学习笔记 (基础回顾,(ex ...

  5. 《python基础教程(第二版)》学习笔记 基础部分(第1章)

    <python基础教程(第二版)>学习笔记 基础部分(第1章)python常用的IDE:Windows: IDLE(gui), Eclipse+PyDev; Python(command ...

  6. Java学习笔记——基础篇

    Tips1:eclipse中会经常用到System.out.println方法,可以先输入syso,然后eclipse就会自动联想出这个语句了!! 学习笔记: *包.权限控制 1.包(package) ...

  7. Apache Lucene学习笔记

    Hadoop概述 Apache lucene: 全球第一个开源的全文检索引擎工具包 完整的查询引擎和搜索引擎 部分文本分析引擎 开发人员在此基础建立完整的全文检索引擎 以下为转载:http://www ...

  8. iOS学习笔记——基础控件(上)

    本篇简单罗列一下一些常用的UI控件以及它们特有的属性,事件等等.由于是笔记,相比起来不会太详细 UIView 所有UI控件都继承于这个UIView,它所拥有的属性必是所有控件都拥有,这些属性都是控件最 ...

  9. Lucene学习笔记

    师兄推荐我学习Lucene这门技术,用了两天时间,大概整理了一下相关知识点. 一.什么是Lucene Lucene即全文检索.全文检索是计算机程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明 ...

随机推荐

  1. Spring-内置Resouce

    Spring 内置Resouce Resource: org.springframework.core.io.Resource; 内置方法 public interface Resource exte ...

  2. [HTML]音乐自动播放(兼容微信)

    文件下载:音乐自动播放(兼容微信).zip   <!DOCTYPE html> <html> <head> <meta charset="utf-8 ...

  3. Servlet中response、request乱码问题解决

    Java Web(二) Servlet中response.request乱码问题解决   三月不减肥,五月徒伤悲,这就是我现在的状态,哈哈~ 健身.博客坚持. --WH 一.request请求参数出现 ...

  4. 颜色空间之CIE2000色差公式

    CIEDE2000色差公式   为了进一步改善工业色差评价的视觉一致性,CIE专门成立了工业色差评价的色相和明度相关修正技术委员会TC1-47(Hue and Lightness Dependent ...

  5. vue-router传递参数的几种方式

    参考资料:vue.js官网  博客 vue-router传递参数分为两大类 编程式的导航 router.push声明式的导航 <router-link>编程式导航传递参数有两种类型:字符串 ...

  6. python3基础-set

    集合:无序的,不重复的数据组合 作用: 1.去重,把一个列表变成集合,就自动去重了 2.关系测试,测试两组数据之前的交集.差集.并集等关系 set和dict类似,也是一组key的集合,但不存储valu ...

  7. 虚拟机中安装完Lunix系统后,开机黑屏,只显示一个-,解决方法

    1,查看设置->硬盘是不是SCSI,如果是,先关闭虚拟机,移除该硬盘(实际数据不会删除) 2,添加一个新的虚拟硬盘,最后位置选IDE设备 3,确定,重启虚拟机即可

  8. ajax请求aspx.cs后台方法

    前台jquery代码 $(function () { $("#btnfix").click(function () { $.ajax({ type: "post" ...

  9. 深入Dockerfile(一): 语法指南(转)

    最近在学习K8S,发现这两篇文章还不错,转了过来 docker官方文档Dockerfile reference的笔记. 一.机制 1.1 构建 docker构建一个镜像,需要: Dockerfile文 ...

  10. 20175314 实验二 Java面向对象程序设计

    20175314 实验二 Java面向对象程序设计 一.实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 二 ...