论坛首页 Java企业应用论坛

基于Spring的Hibernate Search全文检索功能示例

浏览 5205 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (2)
作者 正文
   发表时间:2009-02-25  
数据库:Oracle 9i
JDBC驱动:OJDBC14
开发环境:Eclipse-JEE
Spring版本:Spring 2.0.6
Hibernate版本:Hibernate Core 3.2.5/Hibernate Annotation 3.3.0/Hibernate Validator 3.0.0/Hibernate Search 3.0.0 Beta4

//jdbc.properties(JDBC配置文件)
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.0.3:1521:itone
jdbc.username=test
jdbc.password=test


//hibernate.properties(Hibernate配置文件)
hibernate.dialect=org.hibernate.dialect.Oracle9Dialect
hibernate.show_sql=false
hibernate.cache.use_query_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.hbm2ddl.auto=update
######以下是Hibernate Search的简单配置######
hibernate.search.default.directory_provider=org.hibernate.search.store.FSDirectoryProvider
hibernate.search.default.indexBase=E:/indexes


//Foo.java(Hibernate POJO文件)
@Entity
@Table(name = "search_foo")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Indexed(index = "search_foo")
public class Foo implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@DocumentId
private Integer id;

@Column(nullable = false, length = 256)
@Field(name = "name", index = Index.TOKENIZED, store = Store.YES)
private String name;

@Lob
@Field(name = "content", index = Index.TOKENIZED, store = Store.YES)
private String content;

@Column(name = "post_time", nullable = false)
@DateBridge(resolution = Resolution.DAY)
private Calendar postTime;

public Integer getId() {
     return id;
}

public void setId(Integer id) {
     this.id = id;
}

public String getName() {
     return name;
}

public void setName(String name) {
     this.name = name;
}

public String getContent() {
     return content;
}

public void setContent(String content) {
     this.content = content;
}

public Calendar getPostTime() {
     return postTime;
}

public void setPostTime(Calendar postTime) {
     this.postTime = postTime;
}
}

//hibernate.cfg.xml(Hibernate配置文件)
<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>

     <mapping class="com.itone.search.pojo.Foo"/>
  
</session-factory>

</hibernate-configuration>


//applicationContext-conf.xml(加载.properties文件的配置文件)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans default-autowire="byName">

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-init="false">
     <property name="locations">
      <list>
       <value>classpath*:jdbc.properties</value>
       <value>classpath*:hibernate.properties</value>
      </list>
     </property>
</bean>

</beans>


//applicationContext-database.xml(数据源及SessionFactory定义文件)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans default-autowire="byName">

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
     <property name="driverClassName" value="${jdbc.driverClassName}"/>
     <property name="url" value="${jdbc.url}"/>
     <property name="username" value="${jdbc.username}"/>
     <property name="password" value="${jdbc.password}"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
     <property name="configLocation" value="/WEB-INF/classes/hibernate.cfg.xml"/>
     <property name="hibernateProperties">
      <props>
       <prop key="hibernate.dialect">${hibernate.dialect}</prop>
       <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
       <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
       <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
       <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
    
       <prop key="hibernate.search.default.directory_provider">${hibernate.search.default.directory_provider}</prop>
       <prop key="hibernate.search.default.indexBase">${hibernate.search.default.indexBase}</prop>
      </props>
     </property>
</bean>

<bean id="jdbcExceptionTranslator" class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator"/>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"/>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"/>

</beans>


//applicationContext-manager.xml(Manager定义文件)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans default-autowire="byName">

<bean id="baseTxService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
     <property name="proxyTargetClass" value="true"/>
           <property name="transactionAttributes">
      <props>
       <prop key="*">PROPAGATION_REQUIRED</prop>
      </props>
     </property>
</bean>

<!-- Authentication manager for login and role checking -->
<bean id="fooManager" parent="baseTxService">
     <property name="target">
      <bean class="com.itone.search.manager.FooManager"/>
     </property>
</bean>

</beans>


//SpringMVC、Log4j、EHCache配置略

//FooManager.java(做全文检索示例)
package com.itone.search.manager;

import java.util.List;

import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Query;
import org.hibernate.search.FullTextQuery;
import org.hibernate.search.FullTextSession;

import com.itone.search.pojo.Foo;

public class FooManager extends BaseManager<Foo> {

@SuppressWarnings("unchecked")
public void testQuery() throws Exception {
     QueryParser parser = new QueryParser("name", new StopAnalyzer());
     Query luceneQuery = parser.parse("name:Jack");
     FullTextSession s = Search.createFullTextSession(getSession());
     FullTextQuery query = s.createFullTextQuery(luceneQuery, Foo.class);
     List<Foo> result = query.list();
     for(Foo f : result) {
      System.out.println("ID:" + f.getId());
      System.out.println("NAME:" + f.getName());
      System.out.println("CONTENT:" + f.getContent());
      System.out.println("POSTTIME:" + f.getPostTime());
     }
}
}
   发表时间:2009-02-25  
看你上面的代码实在是太麻烦了。还不如直接用lucene写!
spring真的是把简单问题复杂化。
0 请登录后投票
   发表时间:2009-05-08  
johnnyhg 写道
看你上面的代码实在是太麻烦了。还不如直接用lucene写!
spring真的是把简单问题复杂化。


嗯,我最近的商用测试也发现,hibernate search包装的很烂,只是实现在功能而已。在商业用途上还是有很大的问题。比如:用FullTextQuery来list的时候,生成的sql语句是:select ... from Foo where id in(?,?,?...) or id in (?, ?, ?...) or ...这样,对于结果集很大的情况(如32768条)就很慢了。
0 请登录后投票
   发表时间:2009-05-17  
为什么不会自己建立索引,更新,添加,也全部不会建立或更新索引,是怎么回事啊
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics