lucene简单使用demo
测试结构目录:
1.索引库、分词器
Configuration.java
package com.test.www.web.lucene; import java.io.File; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer; public class Configuration { //索引库的目录位置
private static Directory directory;
//分词器
private static Analyzer analyzer; static{
try {
/**索引库目录为D盘indexDir*/
directory = FSDirectory.open(new File("D:/indexDir/"));
/**词库分词*/
analyzer = new IKAnalyzer();
} catch (Exception e) {
e.printStackTrace();
}
} public static Directory getDirectory() {
return directory;
}
public static Analyzer getAnalyzer() {
return analyzer;
} }
2.文档、实体转换
FileUploadDocument.java
package com.test.www.web.lucene; import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.util.NumericUtils; public class FileUploadDocument { /**将ElecFileUpload对象转换成Document对象*/
public static Document FileUploadToDocument(ElecFileUpload elecFileUpload){
Document document = new Document();
String seqId = NumericUtils.intToPrefixCoded(elecFileUpload.getSeqId());
//主键ID
document.add(new Field("seqId",seqId,Store.YES,Index.NOT_ANALYZED));
//文件名
document.add(new Field("fileName", elecFileUpload.getFileName(), Store.YES, Index.ANALYZED));
//文件描述
document.add(new Field("comment", elecFileUpload.getComment(), Store.YES, Index.ANALYZED));
//所属单位
document.add(new Field("projId",elecFileUpload.getProjId(),Store.YES,Index.NOT_ANALYZED));
//图纸类别
document.add(new Field("belongTo",elecFileUpload.getBelongTo(),Store.YES,Index.NOT_ANALYZED));
return document;
} /**将Document对象转换成ElecFileUpload对象*/
public static ElecFileUpload documentToFileUpload(Document document){
ElecFileUpload elecFileUpload = new ElecFileUpload();
Integer seqId = NumericUtils.prefixCodedToInt(document.get("seqId"));
//主键ID
elecFileUpload.setSeqId(seqId);
//文件名
elecFileUpload.setFileName(document.get("fileName"));
//文件描述
elecFileUpload.setComment(document.get("comment"));
//所属单位
elecFileUpload.setProjId(document.get("projId"));
//图纸类别
elecFileUpload.setBelongTo(document.get("belongTo"));
return elecFileUpload;
}
}
3.lucene工具类
LuceneUtils.java
package com.test.www.web.lucene; import java.util.ArrayList;
import java.util.List; import org.apache.commons.lang.StringUtils;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Formatter;
import org.apache.lucene.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.Scorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.util.NumericUtils;
import org.apache.lucene.util.Version; public class LuceneUtils { /**向索引库中新增数据*/
public void saveFileUpload(ElecFileUpload elecFileUpload) {
Document document = FileUploadDocument.FileUploadToDocument(elecFileUpload);
try {
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_36,Configuration.getAnalyzer());
IndexWriter indexWriter = new IndexWriter(Configuration.getDirectory(),indexWriterConfig);
indexWriter.addDocument(document);
indexWriter.close();
} catch (Exception e) {
throw new RuntimeException();
}
} /**索引库中删除数据*/
public void deleteFileUploadByID(Integer seqId) {
//指定词条的最小单位,相当于id=1
String id = NumericUtils.intToPrefixCoded(seqId);
Term term = new Term("seqId", id);
try {
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_36,Configuration.getAnalyzer());
IndexWriter indexWriter = new IndexWriter(Configuration.getDirectory(),indexWriterConfig);
indexWriter.deleteDocuments(term);
indexWriter.close();
} catch (Exception e) {
e.printStackTrace();
} } /**使用搜索条件,从索引库中搜索出对应的结果*/
public List<ElecFileUpload> searchFileUploadByCondition(String queryString,String projId,String belongTo) {
List<ElecFileUpload> list = new ArrayList<ElecFileUpload>();
try {
IndexSearcher indexSearcher = new IndexSearcher(IndexReader.open(Configuration.getDirectory()));
//指定查询条件在文件名称和文件描述、所属单位、图纸类别的字段上进行搜索
QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_36,new String[]{"fileName","comment"},Configuration.getAnalyzer());
/**使用lucene的多条件查询,即boolean查询,即必须满足3个条件*/
BooleanQuery booleanQuery = new BooleanQuery();
//【按文件名称和描述搜素】搜素的条件
if(StringUtils.isNotBlank(queryString)){
Query query1 = queryParser.parse(queryString);
booleanQuery.add(query1,Occur.MUST);
}
//【所属单位】搜素的条件
if(StringUtils.isNotBlank(projId)){
Query query2 = new TermQuery(new Term("projId", projId));
booleanQuery.add(query2, Occur.MUST);
}
//【图纸类别】搜素的条件
if(StringUtils.isNotBlank(belongTo)){
Query query3 = new TermQuery(new Term("belongTo", belongTo));
booleanQuery.add(query3, Occur.MUST);
}
//返回前100条数据
TopDocs topDocs = indexSearcher.search(booleanQuery, 100);
//返回结果集
ScoreDoc [] scoreDocs = topDocs.scoreDocs;
/**设置高亮效果 begin*/
Formatter formatter = new SimpleHTMLFormatter("<font color='red'>","</font>");
Scorer scorer = new QueryScorer(booleanQuery);
Highlighter highlighter = new Highlighter(formatter,scorer);
//摘要大小(设置大点,最好比文件名大,因为文件名最好不要截取)
int fragmentSize = 50;
Fragmenter fragmenter = new SimpleFragmenter(fragmentSize);
highlighter.setTextFragmenter(fragmenter);
/**设置高亮效果 end*/
if(scoreDocs!=null && scoreDocs.length>0){
for(int i=0;i<scoreDocs.length;i++){
ScoreDoc scoreDoc = scoreDocs[i];
//使用内部惟一编号,获取对应的数据,编号从0开始
Document document = indexSearcher.doc(scoreDoc.doc);
/**获取高亮效果begin*/
/**返回文件名的高亮效果*/
String fileNameText = highlighter.getBestFragment(Configuration.getAnalyzer(), "fileName", document.get("fileName"));
//没有高亮的效果
if(fileNameText==null){
fileNameText = document.get("fileName");
if(fileNameText!=null && fileNameText.length()>fragmentSize){
fileNameText = fileNameText.substring(0, fragmentSize);
}
}
document.getField("fileName").setValue(fileNameText);
/**返回文件描述的高亮效果*/
String commentText = highlighter.getBestFragment(Configuration.getAnalyzer(), "comment", document.get("comment"));
//没有高亮的效果
if(commentText==null){
commentText = document.get("comment");
if(commentText!=null && commentText.length()>fragmentSize){
commentText = commentText.substring(0, fragmentSize);
}
}
document.getField("comment").setValue(commentText);
/**获取高亮效果end*/
//将Document转换成ElecFileUpload
ElecFileUpload elecFileUpload = FileUploadDocument.documentToFileUpload(document);
list.add(elecFileUpload);
}
}
} catch (Exception e) {
throw new RuntimeException();
} return list;
}
}
4.新增索引
TestIndexAdd.java
package com.test.www.web.lucene; public class TestIndexAdd {
/**
* 数据新增
*/
public static void main(String[] args) {
//TODO 数据库新增记录 //同时向索引库中新增记录
ElecFileUpload elecFileUpload = new ElecFileUpload();
LuceneUtils luceneUtils = new LuceneUtils();
elecFileUpload.setBelongTo("111");
elecFileUpload.setSeqId(11);
elecFileUpload.setFileName("春宫图");
elecFileUpload.setComment("这是一本很神奇的书");
elecFileUpload.setProjId("EAS");
//向索引库中新增数据
luceneUtils.saveFileUpload(elecFileUpload);
} }
5.删除索引:
TestIndexDelete.java
package com.test.www.web.lucene; public class TestIndexDelete { /**
* @param args
*/
public static void main(String[] args) {
//TODO 数据库中删除记录 //索引库中删除数据
LuceneUtils luceneUtils = new LuceneUtils();
luceneUtils.deleteFileUploadByID(11);
} }
6.测试类:
Test.java
package com.test.www.web.lucene; import java.util.List; public class Test {
/**
* @param args
*/
public static void main(String[] args) {
LuceneUtils luceneUtils = new LuceneUtils();
String queryString = "神奇春宫图11232322";
String projId = "EAS";
String belongTo = "";
//使用搜索条件,从索引库中搜索出对应的结果
List<ElecFileUpload> elecFileUploadList = luceneUtils.searchFileUploadByCondition(queryString, projId, belongTo);
System.out.println(elecFileUploadList);
}
} /**
* lucene调用全部代码 start
*/
/*ElecFileUpload elecFileUpload = new ElecFileUpload();
LuceneUtils luceneUtils = new LuceneUtils();
elecFileUpload.setBelongTo("111");
elecFileUpload.setSeqId(11);
elecFileUpload.setFileName("春宫图");
elecFileUpload.setComment("这是一本很神奇的书");
elecFileUpload.setProjId("EAS");
//向索引库中新增数据
luceneUtils.saveFileUpload(elecFileUpload); String queryString = "";
String projId = "EAS";
String belongTo = "";
//使用搜索条件,从索引库中搜索出对应的结果
List<ElecFileUpload> elecFileUploadList = luceneUtils.searchFileUploadByCondition(queryString, projId, belongTo);
System.out.println(elecFileUploadList);*/
/**
* lucene调用全部代码 end
*/
效果:
lucene简单使用demo的更多相关文章
- lucene简单搜索demo
方法类 package com.wxf.Test; import com.wxf.pojo.Goods; import org.apache.lucene.analysis.standard.Stan ...
- C#可扩展编程之MEF学习笔记(一):MEF简介及简单的Demo
在文章开始之前,首先简单介绍一下什么是MEF,MEF,全称Managed Extensibility Framework(托管可扩展框架).单从名字我们不难发现:MEF是专门致力于解决扩展性问题的框架 ...
- Maven+Spring+Hibernate+Shiro+Mysql简单的demo框架(二)
然后是项目下的文件:完整的项目请看 上一篇 Maven+Spring+Hibernate+Shiro+Mysql简单的demo框架(一) 项目下的springmvc-servlet.xml配置文件: ...
- MEF简介及简单的Demo
MEF简介及简单的Demo 文章开始之前,首先简单介绍一下什么是MEF,MEF,全称Managed Extensibility Framework(托管可扩展框架).单从名字我们不难发现:MEF是专门 ...
- Spring源码学习:第1步--在Spring源码中添加最简单的Demo代码
为了最大程度地贴近Spring源码并进行学习,一种比较直接的做法是:直接在Spring源码中加入Demo代码,并进行调试. 参照以前使用Spring的经验,Spring最简单的使用方法是:一个实体类. ...
- Dubbo入门—搭建一个最简单的Demo框架
一.Dubbo背景和简介 1.电商系统的演进 Dubbo开始于电商系统,因此在这里先从电商系统的演变讲起. a.单一应用框架(ORM) 当网站流量很小时,只需一个应用,将所有功能如下单支付等都部署在一 ...
- Dubbo入门---搭建一个最简单的Demo框架(转)
Dubbo背景和简介 Dubbo开始于电商系统,因此在这里先从电商系统的演变讲起. 单一应用框架(ORM) 当网站流量很小时,只需一个应用,将所有功能如下单支付等都部署在一起,以减少部署节点和成本. ...
- Dubbo简介---搭建一个最简单的Demo框架
Dubbo背景和简介 Dubbo开始于电商系统,因此在这里先从电商系统的演变讲起. 单一应用框架(ORM) 当网站流量很小时,只需一个应用,将所有功能如下单支付等都部署在一起,以减少部署节点和成本. ...
- [MyBean说明书]-如何进行最简单的DEMO
MyBean是轻量级的.绿色的框架,不需要安装任何的组件和携带任何的其他文件,前 期步骤已经相当精简了,仔细阅读完下面简单的五个步骤,就可以编写基于MyBean的插件: 1.加入Delphi的搜索路径 ...
随机推荐
- 【转】iOS多语言本地化(国际化)设置
原文网址:http://www.jianshu.com/p/2b7743ae9c90 讨论的iOS应用中的多语言设置,Ok 一般是两种情况: 1.根据当前设备语言自动切换显示 2.在应用中可进行语言设 ...
- Redis常用的命令
常规命令查询地址: http://redisdoc.com/ 如下图:
- Python 对象初识
# Demo1class Penson: animal = 'big mom' plant = 'flower' def __init__(self,name,age,sex,job): self.n ...
- Core Java(六)
面向对象语言特性 (一) (一) static 作用于 变量,方法,代码块 静态变量:被类的所有对象共享 在类加载时被加载 优先于对象 Student.country = "中国& ...
- Java对象、Json、Xml转换工具Jackson使用
在Java项目中將一个对象转换成一段Json格式的字符串是非常常见的,能够实现这种需求的工具包也比较多,例如Gson.JSON-lib.Jackson等等.本文主要介绍Jackson的使用,Jacks ...
- 用一个小的例子来说明为什么TCP采用三次握手才能保证连接成功
关于TCP的三次握手,有很多朋友还在疑惑为什么是三次,而不是两次,LZ想了一下用一个例子来说明最好. 场景: 办公室有一名前台(服务器),若干业务员(fork出来的进程) 客户(客户端) 为什么我 ...
- 2015 Objective-C 新特性
Overview 自 WWDC 2015 推出和开源 Swift 2.0 后,大家对 Swift 的热情又一次高涨起来,在羡慕创业公司的朋友们大谈 Swift 新特性的同时,也有很多像我一样工作上依然 ...
- BZOJ3529: [Sdoi2014]数表 莫比乌斯反演_树状数组
Code: #include <cstdio> #include <algorithm> #include <cstring> #define ll long lo ...
- cf掉分记——Avito Code Challenge 2018
再次作死的打了一次cf的修仙比赛感觉有点迷.. 还好掉的分不多(原本就太低没法掉了QAQ) 把会做的前三道水题记录在这.. A: Antipalindrome emmmm...直接暴力枚举 code: ...
- Mysql字段合并
现有数据 合并字段显示:利用GROUP_CONCAT(course,":","score") 严格区分大小写!GROUP_CONCAT 复制代码 SELECT ...