compass简单实例_Product 搜索实体类

Product 搜索实体类 可以对应数据库里的表

 

 

package com.ym.compass.bean;

 

import org.compass.annotations.Index;

import org.compass.annotations.Searchable;

import org.compass.annotations.SearchableId;

import org.compass.annotations.SearchableProperty;

import org.compass.annotations.Store;

 

//标识为搜索实体

@Searchable

public class Product {

 

private String id;

private String name;

private int price;

private String saler;

private String descripiton;

 

public Product(){

 

}

 

public Product(String id){

this.id=id;

}

 

public Product(String id, String name, int price, String saler,

String descripiton) {

this.id = id;

this.name = name;

this.price = price;

this.saler = saler;

this.descripiton = descripiton;

}

 

 

@SearchableId //compass 要求每个搜索实体类都要具有一个标识属性,这点和Hibernate很相似

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

 

/**

* Index.UN_TOKENIZED 不对该Field进行分词 但是建立索引 该属性已过时,建议采用NOT_ANALYZED

* Index.TOKENIZED 先分词后索引 该属性已过时,建议采用ANALYZED

* Index.NOT_ANALYZED 不分词但建立索引

* Index.ANALYZED 分词并且建立索引

*Index.NO 不分词不建立索引 没有索引就不会查询这个字段

* @return

*/

 

//设置收索属性 分词并建立索引

//Store默认值为YES boost的默认值为1用于设置属性在索引中的重要性

@SearchableProperty (index= Index.ANALYZED,store=Store.YES,boost=1)

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@SearchableProperty

public int getPrice() {

return price;

}

public void setPrice(int price) {

this.price = price;

}

@SearchableProperty

public String getSaler() {

return saler;

}

public void setSaler(String saler) {

this.saler = saler;

}

 

@SearchableProperty (index=Index.ANALYZED,store=Store.YES)

public String getDescripiton() {

return descripiton;

}

public void setDescripiton(String descripiton) {

this.descripiton = descripiton;

}

}

 
******************************************************
ProductSearchBean 真正实现Compass搜索的类
 
package com.ym.compass.service.impl;
 
import java.util.ArrayList;
import java.util.List;
 
import org.compass.annotations.config.CompassAnnotationsConfiguration;
import org.compass.core.Compass;
import org.compass.core.CompassException;
import org.compass.core.CompassHits;
import org.compass.core.CompassQuery.SortDirection;
import org.compass.core.CompassQuery.SortPropertyType;
import org.compass.core.CompassQueryBuilder;
import org.compass.core.CompassSession;
import org.compass.core.CompassTransaction;
import org.compass.core.config.CompassEnvironment;
 
import com.ym.compass.bean.Product;
import com.ym.compass.bean.QueryResult;
import com.ym.compass.service.ProductSearch;
 
