大数据量中的模糊查询优化方案

----------------------------------------------------------------------------------------------
[版权申明:本文系做者原创,转载请注明出处] 
文章出处:http://blog.csdn.net/sdksdk0/article/details/52589761
做者:朱培      ID:sdksdk0     javascript

--------------------------------------------------------------------------------------------html

 

对工做单使用 like模糊查询时,实际上 数据库内部索引没法使用 ,须要逐条比较查询内容,效率比较低在数据量不少状况下, 提供模糊查询性能,咱们能够使用lucene全文索引库技术。本文示例是在SSH框架中进行使用。使用Hibernate Search (用来整合 Hibernate + Lucene),工做单搜索功能。java

 

一、首先能够在咱们的maven工程中引入须要的jar包,web

 

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-search</artifactId>
			<version>3.4.2.Final</version>
		</dependency>


二、导入IKAnalyzer分词器。由于IKAnalyzer在maven中没有,因此咱们须要手动下载这个jar包,固然了,在http://mvnrepository.com/网站上面能够找到。spring

 

下载好以后能够装载到你本身的maven仓库中或者直接放在你工程的lib目录中,而后来引用:例如个人是在sql

 

 

		<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>


三、在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


四、新建一个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>


五、在spring中进行配置:在配置SessionFactory中加入一行:固然了,这个时候须要本身去D盘目录中新建一个文件夹DBIndexsession

 

 

 

<!-- 配置索引库 -->
				<prop key="hibernate.search.default.indexBase">d:/DBIndex</prop>


完整的以下:app

 

 

 

<!-- 配置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>


六、在想要实现查询功能的那个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,便可打开下图这个页面,查看具体的索引信息。

 

 

 


七、在界面中添加搜索框,我这里使用的是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>


八、写doSeach这个js函数

 

 

	function doSearch(value,name){
		//将查询条件缓存到datagrid
		$('#grid').datagrid('load',{
			conditionName:name,
			conditionValue:value
		});
	}


九、在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;
	}

返回值以后如何处理这里我就不在说了。

 

十、在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;
	}


十一、在页面中查看搜索的效果

 

 

 

 

这样咱们整个开发流程就完成了。使用luence对大数据量中的模糊查询是很是实用的功能,固然了,luence只适用于站内搜索,对于模糊查询的支持仍是很是好的。

相关文章
相关标签/搜索