作者:再见傻瓜傻瓜_299 | 来源:互联网 | 2023-05-19 09:14
Compass是一个强大的,事务的,高性能的对象搜索引擎映射(OSEM:objectsearchenginemapping)与一个Java持久层框架。Compass之于Luc
Compass是一个强大的,事务的,高性能的对象/搜索引擎映射(OSEM:object/search engine mapping)与一个Java持久层框架。
Compass之于Lucene,就像Hibernate之于JDBC,Compass就是把Lucene封装了一层。
Compass目前版本是2.2.0,已经很久没有更新与维护。Compass2.2的版本所对应的Lucene2.4.1。
下载地址:http://www.compass-project.org/
解压后的目录结构:
开发的基本jar包在dist及其子目录下查找,主要如下,
代码示例:
Article.java
package com.my.bean;
import org.compass.annotations.Index;
import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableBoostProperty;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;
import org.compass.annotations.Store;
@Searchable
public class Article {
@SearchableId(name = "id", format = "00000000")
private Integer id;
@SearchableProperty(name = "title", store = Store.YES, index = Index.ANALYZED)
private String title;
@SearchableProperty(name = "content", store = Store.YES, index = Index.ANALYZED)
private String content;
@SearchableBoostProperty
private float boostValue = 1F;
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;
}
public float getBoostValue() {
return boostValue;
}
public void setBoostValue(float boostValue) {
this.boostValue = boostValue;
}
}
HelloWorld.java
package com.my.compass
import java.util.ArrayList
import java.util.List
import org.compass.core.Compass
import org.compass.core.CompassHits
import org.compass.core.CompassSession
import org.compass.core.CompassTransaction
import org.compass.core.config.CompassConfiguration
import org.junit.Test
import com.my.bean.Article
public class HelloWorld {
private CompassConfiguration cfg = new CompassConfiguration().configure()
private Compass compassSessiOnFactory= cfg.buildCompass()
// 建立索引(模拟在贴吧中发表了一个文章,会保存到数据库中,并且应该建立索引,以便能搜索到)
@Test
public void createIndex() throws Exception {
// 模拟一条刚保存到数据库中的数据
Article article = new Article()
article.setId(1)
article.setTitle("Lucene是全文检索的框架")
article.setContent("如果信息检索系统在用户发出了检索请求后再去互联网上找答案,根本无法在有限的时间内返回结果。")
// 建立索引 ?
CompassSession session = compassSessionFactory.openSession()
CompassTransaction tx = session.beginTransaction()
session.create(article)
tx.commit()
session.close()
}
// 搜索
@Test
public void search() throws Exception {
// 搜索条件
// String queryString = "lucene"
String queryString = "compass"
// 进行搜索,得到结果 ?
List list = new ArrayList()
CompassSession session = compassSessionFactory.openSession()
CompassTransaction tx = session.beginTransaction()
CompassHits hits = session.find(queryString)
// int count = hits.length()
for (int i = 0
compass.cfg.xml
<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="./indexDir/" />
connection>
<mappings>
<class name="com.my.bean.Article" />
mappings>
<settings>
<setting name="compass.engine.highlighter.default.formatter.simple.pre" value="" />
<setting name="compass.engine.highlighter.default.formatter.simple.post" value="" />
<setting name="compass.engine.highlighter.default.fragmenter.simple.size" value="20" />
<setting name="compass.engine.analyzer.default.type" value="jeasy.analysis.MMAnalyzer" />
settings>
compass>
compass-core-config>
Compass增删改查
CompassUtils.java
package com.my.utils;
import org.compass.core.Compass;
import org.compass.core.CompassSession;
import org.compass.core.config.CompassConfiguration;
public class CompassUtils {
private static Compass compassSessionFactory;
static {
CompassConfiguration cfg = new CompassConfiguration().configure();
compassSessiOnFactory= cfg.buildCompass();
}
/**
* 打开一个新的CompassSession并返回
*
* @return
*/
public static CompassSession openSession() {
return compassSessionFactory.openSession();
}
public static Compass getCompassSessionFactory() {
return compassSessionFactory;
}
}
ArticleIndexDao.java
package com.my.compass;
import java.util.ArrayList;
import java.util.List;
import org.compass.core.CompassHits;
import org.compass.core.CompassSession;
import org.compass.core.CompassTransaction;
import com.my.bean.Article;
import com.my.bean.QueryResult;
import com.my.utils.CompassUtils;
public class ArticleIndexDao {
/**
* 创建索引(保存到索引库)
*
* @param article
*/
public void save(Article article) {
CompassSession session = CompassUtils.openSession();
CompassTransaction tx = null;
try {
tx = session.beginTransaction();
session.create(article);
tx.commit();
} catch (Exception e) {
tx.rollback();
throw new RuntimeException(e);
} finally {
session.close();
}
}
/**
* 删除索引
*
* Term:是指某字段中的某个关键词(在目录中出现的关键词)
*
* @param id
*/
public void delete(Integer id) {
CompassSession session = CompassUtils.openSession();
CompassTransaction tx = null;
try {
tx = session.beginTransaction();
session.delete(Article.class, id);
tx.commit();
} catch (Exception e) {
tx.rollback();
throw new RuntimeException(e);
} finally {
session.close();
}
}
/**
* 更新索引
*
* @param article
*/
public void update(Article article) {
CompassSession session = CompassUtils.openSession();
CompassTransaction tx = null;
try {
tx = session.beginTransaction();
session.save(article);
tx.commit();
} catch (Exception e) {
tx.rollback();
throw new RuntimeException(e);
} finally {
session.close();
}
}
/**
* 搜索(分页)
*
* @param queryString
* @param firstResult
* @param maxResults
* @return 总记录数 + 一页数据列表
*/
public QueryResult search(String queryString, int firstResult, int maxResults) {
CompassSession session = CompassUtils.openSession();
CompassTransaction tx = null;
try {
tx = session.beginTransaction();
CompassHits hits = session.find(queryString);
int count = hits.length();
List list = new ArrayList();
int endIndex = Math.min(firstResult + maxResults, hits.length());
for (int i = firstResult; i
Article article = (Article) hits.data(i);
String text = hits.highlighter(i).fragment(
"content");
if (text !=
null) {
article.setContent(text);
}
list.add(article);
}
tx.commit();
return new QueryResult(count, list);
}
catch (Exception e) {
tx.rollback();
throw new RuntimeException(e);
}
finally {
session.close();
}
}
}
基于CompassTemplate的增删改查
package com.my.compass;
import java.util.ArrayList;
import java.util.List;
import org.compass.core.CompassCallback;
import org.compass.core.CompassException;
import org.compass.core.CompassHits;
import org.compass.core.CompassSession;
import org.compass.core.CompassTemplate;
import com.my.bean.Article;
import com.my.bean.QueryResult;
import com.my.utils.CompassUtils;
public class ArticleIndexDao2 {
/**
* 创建索引(保存到索引库)
*
* @param article
*/
public void save(Article article) {
CompassTemplate compassTemplate = new CompassTemplate(CompassUtils.getCompassSessionFactory());
compassTemplate.create(article);
}
/**
* 删除索引
*
* Term:是指某字段中的某个关键词(在目录中出现的关键词)
*
* @param id
*/
public void delete(Integer id) {
CompassTemplate compassTemplate = new CompassTemplate(CompassUtils.getCompassSessionFactory());
compassTemplate.delete(Article.class, id);
}
/**
* 更新索引
*
* @param article
*/
public void update(Article article) {
CompassTemplate compassTemplate = new CompassTemplate(CompassUtils.getCompassSessionFactory());
compassTemplate.save(article);
}
/**
* 搜索(分页)
*
* @param queryString
* @param firstResult
* @param maxResults
* @return 总记录数 + 一页数据列表
*/
public QueryResult search(final String queryString, final int firstResult, final int maxResults) {
CompassTemplate compassTemplate = new CompassTemplate(CompassUtils.getCompassSessionFactory());
return compassTemplate.execute(new CompassCallback() {
public QueryResult doInCompass(CompassSession session) throws CompassException {
CompassHits hits = session.find(queryString);
int count = hits.length();
List list = new ArrayList();
int endIndex = Math.min(firstResult + maxResults, hits.length());
for (int i = firstResult; i
查询示例:
package com.my.compass
import java.util.ArrayList
import java.util.List
import org.compass.core.CompassHits
import org.compass.core.CompassQuery
import org.compass.core.CompassQueryBuilder.CompassMultiPhraseQueryBuilder
import org.compass.core.CompassSession
import org.compass.core.CompassTransaction
import org.junit.Test
import com.my.bean.Article
import com.my.utils.CompassUtils
public class QueryTest {
@Test
public void search() throws Exception {
CompassSession session = CompassUtils.openSession()
CompassTransaction tx = session.beginTransaction()
// 查询
// ------------------------------------------------
// 1,查询所有
CompassQuery query1 = session.queryBuilder().matchAll()
// 关键词查询
CompassQuery query2 = session.queryBuilder().term("title", "lucene")
// 范围查询
CompassQuery query3 = session.queryBuilder().between("id", 5, 15, true)
// 通配符查询
CompassQuery query4 = session.queryBuilder().wildcard("title", "luc*n?")
// 模糊查询
CompassQuery query5 = session.queryBuilder().fuzzy("title", "lucenx", 0.8F)
// 短语查询
CompassMultiPhraseQueryBuilder multiPhraseQueryBuilder = session.queryBuilder().multiPhrase("title")
multiPhraseQueryBuilder.add("lucene", 0)
multiPhraseQueryBuilder
var cpro_id = "u6885494";