配置compass的步骤

本文详细介绍了如何配置Compass与SO以实现高效全文检索,包括配置Compass核心参数、定义索引库存储目录、设置缓存、映射类及属性,并通过Spring配置文件实现索引化。此外,还提供了示例代码展示如何使用索引进行关键词搜索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.配置compass

           1).用独立的compass.cfg.xml 文件,和hibernate.cfg.xml文件一样。

                  

<?xml version="1.0" encoding="utf-8"?>
<compass-core-config xmlns="http://www.compass-project.org/schema/core-config"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.compass-project.org/schema/core-config
	http://www.compass-project.org/schema/compass-core-config-2.2.xsd">

	<compass name="default">

		<!-- 配置索引库的存储目录 -->
		<connection>
			<file path="e:/liuyan_index" />
		</connection>
		
		<cache>
			<firstLevel type="org.compass.core.cache.first.NullFirstLevelCache" />
		</cache>
		
		<mappings>
			<class name="com.sharp.liuyan.so.ArticleSo" />
		</mappings>
		
		<settings>
			<!-- setting元素就像于property元素 -->
			<setting name="compass.engine.analyzer.default.type" 
				value="net.paoding.analysis.analyzer.PaodingAnalyzer"/>	
			<!-- 配置高亮 -->
			<setting name="compass.engine.highlighter.default.formatter.simple.pre" 
				value="<font color='red'><b>"/>
			<setting name="compass.engine.highlighter.default.formatter.simple.post"
				value="</b></font>"/>
							       
		</settings>
		
	</compass>

</compass-core-config>

             还需在spring applicationcontext.xml配置文件中指定该配置文件

                  

		<bean id="compass" class="org.compass.spring.LocalCompassBean"
			p:configLocation="classpath:compass.cfg.xml"/>



          2), 直接在spring 配置文件里配置,无需compass.cfg.xml中配置

                

		<bean id="compass" class="org.compass.spring.LocalCompassBean">
			<property name="classMappings">
				<list>
					<value>com.sharp.liuyan.so.ArticleSo</value>
				</list>
			</property>
			<property name="compassSettings">
				<props>
					<prop key="compass.engine.analyzer.default.type">net.paoding.analysis.analyzer.PaodingAnalyzer</prop>
					<prop key="compass.engine.connection">file://e:/liuyan_index</prop>     <!-- 在内存中建立索引 <prop key="compass.engine.connection">ram://index</prop> -->
					<prop key="compass.engine.highlighter.default.formatter.simple.pre"><![CDATA[<font color='red'>]]></prop>
					<prop key="compass.engine.highlighter.default.formatter.simple.post"><![CDATA[</font>]]></prop>
					<prop key="compass.transaction.factory">org.compass.spring.transaction.SpringSyncTransactionFactory</prop>
				</props>
			</property>
			<property name="transactionManager" ref="transactionManager" />
		</bean>

2.配置SO

 

/**
 * @Version: 1
 * @JDK: jdk 1.7
 * @Module: liuyan

 * 2012-12-13 - 上午11:32:03 Created by Sharp
 */ 

 /*- 				History
 **********************************************
 *  ID      DATE           PERSON       REASON
 *  1     2012-12-13         Sharp        Created
 **********************************************
 */

package com.sharp.liuyan.so;

import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;

@Searchable
public class ArticleSo {
	@SearchableId
	private Integer id;
	@SearchableProperty
	private String title;
	@SearchableProperty
	private String content;
	public Integer getId() {
		return id;
	}
	public void setId(Integer 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;
	}
	
}

3.使用搜索:

/**
 * @Version: 1
 * @JDK: jdk 1.7
 * @Module: liuyan

 * 2012-12-13 - 下午2:03:20 Created by Sharp
 */ 

 /*- 				History
 **********************************************
 *  ID      DATE           PERSON       REASON
 *  1     2012-12-13         Sharp        Created
 **********************************************
 */

package com.sharp.liuyan.indexdao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.compass.core.CompassHighlighter;
import org.compass.core.CompassHits;
import org.compass.core.CompassSession;
import org.springframework.stereotype.Repository;

import com.sharp.common.indexdao.impl.BaseIndexDaoImpl;
import com.sharp.liuyan.indexdao.IArticleIndexDao;
import com.sharp.liuyan.so.ArticleSo;
@Repository("articleIndexDao")
public class ArticleIndexDaoImpl extends BaseIndexDaoImpl<ArticleSo> implements IArticleIndexDao{

	@Override
	public List<ArticleSo> getArticleByKeyword(String keyword) {
		CompassSession session = getCompass().openSession();
		CompassHits hints = session.find("title:" + keyword + " OR content:" + keyword);
		List<ArticleSo> articlesos = new ArrayList<ArticleSo>();
		for(int i=0,size = hints.length(); i<size; i++){
			ArticleSo articleSo = (ArticleSo) hints.data(i);
			CompassHighlighter hignlighter = hints.highlighter(i);
			String title = hignlighter.fragment("title");
			String content = hignlighter.fragment("content");
			if(!StringUtils.isEmpty(title)){
				articleSo.setTitle(title);
			}
			if(!StringUtils.isEmpty(content)){
				articleSo.setContent(content);
			}
			articlesos.add(articleSo);
		}
		return articlesos;
	}

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值