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();
}
}
}