Compass介绍:
Compass是一个Java搜索框架。它封装了Lucene,增加了一些Lucene不支持的特性(例如实时更新索引),支持各种数据(Java对象、xml、json)到索引的映射,支持各种数据源(JDBC, Hibernate, iBatis)。
Compass所依赖的jar包:
pom.xml代码
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yangchao</groupId>
<artifactId>compass</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>compass Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.yangchao</groupId>
<artifactId>lucene-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- 表示开发的时候引入,发布的时候不会加载此包 -->
<scope>test</scope>
</dependency>
<!-- 导入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<!-- 日志文件管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!--lucene包 -->
<!-- https://mvnrepository.com/artifact/org.compass-project/compass -->
<dependency>
<groupId>org.compass-project</groupId>
<artifactId>compass</artifactId>
<version>2.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-analyzers -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers</artifactId>
<version>2.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>2.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-highlighter -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>2.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-queries -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queries</artifactId>
<version>2.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-snowball -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-snowball</artifactId>
<version>2.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-spellchecker -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-spellchecker</artifactId>
<version>2.4.1</version>
</dependency>
<!--jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>compass</finalName>
<plugins>
<!-- tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<path>/</path>
<port>80</port>
</configuration>
<version>2.2</version>
</plugin>
</plugins>
</build>
</project>
compass.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="./testIndex/" />
</connection>
<!-- 映射信息 -->
<mappings>
<class name="compass.Article" />
</mappings>
</compass>
</compass-core-config>
测试代码:
/**
* @项目名称:compass
* @类名称:TestCompass
* @类描述:
* @创建人:YangChao
* @创建时间:2016年9月1日 下午5:40:44
* @version 1.0.0
*/
public class TestCompass {
@Test
public void createIndex() throws Exception {
// 虚拟一条文章数据
Article article = new Article();
article.setId(1);
article.setTitle("全文检索");
article.setContent("我做了一个全文检索,及对系统进行系统内的资源进行检索");
CompassSession session = CompassUtils.getCompassSessionFactory().openSession();
CompassTransaction tx = session.beginTransaction();
session.create(article);
tx.commit();
session.close();
}
@Test
public void search() throws Exception {
String queryString = "检索";
CompassSession session = CompassUtils.getCompassSessionFactory().openSession();
CompassHits hits = session.find(queryString);
for (int i = 0; i < hits.length(); i++) {
Article article = (Article) hits.data(i);
System.out.println("第" + i + "个: " + article.toString());
}
session.close();
}
}