springside + hibernate 3 + hiberate search

使用 hibernate search 不需要太多lucene 的知识,就能很容易的使用好。在使用之前一定要把jar包搭配好,不然不兼容会让你很头痛。其实摆脱不兼容的问题很简单,你只要把你下载的hibernate-search-3.0.1.GA.zip  解压后,把里面lib目录的jar包全部考到你项目资源文件里就可以了。你也可以看看lib里的readme.txt,里面写了需要的包。下面配置十分简单,实体bean 里配@Indexed @DocumentId @Field  

 

searchhibernate.cfg.xml : <!DOCTYPE hibernate-configuration PUBLIC  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  "D://work//svn//ahga1//test//resources//spring//test//hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory>   <property name="hibernate.dialect">   org.hibernate.dialect.OracleDialect  </property>  <property name="hibernate.connection.driver_class">   oracle.jdbc.driver.OracleDriver  </property>  <property name="hibernate.connection.url">   jdbc:oracle:thin:@192.168.84.15:1521:ahga  </property>  <property name="hibernate.connection.username">   ahga_qb  </property>  <property name="hibernate.connection.password">ahga</property>  <property name="hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider</property>        <property name="hibernate.search.default.indexBase">E:/seamindex</property>  <mapping class="com.iflytek.police.ahga.us.domain.User"/>  <mapping class="com.iflytek.police.ahga.us.domain.Theme"/>  <mapping class="com.iflytek.police.ahga.us.domain.Menu"/>  <mapping class="com.iflytek.police.ahga.qb.domain.ArticleInfo"/>  <mapping class="com.iflytek.police.ahga.qb.domain.CheckResult"/>  <mapping class="com.iflytek.police.ahga.qb.domain.DynamicInfo"/>  <mapping class="com.iflytek.police.ahga.qb.domain.InfoFlowStep"/>  <mapping class="com.iflytek.police.ahga.qb.domain.AttachFile" />  <mapping class="com.iflytek.police.ahga.qb.domain.FeedBack" />  <mapping class="com.iflytek.police.ahga.qb.domain.Meeting" />  <mapping class="com.iflytek.police.ahga.qb.domain.TaskInfo" />  <mapping class="com.iflytek.police.ahga.qb.domain.InfoSubject" />  <mapping class="com.iflytek.police.ahga.qb.domain.CaseBunch" />  <mapping class="com.iflytek.police.ahga.qb.domain.WordClass" />  <mapping class="com.iflytek.police.ahga.qb.domain.ElectronicMedium" />   <mapping class="com.iflytek.police.ahga.us.domain.Dept" />  <mapping class="com.iflytek.police.ahga.us.domain.Resource" />  <mapping class="com.iflytek.police.ahga.us.domain.Role" /> </session-factory></hibernate-configuration>   

创建索引:

 public static void testcreateIndex() throws Exception { 

  sf = new AnnotationConfiguration()    .configure(      new File(        "D:/work/svn/ahga1/test/unit/com/iflytek/police/ahga/us/service/searchhibernate.cfg.xml"))    .buildSessionFactory();     

session = sf.openSession();  

FullTextSession fullTextSession = Search.createFullTextSession(session);  

tx = session.beginTransaction();   tx.begin();     

 //create User index   Query userquery = session.createQuery("from User");   List<User> userList = userquery.list();   System.out.println("size=" + userList.size());   if (userList != null && userList.size() > 0) {    for (User u : userList) {     System.out.println(" username=" + u.getUserName());     System.out.println(" loginname=" + u.getLoginName());    fullTextSession.index(u);   }  }    

 tx.commit();  session.close();  if (sf != null)   sf.close();  }      

 

 搜索:

public static void testHunhe() throws Exception {   

String word = "a";  sf = new AnnotationConfiguration()    .configure(      new File(        "D:/work/svn/ahga1/test/unit/com/iflytek/police/ahga/us/service/searchhibernate.cfg.xml"))    .buildSessionFactory(); 

 session = sf.openSession(); 

 tx = session.beginTransaction();  

tx.begin();  

FullTextSession fullTextSession = Search.createFullTextSession(session); 

 Map<String, Float> boostPerField = new HashMap<String, Float>();  

boostPerField.put("loginName", 4f);  

boostPerField.put("userName", 1f);  

String[] productFields = { "loginName", "userName" };  

QueryParser parser = new MultiFieldQueryParser(productFields,    new StandardAnalyzer(), boostPerField);   parser.setAllowLeadingWildcard(true);  

org.apache.lucene.search.Query luceneQuery;   

try {   

luceneQuery = parser.parse(word);   

org.apache.lucene.search.Query mnquery = new WildcardQuery(     new Term("loginName", "*" + word + "*"));   

org.apache.lucene.search.Query dquery = new WildcardQuery(new Term(     "userName", "*" + word + "*"));   

org.apache.lucene.search.Query deptquery = new WildcardQuery(new Term(     "dept.deptname", "*" + word + "*"));   

BooleanQuery bquery = new BooleanQuery(); 

  bquery.add(mnquery, Occur.SHOULD);  

 bquery.add(dquery, Occur.SHOULD);   

bquery.add(deptquery, Occur.SHOULD);   

bquery.add(luceneQuery, Occur.SHOULD);  

 List list = fullTextSession.createFullTextQuery(bquery, User.class)     .setMaxResults(100).list();   

System.out.println("testHunhe size=" + list.size());  

 for (int i = 0; i < list.size(); i++) {    User u = (User) list.get(i);    

System.out.println(" username=" + u.getUserName());    

System.out.println(" loginname=" + u.getLoginName());   }

  }

catch (ParseException e) {   e.printStackTrace();   }  }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值