首先是spring與compass的集成配置文件:applicationContext-compass.xml
- <? xml version = "1.0" encoding = "UTF-8" ?>
- < beans xmlns = "http://www.springframework.org/schema/beans"
- xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context = "http://www.springframework.org/schema/context"
- xsi:schemaLocation ="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
- default-lazy-init = "true" >
- <!-- 配置compass註解 -->
- < bean id = "annotationConfiguration" class = "org.compass.annotations.config.CompassAnnotationsConfiguration" >
- </ bean >
- <!-- 配置compass bean -->
- < bean id = "compass" class = "org.compass.spring.LocalCompassBean" >
- <!-- 定義索引存放路徑 -->
- < property name = "connection" >
- < value > file://${index.path} </ value >
- </ property >
- <!-- 配置需要創建索引的實體 -->
- < property name = "classMappings" >
- < list >
- < value > com.***.mobile.entity.KnowledgeInfo </ value >
- < value > com.***.mobile.entity.ChannelInfo </ value >
- < value > com.***.mobile.entity.MobileBaseEntity </ value >
- < value > com.***.mobile.entity.BaseEntity </ value >
- < value > com.***.mobile.entity.BusLine </ value >
- </ list >
- </ property >
- < property name = "compassConfiguration" ref = "annotationConfiguration" > </ property >
- < property name = "compassSettings" >
- < props >
- <!-- compass事務工廠 -->
- < prop key = "compass.transaction.factory" > org.compass.spring.transaction.SpringSyncTransactionFactory </ prop >
- <!-- paoding分詞器 -->
- < prop key = "compass.engine.analyzer.MMAnalyzer.CustomAnalyzer" > net.paoding.analysis.analyzer.PaodingAnalyzer </ prop >
- <!-- 設置高亮顯示 -->
- < prop key = "compass.engine.highlighter.default.formatter.simple.pre" > <![CDATA[<font color="#cc0033"><b>]]> </ prop >
- < prop key = "compass.engine.highlighter.default.formatter.simple.post" > <![CDATA[</b></font>]]> </ prop >
- </ props >
- </ property >
- < property name = "transactionManager" ref = "transactionManager" />
- </ bean >
- <!-- 用Hibernate3事件系統,支持Real Time Data Mirroring.經Hiberante改變的數據會自動被反射到索引裏面 -->
- < bean id = "hibernateGpsDevice" class = "org.compass.gps.device.hibernate.dep.Hibernate3GpsDevice" >
- < property name = "name" >
- < value > hibernateDevice </ value >
- </ property >
- < property name = "sessionFactory" ref = "sessionFactory" />
- < property name = "mirrorDataChanges" >
- < value > true </ value >
- </ property >
- </ bean >
- <!-- 同步更新索引 -->
- < bean id = "compassGps" class = "org.compass.gps.impl.SingleCompassGps" init-method = "start" destroy-method = "stop" >
- < property name = "compass" ref = "compass" />
- < property name = "gpsDevices" >
- < list >
- < bean class = "org.compass.spring.device.hibernate.dep.SpringHibernate3GpsDevice" >
- < property name = "name" value = "hibernateDevice" />
- < property name = "sessionFactory" ref = "sessionFactory" />
- </ bean >
- </ list >
- </ property >
- </ bean >
- < bean id = "compassTemplate" class = "org.compass.core.CompassTemplate" >
- < property name = "compass" ref = "compass" />
- </ bean >
- </ beans >
接着是實體的compass註解,將需要創建索引的實體及實體屬性標註:
- @Entity
- @Table (name= "B_ENTITY_BASE" )
- @Searchable
- public class MobileBaseEntity implements Serializable {
- private static final long serialVersionUID = -5594658438463757978L;
- @SearchableId
- protected String id;
- @SearchableProperty (name = "keys" )
- protected String name;
- @SearchableProperty (index = Index.NO, store = Store.YES)
- protected String address;
- protected String phone;
- protected String description;
- @SearchableProperty (index = Index.NO, store = Store.YES)
- protected Double longitude;
- @SearchableProperty (index = Index.NO, store = Store.YES)
- protected Double latitude;
- @SearchableProperty (index = Index.NO, store = Store.YES)
- protected String type;
- protected String channel;
- protected String knowledgeId;
- protected String stauts; //0審覈退回,1審覈中,2正常
- }
@SearchableId:不要求定義搜索的元數據;
@SearchableProperty(name = "keys"):屬性索引的別名,這個別名在做查詢的時候會用到,我們可以把多個實體類的多個屬性定義爲同一個別名來做全文檢索;
@SearchableProperty(index = Index.NO, store = Store.YES):保存這個屬性的值,但不作爲索引。index = Index.XXX,這個有幾種策略,1: NOT_ANALYZED ,不分詞但創建索引;2:ANALYZED,分詞並創建索引。
@SearchableComponent:關聯複合索引,用以複合類型;
@ SearchableReference:用以引用類型。
接下來就是使用compass的API來做數據查詢,首先是模糊匹配查詢,
- List<BusLine> busLineList = new ArrayList<BusLine>();
- Compass compass = this .compassTemplate.getCompass();
- CompassSession compassSession = compass.openSession();
- CompassQueryBuilder queryBuilder = compassSession.queryBuilder();
- //指定查詢實體
- CompassQuery queryAlias = queryBuilder.alias(BusLine.class .getSimpleName());
- CompassBooleanQueryBuilder boolQueryBuilder = queryBuilder.bool();
- boolQueryBuilder.addMust(queryAlias);
- boolQueryBuilder.addMust(queryBuilder.wildcard("baseEntityKeys" , "*" +value+ "*" ));
- CompassHits compassHits = boolQueryBuilder.toQuery().hits();
- if (compassHits != null && compassHits.length() > 0 ) {
- for ( int i = 0 ; i < compassHits.length(); i++) {
- BusLine line = (BusLine)compassHits.data(i);
- busLineList.add(line);
- }
- }
- compassSession.close();
- return busLineList;
至於是使用模糊匹配查詢還是全文檢索,這需要根據應用場景來選擇,能使用模糊匹配的話儘量使用,全文檢索的代價比模糊匹配要高。全文檢索的例子:
- String keywords = keyword.trim();
- List<KnowledgeInfo> result = new ArrayList<KnowledgeInfo>();
- Properties prop = PropertyUtil.readPropertyFile();
- String type = prop.getProperty("knowledge.zc.type" );
- ChannelInfo channel = channelInfoService.getChannelById(type);
- Compass compass = this .compassTemplate.getCompass();
- CompassSession session = compass.openSession();
- CompassQueryBuilder builder = session.queryBuilder();
- CompassBooleanQueryBuilder boolBuidler = builder.bool();
- boolBuidler.addMust(builder.wildcard("sort" , channel.getSort()+ "*" ));
- if (keywords != null && ! "" .equals(keywords)) {
- String[] array = keywords.split(" " );
- if (array != null && array.length > 0 ) {
- for (String value : array) {
- if (value != null && StringUtils.isNotEmpty(value.trim())) {
- boolBuidler.addMust(builder.queryString("keys:" + value).toQuery());
- }
- }
- }
- CompassHits hits = boolBuidler.toQuery().hits();
- if (hits != null && hits.length() > 0 ) {
- for ( int i = 0 ; i < hits.length(); i++) {
- KnowledgeInfo info = (KnowledgeInfo) hits.data(i);
- String ht = hits.highlighter(i).fragment("keys" );
- if ( null != ht) {
- info.setKeywords(ht);
- }
- result.add(info);
- }
- }
- }
- session.close();
- return result;
compass一個簡單的應用就完成了。