`

compass使用的步骤

阅读更多
打入lucene,paoding分词器,compass,common-log.jar相关的jar包,
可以使用配置或者注释的方式来使用compass.

使用xml配置方式:
01.<?xml version="1.0" encoding="utf-8"?> 
02.<compass-core-config xmlns="http://www.compass-project.org/schema/core-config" 
03.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
04.    xsi:schemaLocation="http://www.compass-project.org/schema/core-config 
05.    http://www.compass-project.org/schema/compass-core-config-2.2.xsd"> 
06. 
07.    <compass name="default"> 
08. 
09.        <!-- 配置索引库的存储目录 --> 
10.        <connection> 
11.            <file path="e:/liuyan_index" /> 
12.        </connection> 
13.         
14.        <cache> 
15.            <firstLevel type="org.compass.core.cache.first.NullFirstLevelCache" /> 
16.        </cache> 
17.         
18.        <mappings> 
19.            <class name="com.sharp.liuyan.so.ArticleSo" /> 
20.        </mappings> 
21.         
22.        <settings> 
23.            <!-- setting元素就像于property元素 --> 
24.            <setting name="compass.engine.analyzer.default.type"  
25.                value="net.paoding.analysis.analyzer.PaodingAnalyzer"/>   
26.            <!-- 配置高亮 --> 
27.            <setting name="compass.engine.highlighter.default.formatter.simple.pre"  
28.                value="<font color='red'><b>"/> 
29.            <setting name="compass.engine.highlighter.default.formatter.simple.post" 
30.                value="</b></font>"/> 
31.                                    
32.        </settings> 
33.         
34.    </compass> 
35. 

使用注释方式:
Compass compass=new CompassAnnotationsConfiguration()
.setSetting("compass.engine.analyzer.default.type","net.paoding.analysis.analyzer.PaodingAnalyzer")
.setSetting(CompassEnvironment.CONNECTION,"c:/lucene")
.setSetting("compass.engine.highlighter.default.formatter.simple.pre", "<span style='color:red;'>")
.setSetting("compass.engine.highlighter.default.formatter.simple.post", "</span>")
.addScan("com.domain").buildCompass();
Message message=new Message();
message.setId(1L);
message.setTitle("当你孤单你会想起谁");
message.setContent("当你孤单你会想起谁,你想不想找个人来陪");
message.setCreateTime(new Date());

CompassSession session=compass.openSession();
session.create(message);
session.commit();



相关Message的注解:
@Searchable
public class Message {
@SearchableId
private Long id;
@SearchableProperty(index=Index.ANALYZED,store=Store.YES)
private String title;
@SearchableProperty(index=Index.ANALYZED,store=Store.YES)
private String content;
@SearchableProperty(index=Index.NO,store=Store.YES)
private Date createTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}

}


@Test
public void test3(){
Compass compass=new CompassConfiguration().configure().buildCompass();
CompassSession session=compass.openSession();
CompassHits hits=session.find("title:孤单");
for(int i=0;i<hits.getLength();i++){
Message message=(Message)hits.data(i);
message.setTitle(hits.highlighter(i).fragment("title"));
message.setContent(hits.highlighter(i).fragment("content"));
System.out.println(message.getId()+"--"+message.getTitle()+"--"+message.getContent()+"--"+message.getCreateTime().toString());
}
}


3:与spring整合时的配置

1.<!-- 使用annotation配置,指定要转换的POJO。PO类在compass的classMappings值指定 --> 
2.    <bean id="annotationConfiguration" 
3.        class="org.compass.annotations.config.CompassAnnotationsConfiguration"> 
4.    </bean> 
5.    
6.   <bean id="compass" class="org.compass.spring.LocalCompassBean"> 
7.        <!-- OSEM映射的对象PO类 --> 
8.        <property name="classMappings"> 
9.            <list> 
10.               <value>com.core.persistence.po.Article</value> 
11.               <value>com.core.persistence.po.Author</value> 
12.            </list> 
13.        </property> 
14.        <!-- 使用注解配置 --> 
15.        <property name="compassConfiguration" ref="annotationConfiguration"/> 
16.        <property name="compassSettings"> 
17.            <props> 
18.                <!-- 索引文件在服务器上的存储路径 如:file://d:/index --> 
19.                <prop key="compass.engine.connection">/lucene/indexes</prop> 
20.                <!-- 在内存中建立索引 
21.               <prop key="compass.engine.connection">ram://index</prop> 
22.               --> 
23.                <prop key="compass.transaction.factory"> 
24.                    org.compass.spring.transaction.SpringSyncTransactionFactory 
25.                 </prop> 
26.                 <!-- 配置高亮为红色 --> 
27.                <prop key="compass.engine.highlighter.default.formatter.simple.pre"> 
28.                    <![CDATA[<font color="red"><b>]]> 
29.                </prop> 
30.                <prop 
31.                    key="compass.engine.highlighter.default.formatter.simple.post"> 
32.                    <![CDATA[</b></font>]]> 
33.                </prop> 
34.            </props> 
35.        </property> 
36.  
37.        <property name="transactionManager" ref="transactionManager"/> 
38.    </bean> 
39.    
40.    <bean id="compassTemplate" class="org.compass.core.CompassTemplate"> 
41.        <property name="compass" ref="compass"/> 
42.    </bean> 
43.    
44.    <!-- 与hibernate的绑定,经Hiberante的数据改变会自动被反射到索引里面(增加、修改、删除操作). --> 
45.   <bean id="hibernateGpsDevice" 
46.        class="org.compass.gps.device.hibernate.HibernateGpsDevice"> 
47.        <property name="name"> 
48.            <value>hibernateDevice</value> 
49.        </property> 
50.        <property name="sessionFactory" ref="sessionFactory"/> 
51.        <property name="mirrorDataChanges"> 
52.            <value>true</value> 
53.        </property> 
54.    </bean> 
55.    
56.    <bean id="compassGps" class="org.compass.gps.impl.SingleCompassGps" 
57.        init-method="start" destroy-method="stop"> 
58.        <property name="compass" ref="compass"/> 
59.        <property name="gpsDevices"> 
60.            <list> 
61.              <ref local="hibernateGpsDevice"/>   
62.            </list> 
63.        </property> 
64.    </bean> 
65.  
66.    <!-- 定时重建索引(利用quartz)或随Spring ApplicationContext启动而重建索引 --> 
67.    <bean id="compassIndexBuilder" class="com.lucene.service.CompassIndexBuilder" lazy-init="false"> 
68.        <property name="compassGps" ref="compassGps"/> 
69.        <property name="buildIndex" value="true"/> 
70.        <property name="lazyTime" value="10"/> 
71.    </bean> 
72.  
73. 
74.  
75.public class CompassIndexBuilder implements InitializingBean { 
76.  
77.    private static final Logger log = Logger.getLogger(CompassIndexBuilder.class); 
78.  
79.    // 是否需要建立索引,可被设置为false使本Builder失效. 
80.    private boolean buildIndex = true; 
81.  
82.    // 索引操作线程延时启动的时间,单位为秒 
83.    private int lazyTime = 10; 
84.  
85.    // Compass封装 
86.    private CompassGps compassGps; 
87.  
88.    // 索引线程 
89.    private Thread indexThread = new Thread() { 
90.  
91.        @Override 
92.        public void run() { 
93.            try { 
94.                Thread.sleep(lazyTime * 1000); 
95.  
96.                log.info("begin compass index..."); 
97.                long beginTime = System.currentTimeMillis(); 
98.                // 重建索引. 
99.                // 如果compass实体中定义的索引文件已存在,索引过程中会建立临时索引, 
100.                // 索引完成后再进行覆盖. 
101.                compassGps.index(); 
102.                long costTime = System.currentTimeMillis() - beginTime; 
103.                log.info("compss index finished."); 
104.                log.info("costed " + costTime + " milliseconds"); 
105.            } catch (InterruptedException e) { 
106.            } 
107.        } 
108.    }; 
109.  
110.    /**
111.     * 实现<code>InitializingBean</code>接口,在完成注入后调用启动索引线程.
112.     * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
113.     */ 
114.    public void afterPropertiesSet() throws Exception { 
115.        if (buildIndex) { 
116.            indexThread.setDaemon(true); 
117.            indexThread.setName("Compass Indexer"); 
118.            indexThread.start(); 
119.        } 
120.    } 
121.  
122.    public void setBuildIndex(boolean buildIndex) { 
123.        this.buildIndex = buildIndex; 
124.    } 
125.  
126.    public void setLazyTime(int lazyTime) { 
127.        this.lazyTime = lazyTime; 
128.    } 
129.  
130.    public void setCompassGps(CompassGps compassGps) { 
131.        this.compassGps = compassGps; 
132.    } 
133.} 


1.Compass compass = compassTemplate.getCompass(); 
2.CompassSession session=compass.openSession(); 
3.List list = new ArrayList(); 
4.CompassHits hits= session.queryBuilder().queryString(queryString).toQuery().hits(); 
5.for(int i=0;i<hits.length();i++){ 
6.    Person hit=( Person)hits.data(i); 
7.    list.add(hit); 
8.} 
9.session.close(); 
10.return list; 



paoding解牛分词器配置信息:
1:导入jar包
2:配置paoding-dic-home.properties文件,指定词典路径
3:复制词典到src目录下
4:在copass配置文件中修改分词器配置
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics