现在的位置: 首页 > 综合 > 正文

compass指南针搜索框架学习(二)

2018年05月26日 ⁄ 综合 ⁄ 共 18185字 ⁄ 字号 评论关闭

一、准备

    个人在学习中采用Struts2 + Hibernate3.2 + Spring2.5 + Compass2.2.0, 一下图片为本次学习中用到的jar包:

 

图中圈出的jar包为本次学习的主要部分,另外用绿色框圈出的jar包为分词器,主要用来做实验看分词效果的,选用一个即可。

二、什么是Compass

    Compass是一个Java搜索框架。它封装了Lucene,增加了一些Lucene不支持的特性(例如实时更新索引),支持各种数据(Java对象、xml、json)到索引的映射,支持各种数据源(JDBC, Hibernate, iBatis)

 

 

图解:

  • Compass - 一般在程序启动时建立并被整个程序共享,主要用于建立CompassSession并通过其管理索引数据。与Hibernate的SessionFactory类似
  • CompassSession - 用于处理数据的session。与Hibernate的Session类似
  • CompassTransaction - 手动进行事务管理,如果不使用,Compass会自动管理事务。与Hibenate的事物管理类似
  • CompassTemplate - 将session和transaction透明化。类似Spring提供的HibernateTemplate
  • 数据到索引的各种映射 - OSEM, XSEM, JSEM, RSEM。支持通过程序、XML、JSON进行配置。
  • CompassGps - Gps的核心模块,管理GpsDevice,有两种实现:SingleCompassGps和DualCompassGps。
  • CompassGpsDevice - 处理各种数据源到索引的操作:JDBC, Hibernate, iBatis等。不能独立使用而必须融合到CompassGps中。

三、与Spring、Hibernate整合

 

     这里主要结合代码进行。

    

     1.数据库脚本(Oracle)

[java] view
plain
copy

  1. --创建表Article  
  2. create table ARTICLE  
  3. (  
  4.   ID      NUMBER,--ID,主键  
  5.   TITLE   VARCHAR2(100),--标题  
  6.   CONTENT CLOB,--文章内容  
  7.   PUBDATE DATE--发布日期  
  8. )  

   

    2.配置Compass的OSEM 以及Hibernate映射

