整合hibernate的lucene大数据模糊查询
大数据模糊查询lucene
对工作单使用 like模糊查询时,实际上 数据库内部索引无法使用 ,需要逐条比较查询内容,效率比较低在数据量很多情况下, 提供模糊查询性能,我们可以使用lucene全文索引库技术。本文示例是在SSH框架中进行使用。使用hibernate Search (用来整合 Hibernate + Lucene),工作单搜索功能。
1、首先可以在我们的maven工程中引入需要的jar包,
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search</artifactId>
<version>3.4.2.Final</version>
</dependency>
2、导入IKAnalyzer分词器。因为IKAnalyzer在maven中没有,所以我们需要手动下载这个jar包,当然了,在http://mvnrepository.com/网站上面可以找到。
下载好之后可以装载到你自己的maven仓库中或者直接放在你工程的lib目录中,然后来引用:例如我的是在
<dependency>
<groupId>org.wltea</groupId>
<artifactId>IKAnalyzer</artifactId>
<version>2012_u6</version>
<scope>system</scope>
<systemPath>E:\myeclipse_work\BOS\src\main\webapp\WEB-INF\lib\IKAnalyzer2012_u6.jar</systemPath>
</dependency>
3、在resource目录中新建stopword.dic文件,内容为:
a
an
and
are
as
at
be
but
by
for
if
in
into
is
it
no
not
of
on
or
such
that
the
their
then
there
these
they
this
to
was
will
with
4、新建一个IKAnalyzer.cfg.xml文件,内容为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典
<entry key="ext_dict">ext.dic;</entry>
-->
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords">stopword.dic;</entry> </properties>
5、在spring中进行配置:在配置SessionFactory中加入一行:当然了,这个时候需要自己去D盘目录中新建一个文件夹DBIndex
<!-- 配置索引库 -->
<prop key="hibernate.search.default.indexBase">d:/DBIndex</prop>
完整的如下:
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate 属性 ,参考 hibernate.properties 文件 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- 配置索引库 -->
<prop key="hibernate.search.default.indexBase">d:/DBIndex</prop>
</props>
</property>
<!-- 映射hbm -->
<property name="mappingDirectoryLocations" value="classpath:cn/tf/bos/domain"></property>
</bean>
6、在想要实现查询功能的那个domain中添加注解:想要搜索哪个字段就在哪个字段上面加上@Field注解,注意导入的是IKAnalyzer的分词器,不是hibernate-search的分词器。
@Indexed
@Analyzer(impl = IKAnalyzer.class)
public class WorkOrderManage implements java.io.Serializable { // Fields
@DocumentId
private String id;
@Field
private String arrivecity; //到达城市
@Field
private String product;
分词的效果如下:
使用 Luke 工具,查询索引文件内容 ! 在cmd中运行 Java -jar lukeall-3.5.0.jar,即可打开下图这个页面,查看具体的索引信息。
7、在界面中添加搜索框,我这里使用的是easyui,so...
<div data-options="region:'north'">
<!-- 编写搜索框 -->
<!--
prompt 默认提示内容
menu 搜索条件下拉选项
searcher 点击搜索按钮执行js函数名称
-->
<input id="ss" class="easyui-searchbox" style="width:300px"
data-options="prompt:'请输入您的查询内容',menu:'#nm',searcher:doSearch"/> <div id="nm">
<div data-options="name:'arrivecity'">按照到达地搜索</div>
<div data-options="name:'product'">按照货物名称搜索</div>
</div>
</div>
8、写doSeach这个js函数
function doSearch(value,name){
//将查询条件缓存到datagrid
$('#grid').datagrid('load',{
conditionName:name,
conditionValue:value
});
}
9、在action中接收页面传过来的name和value属性的值,然后进行处理:
public String findByPage(){
if(conditionName!=null && conditionName.trim().length()>0 && conditionValue!=null && conditionValue.trim().length()>0){
//有条件查询
PageResponseBean pageResponseBean=workordermanagerService.findByLucene(conditionName,conditionValue,page,rows);
ActionContext.getContext().put("pageResponseBean", pageResponseBean);
}else{
DetachedCriteria detachedCriteria=DetachedCriteria.forClass(WorkOrderManage.class);
PageRequestBean pageRequestBean=initPageRequestBean(detachedCriteria);
PageResponseBean pageResponseBean=workordermanagerService.findByPage(pageRequestBean);
ActionContext.getContext().put("pageResponseBean", pageResponseBean);
}
return "findByPage";
}
private String conditionName;
private String conditionValue;
public void setConditionName(String conditionName) {
this.conditionName = conditionName;
}
public void setConditionValue(String conditionValue) {
this.conditionValue = conditionValue;
}
返回值之后如何处理这里我就不在说了。
10、在service中进行处理,经过service和serviceImpl之后,就会到达dao中,所以我们可以在dao中进行处理。
//luence查询
@Override
public PageResponseBean findByLucene(String conditionName,
String conditionValue, int page, int rows) {
Session session=this.getSession();
FullTextSession fullTextSession=new FullTextSessionImpl(session); Query query=new WildcardQuery(new Term(conditionName,"*"+conditionValue+"*")); //获得全文检索的query
FullTextQuery fullTextQuery=fullTextSession.createFullTextQuery(query);
PageResponseBean pageResponseBean=new PageResponseBean();
pageResponseBean.setTotal(fullTextQuery.getResultSize()); //当前页数据
int firstResult=(page-1)*rows;
int maxResults=rows;
List list=fullTextQuery.setFirstResult(firstResult).setMaxResults(maxResults).list();
pageResponseBean.setRows(list); return pageResponseBean;
}
11、在页面中查看搜索的效果
这样我们整个开发流程就完成了。使用luence对大数据量中的模糊查询是非常实用的功能,当然了,luence只适用于站内搜索,对于模糊查询的支持还是非常好的。
整合hibernate的lucene大数据模糊查询的更多相关文章
- Impala简介PB级大数据实时查询分析引擎
1.Impala简介 • Cloudera公司推出,提供对HDFS.Hbase数据的高性能.低延迟的交互式SQL查询功能. • 基于Hive使用内存计算,兼顾数据仓库.具有实时.批处理.多并发等优点 ...
- (十四)整合 ClickHouse数据库,实现数据高性能查询分析
整合 ClickHouse数据库,实现数据高性能查询分析 1.ClickHouse简介 1.1 数据分析能力 2.SpringBoot整个ClickHouse 2.1 核心依赖 2.2 配属数据源 2 ...
- ajax 判断账户密码 调取数据模糊查询 时钟
一.判断账户密码 <Login.html> <head> <meta http-equiv="Content-Type" content=" ...
- Druid:一个用于大数据实时处理的开源分布式系统——大数据实时查询和分析的高容错、高性能开源分布式系统
转自:http://www.36dsj.com/archives/28590 Druid 是一个用于大数据实时查询和分析的高容错.高性能开源分布式系统,旨在快速处理大规模的数据,并能够实现快速查询和分 ...
- 给Clouderamanager集群里安装基于Hive的大数据实时分析查询引擎工具Impala步骤(图文详解)
这个很简单,在集群机器里,选择就是了,本来自带就有Impala的. 扩展博客 给Ambari集群里安装基于Hive的大数据实时分析查询引擎工具Impala步骤(图文详解)
- 给Ambari集群里安装基于Hive的大数据实时分析查询引擎工具Impala步骤(图文详解)
不多说,直接上干货! Impala和Hive的关系(详解) 扩展博客 给Clouderamanager集群里安装基于Hive的大数据实时分析查询引擎工具Impala步骤(图文详解) 参考 horton ...
- 《深度访谈:华为开源数据格式 CarbonData 项目,实现大数据即席查询秒级响应》
深度访谈:华为开源数据格式 CarbonData 项目,实现大数据即席查询秒级响应 Tina 阅读数:146012016 年 7 月 13 日 19:00 华为宣布开源了 CarbonData ...
- 大数据量查询容易OOM?试试MySQL流式查询
一.前言 程序访问 MySQL 数据库时,当查询出来的数据量特别大时,数据库驱动把加载到的数据全部加载到内存里,就有可能会导致内存溢出(OOM). 其实在 MySQL 数据库中提供了流式查询,允许把符 ...
- offset新探索:双管齐下,加速大数据量查询
摘要:随着offset的增加,查询的时长也会越来越长.当offset达到百万级别的时候查询时长通常是业务所不能容忍的. 本文分享自华为云社区<offset新探索:双管齐下,加速大数据量查询> ...
随机推荐
- OpenCV学习(4) Mat的基本操作(1)
图像在OpenCV中都是通过Mat类来存储的,Mat可以用来表示N维矩阵,当然用的最多的还是二维矩阵. Mat类有两部分组成:第一部分是头信息,这些信息主要用来描述矩阵,比如矩 ...
- C语言指针加1问题以及字节对齐问题
今天早上自己写了一段代码,然后测试的时候发现结果总是和预期的不一样,而且偏差的有点离谱,冥思苦想了将近五个小时,最后在我要开始怀疑人生的时候,发现原来是自己犯了一个极其低级但又容易被忽略的问题.好吧, ...
- meterpreter源码
/* by codeliker @2014.12.08 github: https://github.com/codeliker*/ #include <WinSock2.h> ...
- [Ubuntu]明明白白安装中文字体
Ubuntu 6.06 dapper对中文的支持应该说是一向不错的,包括现在默认安装就包含了scim输入法.然而,一般安装的ubuntu虽然能够显示.输入中文了,但总觉 得所显示的中文像歪瓜裂枣,很是 ...
- Cognos TM1_10.1.1服务端安装
出于对bi行业的强大热爱,出于对cognos tm1的强大兴趣,于是就想研究一下Cognos TM1(table manager one),今天就分享一下自己微不足道研究成果,真可谓是tm1的九牛一毛 ...
- hdoj5645DZY Loves Balls
Problem Description DZY loves playing balls. He has n balls in a big box. On each ball there is an i ...
- 第一个Xamarin的 Android 应用程序!
你好,安卓 Xamarin的工作室 Xamarin的应用程序图标和启动屏幕 脱机使用PDF格式: 介绍与Xamarin的Android开发 示例代码: 开始使用应用程序的探险家 显示说明: Visua ...
- 用thinkphp执行原生sql
Controller代码: Demo2Controller.class.php <?php namespace Home\Controller; use Think\Controller; us ...
- iOS arc和非arc 适用 宏
iOS arc和非arc 适用 宏 1:使用宏 + (void)showAlertWithMessage:(NSString *)messages { dispatch_async(dispatch_ ...
- IOS手机使用Fiddler抓获HTTPS报文方法
Configure Fiddler Click Tools > Fiddler Options > Connections. Click the checkbox by Allow rem ...