public class ProductSearchBean implements ProductSearch {
 
private Compass compass = null;
 
public ProductSearchBean() {
try {
// 编程式配置 也可以使用配置文件
compass = new CompassAnnotationsConfiguration()
.setSetting(CompassEnvironment.CONNECTION,
"file://indexfile")
// .setSetting(CompassEnvironment.CONNECTION, "ram://index")
// //在内存中建立索引
.setSetting("compass.engine.analyzer.default.type",
"net.paoding.analysis.analyzer.PaodingAnalyzer")
//进行加亮处理 如把关键字变成红色
.setSetting(
"compass.engine.highlighter.default.formatter.simp.pre",
"<font color='red'>")
.setSetting(
"compass.engine.highlighter.default.formatter.simp.post",
"</font>")
.addScan("com.ym.compass.bean").buildCompass();//扫描包下面的实体类 如果发现@Searchable就会当作搜索实体类
} catch (CompassException e) {
e.printStackTrace();
}
}
 
public QueryResult<Product> search(String keyWord, int firstIndex,
int maxResult) {
QueryResult<Product> qr = new QueryResult<Product>();
CompassSession session = null;
CompassTransaction tx = null;
try {
session = compass.openSession();
tx = session.beginTransaction();
CompassHits hits = session.find(keyWord);
List<Product> products = new ArrayList<Product>();
int length = firstIndex + maxResult;
if (length > hits.length()) {
length = hits.length();
}
for (int i = firstIndex; i < length; i++) {
Product product = (Product) hits.data(i);
product.setDescripiton(hits.highlighter(i).fragment(
"descripiton"));
products.add(product);
}
qr.setResults(products);
qr.setTotal(hits.length());
hits.close();
} catch (CompassException e) {
e.printStackTrace();
tx.rollback();
} finally {
if (session != null && !session.isClosed())
session.close();
}
return qr;
}
 
/**
* 高级查询 比如搜索 销售员为“yang”的商品里面包含关键字的商品
*/
public QueryResult<Product> search(String keyWord, String saler,
int firstIndex, int maxResult) {
QueryResult<Product> qr = new QueryResult<Product>();
CompassSession session = null;
CompassTransaction tx = null;
try {
session = compass.openSession();
tx = session.beginTransaction();
// 查询指定类型的匹配记录,并按照什么排序
CompassQueryBuilder queryBuilder = session.queryBuilder();
CompassHits hits = queryBuilder.bool()
.addMust(queryBuilder.spanEq("saler", saler))
.addMust(queryBuilder.queryString(keyWord).toQuery())
.toQuery().addSort("price", SortPropertyType.INT, SortDirection.REVERSE)
.hits(); // 等同于效果 sql where saler=yang and (xxx like ?) order by price desc
 
List<Product> products = new ArrayList<Product>();
int length = firstIndex + maxResult;
if (length > hits.length()) {
length = hits.length();
}
for (int i = firstIndex; i < length; i++) {
Product product = (Product) hits.data(i);
//把描述中 有关键字的部分 特殊处理 变成红色
product.setDescripiton(hits.highlighter(i).fragment(
"descripiton"));
product.setName(hits.highlighter(i).fragment(
"name"));
products.add(product);
}
qr.setResults(products);
qr.setTotal(hits.length());
hits.close();
 
} catch (CompassException e) {
e.printStackTrace();
tx.rollback();
} finally {
if (session != null && !session.isClosed())
session.close();
}
return qr;
}
 
 
public void destroy() {
compass.close();
 
}
 
public void buildIndex() {
CompassSession session = null;
CompassTransaction tx = null;
try {
session = compass.openSession();
tx = session.beginTransaction();
Product p1 = new Product("1", "红豆经典衬衫", 150, "汤姆", "经典舒适衬衫 大品牌你懂的");
session.create(p1);
Product p2 = new Product("5", "红豆内衣", 50, "杰克", "经典舒适衬衫 大品牌你懂的");
session.create(p2);
Product p3 = new Product("8", "联想笔记本", 150, "汤姆", "经典商务本,不要错过哦");
session.create(p3);
Product p4 = new Product("11", "文具笔记本", 10, "琼斯", "书写流畅");
session.create(p4);
Product p5 = new Product("17", "红豆", 8, "汤姆", "红豆冰淇淋很好吃的哦");
session.create(p5);
tx.commit();
} catch (CompassException e) {
e.printStackTrace();
tx.rollback();
} finally {
if (session != null && !session.isClosed())
session.close();
}
 
}
 
public void deleteIndex(Product product) {
CompassSession session = null;
CompassTransaction tx = null;
try {
session = compass.openSession();
tx = session.beginTransaction();
session.delete(product);
tx.commit();
} catch (CompassException e) {
e.printStackTrace();
tx.rollback();
} finally {
if (session != null && !session.isClosed())
session.close();
}
}
 
/**
* 更新 先删除再新增
*/
public void updateIndex(Product product) {
CompassSession session = null;
CompassTransaction tx = null;
try {
session = compass.openSession();
tx = session.beginTransaction();
session.delete(product);
session.save(product);
} catch (CompassException e) {
e.printStackTrace();
tx.rollback();
} finally {
if (session != null && !session.isClosed())
session.close();
}
}
}
 
 
详细代码见附件,是在eclipse下一个可以通过Junit运行的工程。 解压密码:123
 
dic目录下是庖丁分词器的字典,paoding-dic-home.properties文件指定字典的存放位置
下载地址:http://www.iteye.com/topics/download/eb86610a-a80c-30cb-afa7-ce94276fe750
posted @ 2013-05-01 16:12 眉间尺之魂 阅读(...) 评论(...) 编辑 收藏