[java] view
plain
copy

  1. import java.io.Serializable;  
  2. import java.util.Date;  
  3.   
  4. import javax.persistence.Column;  
  5. import javax.persistence.Entity;  
  6. import javax.persistence.GeneratedValue;  
  7. import javax.persistence.Id;  
  8. import javax.persistence.Lob;  
  9. import javax.persistence.Table;  
  10. import javax.persistence.Temporal;  
  11. import javax.persistence.TemporalType;  
  12.   
  13. import org.compass.annotations.Index;  
  14. import org.compass.annotations.Searchable;  
  15. import org.compass.annotations.SearchableId;  
  16. import org.compass.annotations.SearchableProperty;  
  17. import org.compass.annotations.Store;  
  18. import org.hibernate.annotations.GenericGenerator;  
  19.   
  20.   
  21. @Searchable(alias = "article")  
  22. @Entity  
  23. @Table(name = "ARTICLE", schema = "SCOTT")  
  24. public class Article implements Serializable {  
  25.   
  26.     private static final long serialVersionUID = 1L;  
  27.   
  28.     private Long id;  
  29.     private String title;  
  30.     private Date pubdate = new Date();  
  31.     private String content;  
  32.   
  33.     @SearchableId(  
  34.             name = "id",   
  35.             store = Store.NO,   
  36.             index = Index.NOT_ANALYZED)  
  37.     @Id  
  38.     @GeneratedValue(generator = "paymentableGenerator")  
  39.     @GenericGenerator(name = "paymentableGenerator", strategy = "increment")  
  40.     public Long getId() {  
  41.         return id;  
  42.     }  
  43.   
  44.     public void setId(Long id) {  
  45.         this.id = id;  
  46.     }  
  47.   
  48.     @SearchableProperty(  
  49.             name = "title",   
  50.             store = Store.YES,   
  51.             index = Index.ANALYZED)  
  52.     @Column(name = "TITLE")  
  53.     public String getTitle() {  
  54.         return title;  
  55.     }  
  56.   
  57.     public void setTitle(String title) {  
  58.         this.title = title;  
  59.     }  
  60.   
  61.     @SearchableProperty(  
  62.             name = "pubdate",   
  63.             store = Store.NO,   
  64.             index = Index.UN_TOKENIZED)  
  65.     @Temporal(TemporalType.TIMESTAMP)  
  66.     @Column(name = "PUBDATE")  
  67.     public Date getPubdate() {  
  68.         return pubdate;  
  69.     }  
  70.   
  71.     public void setPubdate(Date pubdate) {  
  72.         this.pubdate = pubdate;  
  73.     }  
  74.   
  75.     @SearchableProperty(  
  76.             name = "content",   
  77.             index = Index.TOKENIZED,   
  78.             store = Store.YES,   
  79.             converter = "htmlPropertyConverter")  
  80.     @Lob  
  81.     @Column(name = "CONTENT")  
  82.     public String getContent() {  
  83.         return content;  
  84.     }  
  85.   
  86.     public void setContent(String content) {  
  87.         this.content = content;  
  88.     }  
  89.   
  90. }  

    说明:

    @Searchable(alias="article")表示这个是可以搜索实体,别名为article. 
    @SearchableId  这个是实体搜索的标识ID,和hibernate里的概念差不多,用来区分索引文件里的实体索引。 
    @SearchableProperty(index = Index.NOT_ANALYZED, store = Store.NO) 表示这个属性存入索引文件,不进行分词,不存储要索引中。

    另外在getContent()方法上的@SearchableProperty中还加入了converter = "htmlPropertyConverter",主要是用来将文章中的HTML标签进行过滤获取纯文本,在建立到索引中。在后面会具体介绍这个转换器。

 

    3.建立Compass搜索的类

[java] view
plain
copy

  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.   
  4. import javax.annotation.Resource;  
  5.   
  6. import org.compass.core.Compass;  
  7. import org.compass.core.CompassHighlighter;  
  8. import org.compass.core.CompassHits;  
  9. import org.compass.core.CompassQuery;  
  10. import org.compass.core.CompassQueryBuilder;  
  11. import org.compass.core.CompassSession;  
  12. import org.compass.core.CompassTemplate;  
  13. import org.compass.core.CompassHighlighter.TextTokenizer;  
  14. import org.compass.core.CompassQuery.SortPropertyType;  
  15. import org.springframework.stereotype.Component;  
  16.   
  17. import com.compass.example.dao.SearchArticleDao;  
  18. import com.compass.example.model.Article;  
  19.   
  20. @Component("SearchArticleDao")  
  21. public class SearchArticleDaoCompass implements SearchArticleDao {  
  22.       
  23.     @Resource  
  24.     private CompassTemplate compassTemplate;  
  25.       
  26.     @Override  
  27.     public List<Article> searchWithList(final String queryString) {  
  28.           
  29.         Compass compass = compassTemplate.getCompass();  
  30.         CompassSession session = compass.openSession();  
  31.         CompassQueryBuilder builder = session.queryBuilder();  
  32.         CompassQuery compassQuery = builder.queryString(queryString).toQuery().addSort("article.id",SortPropertyType.STRING);  
  33.         CompassHits compassHits = compassQuery.hits();  
  34.           
  35.           
  36.         List<Article> articles = new ArrayList<Article>();  
  37.         for(int i=0; i<compassHits.length(); i++) {  
  38.             Article article = (Article) compassHits.data(i);  
  39.             CompassHighlighter highlighter = compassHits.highlighter(i);  
  40.             String title = highlighter.fragment("title");  
  41.             if(title != null) {  
  42.                 article.setTitle(title);  
  43.             }  
  44.             String content = highlighter.setTextTokenizer(TextTokenizer.AUTO).fragment("content");  
  45.             if(content != null) {  
  46.                 article.setContent(content);  
  47.             }  
  48.             articles.add(article);  
  49.         }  
  50.         return articles;  
  51.     }  
  52.   
  53. }  

索引的查询主要是根据传过来的参数,关键字keyword,是搜索的关键字

String title = hits.highlighter(i).fragment("title");这段是检索titile这个属性有没有出现搜索的关键字,有就将它高亮(其实就是在关键字前后加个html标记设置颜色,等下可以看到在配置文件里可以自由设置高亮的颜色). 
String content = hits.highlighter(i).setTextTokenizer( 
CompassHighlighter.TextTokenizer.AUTO) 
.fragment("content"); 
这段代码和上面的title具有一样的一样的功能,另外还多了个很重要的功能,自动选择正文中最匹配关键字的内容中的一部分输出。因为很多时候一篇文章几千字,我们只想显示有关键字的那部分的摘要,这时候这个功能就很方便.

 

    4.建立索引,将在服务器启动时或定时重建索引

[java] view
plain
copy

  1. import org.compass.gps.CompassGps;  
  2. import org.springframework.beans.factory.InitializingBean;  
  3.   
  4. /** 
  5.  * 通过quertz 定时调度定时重建索引或自动随Spring ApplicationContext 启动而重建 
  6.  * 索引的Builder。会在启动后延时数秒新开线程调用compassGps.index()函数。 
  7.  * 默认会在Web应用每次启动时重建索引,可以设置BuildIndex属性为false来禁止此功能。 
  8.  * 也可以不用本builder,编写手动调用compasssGps.index()的代码。 
  9.  * @author YinGuojun 
  10.  * 
  11.  */  
  12. public class CompassIndexBuilder implements InitializingBean {  
  13.   
  14.     /*是否需要建立索引,可以被设置为false使本Builder失效*/  
  15.     private boolean buildIndex = false;  
  16.     /*索引操作线程延时启动的时间,单位为秒*/  
  17.     private int lazyTime = 10;  
  18.     /*Compass封装*/  
  19.     private CompassGps compassGps;  
  20.       
  21.     private Thread indexThread = new Thread() {  
  22.   
  23.         @Override  
  24.         public void run() {  
  25.             try {  
  26.                 System.out.println("lazyTime: " + lazyTime);  
  27.                 Thread.sleep(lazyTime * 1000);  
  28.                 System.out.println("begin compass index ...");  
  29.                 long beginTime = System.currentTimeMillis();  
  30.                   
  31.                 // 重建索引.  
  32.                 // 如果compass实体中定义的索引文件已存在,索引过程中会建立临时索引,  
  33.                 // 索引完成后再进行覆盖.  
  34.                 compassGps.index();  
  35.                   
  36.                 long costTime = System.currentTimeMillis() - beginTime;  
  37.                 System.out.println("compss index finished.");  
  38.                 System.out.println("costed " + costTime + " milliseconds");  
  39.                   
  40.             } catch (InterruptedException e) {  
  41.                 e.printStackTrace();  
  42.             }  
  43.               
  44.         }  
  45.           
  46.     };  
  47.       
  48.     /** 
  49.      * 实现<code>InitializingBean</code>接口,在完成注入后调用启动索引线程. 
  50.      * 
  51.      * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() 
  52.      */  
  53.     @Override  
  54.     public void afterPropertiesSet() throws Exception {  
  55.         if (buildIndex) {  
  56.             indexThread.setDaemon(true);  
  57.             indexThread.setName("Compass Indexer");  
  58.             indexThread.start();  
  59.         }  
  60.     }  
  61.   
  62.     public boolean isBuildIndex() {  
  63.         return buildIndex;  
  64.     }  
  65.   
  66.     public void setBuildIndex(boolean buildIndex) {  
  67.         this.buildIndex = buildIndex;  
  68.     }  
  69.   
  70.     public int getLazyTime() {  
  71.         return lazyTime;  
  72.     }  
  73.   
  74.     public void setLazyTime(int lazyTime) {  
  75.         this.lazyTime = lazyTime;  
  76.     }  
  77.   
  78.     public CompassGps getCompassGps() {  
  79.         return compassGps;  
  80.     }  
  81.   
  82.     public void setCompassGps(CompassGps compassGps) {  
  83.         this.compassGps = compassGps;  
  84.     }  
  85.   
  86. }  

  

   5.转换器

  

[java] view
plain
copy

  1. import org.apache.log4j.Logger;  
  2. import org.compass.core.Property;  
  3. import org.compass.core.converter.ConversionException;  
  4. import org.compass.core.converter.basic.AbstractBasicConverter;  
  5. import org.compass.core.mapping.ResourcePropertyMapping;  
  6. import org.compass.core.marshall.MarshallingContext;  
  7.   
  8. import com.compass.example.utils.StringUtil;  
  9.   
  10. public class HtmlPropertyConverter extends AbstractBasicConverter<String> {  
  11.   
  12.     private static Logger logger = Logger.getLogger(HtmlPropertyConverter.class);  
  13.       
  14.     public HtmlPropertyConverter() {  
  15.         super();  
  16.         // called by application server starting  
  17.         logger.info("----------HtmlPropertyConverter Initializing ...");  
  18.     }  
  19.       
  20.     /** 
  21.      *  搜索的时候被调用 
  22.      */  
  23.     @Override  
  24.     protected String doFromString(String str,  
  25.             ResourcePropertyMapping resourcePropertyMapping,  
  26.             MarshallingContext context) throws ConversionException {  
  27.         logger.info("----------calling doFromString...");  
  28.         return str;  
  29.     }  
  30.   
  31.       
  32.     /** 
  33.      * 创建索引的时候被调用,此时将文本中的HTML标签过滤 
  34.      */  
  35.     @Override  
  36.     protected Property createProperty(String value,  
  37.             ResourcePropertyMapping resourcePropertyMapping,  
  38.             MarshallingContext context) {  
  39.         logger.info("----------calling createProperty...");  
  40.                   //过滤html标签  
  41.         value = StringUtil.removeHTML(value);  
  42.           
  43.         return super.createProperty(value, resourcePropertyMapping, context);  
  44.     }  
  45.       
  46.   
  47. public class StringUtil {  
  48. /** 
  49.      * Remove occurences of html, defined as any text 
  50.      * between the characters "<" and ">". Optionally  
  51.      * replace HTML tags with a space. 
  52.      * @param str 
  53.      * @param addSpace 
  54.      * @return 
  55.      */  
  56.     public static String removeHTML(String str, boolean addSpace) {  
  57.           
  58.         //System.out.println(str);  
  59.           
  60.         if(str == nullreturn "";  
  61.         StringBuffer ret = new StringBuffer(str.length());  
  62.         int start = 0;  
  63.         int beginTag = str.indexOf("<");  
  64.         int endTag = 0;  
  65.         if(beginTag == -1return str;  
  66.           
  67.         while(beginTag >= start) {  
  68.             if(beginTag > 0) {  
  69.                 ret.append(str.substring(start, beginTag));  
  70.                   
  71.                 // replace each tag with a space (looks better)  
  72.                 if(addSpace) ret.append(" ");  
  73.             }  
  74.             endTag = str.indexOf(">", beginTag);  
  75.               
  76.             // if endTag found move "cursor" forward  
  77.             if(endTag > -1) {  
  78.                 start = endTag + 1;  
  79.                 beginTag = str.indexOf("<", start);  
  80.             }  
  81.               
  82.             // if no endTag found, get rest of str and break  
  83.             else {  
  84.                 ret.append(str.substring(beginTag));  
  85.                 break;  
  86.             }  
  87.         }  
  88.         // append everything after the last endTag  
  89.         if(endTag >-1 && endTag + 1 < str.length()) {  
  90.             ret.append(str.substring(endTag + 1));  
  91.         }  
  92.           
  93.         //System.out.println(ret.toString());  
  94.           
  95.         return ret.toString().trim();  
  96.     }  
  97.       
  98.     /** 
  99.      * Remove occurences of html, defined as any text 
  100.      * between the characters "<" and ">". 
  101.      * Replace any HTML tags with a space.  
  102.      * @param str 
  103.      * @return 
  104.      */  
  105.     public static String removeHTML(String str) {  
  106.         return removeHTML(str, true);  
  107.     }  
  108. }  
  109.       

 

   5.配置文件

 

[xhtml] view
plain
copy

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  7.   
  8.     <bean id="annotationConfiguration" class="org.compass.annotations.config.CompassAnnotationsConfiguration"/>  
  9.       
  10.     <bean id="compass" class="org.compass.spring.LocalCompassBean">  
  11.         <property name="resourceDirectoryLocations">  
  12.             <list>  
  13.                 <value>classpath:com/compass</value>  
  14.             </list>  
  15.         </property>  
  16.         <!--  如果compass有单独的配置文件,可以从这里引入  
  17.         <property name="configLocation" value="classpath:compass.cfg.xml"/>  
  18.          -->  
  19.         <!-- 数据索引存储位置 -->  
  20.         <property name="connection" value="/lucene/indexes"/>  
  21.            
  22.         <property name="classMappings">  
  23.             <list>  
  24.                 <value>com.compass.example.model.Product</value>  
  25.                 <value>com.compass.example.model.Article</value>  
  26.             </list>  
  27.         </property>  
  28.         <property name="compassConfiguration" ref="annotationConfiguration"/>  
  29.         <property name="compassSettings">  
  30.             <props>  
  31.                 <!-- 建立索引位置的另一种方式  
  32.                 <prop key="compass.engine.connection">  
  33.                     file://${user.home}/lucene/indexes  
  34.                 </prop>  
  35.                  -->  
  36.                 <prop key="compass.transaction.factory">  
  37.                     org.compass.spring.transaction.SpringSyncTransactionFactory  
  38.                 </prop>  
  39.                   
  40.                 <!-- 指定摘要文本的长度 -->  
  41.                 <prop key="compass.engine.highlighter.default.fragmenter.simple.size">  
  42.                     200  
  43.                 </prop>  
  44.                   
  45.                 <!-- 搜索内容高亮显示 -->  
  46.                 <prop  key="compass.engine.highlighter.default.formatter.simple.pre">     
  47.                     <![CDATA[<span style='background-color:yellow;color:red;'>]]>     
  48.                 </prop>     
  49.                 <prop  key="compass.engine.highlighter.default.formatter.simple.post">     
  50.                   <![CDATA[</span>]]>     
  51.                 </prop>   
  52.                 <!--定义分词器-->         
  53.                 <!--          
  54.                 <prop     
  55.                     key="compass.engine.analyzer.default.type">     
  56.                    org.wltea.analyzer.lucene.IKAnalyzer  
  57.                 </prop>    
  58.                 -->  
  59.                 <!--   
  60.                 <prop key="compass.engine.analyzer.MMAnalyzer.CustomAnalyzer">  
  61.                     org.wltea.analyzer.lucene.IKAnalyzer  
  62.                     jeasy.analysis.MMAnalyzer  
  63.                     net.paoding.analysis.analyzer.PaodingAnalyzer  
  64.                 </prop>  
  65.                 -->  
  66.                   
  67.                 <prop key="compass.engine.analyzer.default.type">  
  68.                     org.wltea.analyzer.lucene.IKAnalyzer  
  69.                 </prop>  
  70.                    
  71.             </props>  
  72.         </property>  
  73.         <property name="transactionManager" ref="transactionManager"/>  
  74.         <property name="convertersByName">  
  75.             <map>  
  76.                 <entry key="htmlPropertyConverter">  
  77.                     <bean class="com.compass.converter.HtmlPropertyConverter"/>  
  78.                 </entry>  
  79.             </map>  
  80.         </property>  
  81.     </bean>  
  82.   
  83.     <bean id="hibernateGpsDevice" class="org.compass.gps.device.hibernate.HibernateGpsDevice">  
  84.         <property name="name" value="hibernateDevice"/>  
  85.         <property name="sessionFactory" ref="sessionFactory"/>  
  86.         <property name="mirrorDataChanges" value="true"/>  
  87.     </bean>  
  88.   
  89.     <!-- 数据库中的记录变化后同步更新索引 -->  
  90.     <bean id="compassGps" class="org.compass.gps.impl.SingleCompassGps" init-method="start" destroy-method="stop">  
  91.         <property name="compass" ref="compass"/>  
  92.         <property name="gpsDevices">  
  93.             <list>  
  94.                 <!-- compass2.1  
  95.                 <bean class="org.compass.spring.device.SpringSyncTransactionGpsDeviceWrapper">  
  96.                     <property name="gpsDevice" ref="hibernateGpsDevice"/>  
  97.                 </bean>  
  98.                  -->  
  99.                  <!-- compass2.2 -->  
  100.                  <ref local="hibernateGpsDevice"/>  
  101.             </list>  
  102.         </property>  
  103.     </bean>  
  104.   
  105.     <!-- compass模板 -->  
  106.     <bean id="compassTemplate" class="org.compass.core.CompassTemplate">  
  107.         <property name="compass" ref="compass"/>  
  108.     </bean>  
  109.       
  110.     <!-- 定时重建索引(利用quartz)或随Spring ApplicationContext启动而重建索引 -->  
  111.     <bean id="compassIndexBuilder" lazy-init="false" class="com.compass.example.utils.CompassIndexBuilder">  
  112.         <property name="compassGps" ref="compassGps"/>  
  113.         <property name="buildIndex" value="true"/>  
  114.         <property name="lazyTime" value="10"/>  
  115.     </bean>  
  116. </beans>  

 

    6.效果(英文)

 

     中文

 

 

四、问题总结

 

    1.异常there are more terms than documents in field "XXX", but it's impossible to sort on tokenized fields.

    在Luncene的API中对Sort的说明中有以下描述:

    The fields used to determine sort order must be carefully chosen. Documents must contain a single term in such a field, and the value of the term should indicate the document's relative position in a given sort order.The
field must be indexed, but should not be tokenized, and does not need to be stored

(unless you happen to want it back with the rest of your document data). In other words:

document.add (new Field ("byNumber", Integer.toString(x), Field.Store.NO, Field.Index.NOT_ANALYZED));

    描述中红色部分需特别注意,排序的字段必须被索引,并且不应该被tokennized,也就是在注解@SearchableProperty中的index=Index.NOT_ANALYZED, store=Store.NO,括号中的说明不是很明白,希望知道的可以给我点提示,再此谢谢了。

   

    2.异常java.lang.RuntimeException: field "XXX" does not appear to be indexed

    对多个表建索引后进行搜索,在添加排序条件时,如果不指定SortPropertyType,那么在没有指定converter的字段上排序时会抛以上异常, 但如果只对单个表建索引,不会有这个问题。

 

五、本次学习在网上查找各种资料的汇总,对引用到别处博客内容的博主表示感谢!文章尚有很多不完善之处,望指正,本人不胜感激!

六、其他资料

     Compass入门

     http://www.yeeach.com/2008/03/23/compass-%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/

  

     关于高亮显示的解决方案

     http://jdkcn.com/entry/the-better-revolution-about-the-compass-lucene-highlight.html,此网站开放源码,有助于大家学习

抱歉!评论已关闭.