package com.wuhay.compass.service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.compass.core.Compass;
import org.compass.core.CompassCallbackWithoutResult;
import org.compass.core.CompassDetachedHits;
import org.compass.core.CompassHit;
import org.compass.core.CompassHits;
import org.compass.core.CompassQuery;
import org.compass.core.CompassQueryBuilder;
import org.compass.core.CompassSession;
import org.compass.core.CompassTemplate;
import org.compass.core.CompassTransaction;
import org.compass.core.Resource;
import org.compass.core.CompassQueryBuilder.CompassQueryStringBuilder;
import org.compass.core.support.search.CompassSearchCommand;
import org.compass.core.support.search.CompassSearchResults;
import org.compass.spring.web.mvc.AbstractCompassCommandController;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import com.wuhay.compass.bo.Datacode;
import com.wuhay.compass.bo.DatacodeDAO;
import com.wuhay.compass.service.AdvancedSearchCommand.CompassSort;
public class Search extends DatacodeDAO implements InitializingBean{
private Compass compass;
private CompassTemplate compassTemplate;
// @Override
// protected ModelAndView handle(HttpServletRequest arg0,
// HttpServletResponse arg1, Object arg2, BindException arg3)
// throws Exception {
// // TODO Auto-generated method stub
// return null;
// }
public void insertData(){
Datacode code = new Datacode();
code.setDccode("test");
code.setId(8L);
code.setDctype("aot");
code.setDcname(" 2 compass service java test");
this.save(code); //将数据保存于数据库中
compassTemplate.save(code); //设置数据在COMPASS的索引
}
public Datacode getDataCode(){
Datacode code=(Datacode)this.findAll().get(0);
return code;
}
public void searchForString(final String word){
long start=System.currentTimeMillis();
getCompassTemplate().execute(CompassTransaction.TransactionIsolation.READ_ONLY_READ_COMMITTED,
new CompassCallbackWithoutResult(){
public void doInCompassWithoutResult(CompassSession session){
CompassHits hits = session.find(word);
System.out.println("Found [" + hits.getLength() + "] hits for [" + word + "] query");
System.out.println("======================================================");
for (int i = 0; i < hits.getLength(); i++){
print(hits,i);
}
CompassQueryBuilder queryBuilder = session.queryBuilder();
hits = session.queryBuilder().bool()
.addMust(queryBuilder.term(" name ", " jack "))
.addMustNot(queryBuilder.term(" familyName ", " london "))
.addShould(queryBuilder.term(" ", " ")).toQuery().hits();
hits.close();
}
});
System.out.println("time span "+(System.currentTimeMillis()-start));
}
public void search(final String word){
Compass compass = compassTemplate.getCompass();
CompassSession session = compass.openSession();
CompassTransaction tx = null;
try {
tx = session.beginTransaction();
CompassSearchCommand command = new CompassSearchCommand();
command.setQuery(word);
CompassSearchResults results=performSearch(command,session);
System.out.println(results.getPages().length);
System.out.println(results.getHits().length);
for (int i = 0; i < results.getHits().length; i++) {
CompassHit hits=results.getHits()[i];
Object hit=hits.getData();
if (hit instanceof Datacode) {
Datacode item = (Datacode) hit;
System.out.println(item.getDcname());
}
}
tx.commit();
} catch (Exception e) {
if (tx != null) {
e.printStackTrace();
tx.rollback();
}
} finally {
session.close();
}
}
protected CompassSearchResults performSearch(CompassSearchCommand searchCommand, CompassSession session) {
long time = System.currentTimeMillis();
// CompassQueryBuilder queryBuilder = session.queryBuilder();
// CompassQueryStringBuilder queryString = queryBuilder.queryString(searchCommand.getQuery().trim());
// CompassQuery query = queryString.toQuery();
CompassQuery query = buildQuery(searchCommand,session);
CompassHits hits = query.hits();
CompassDetachedHits detachedHits;
Integer pageSize=3; //每页显示记录数
CompassSearchResults.Page[] pages = null;
if (pageSize == null) { //每页显示的记录数为空时,将全部记录显示出来
doProcessBeforeDetach(searchCommand, session, hits, -1, -1);
detachedHits = hits.detach();
} else {
int iPageSize = pageSize;
int page = 0; //显示第几页,从0开始为每一页
int hitsLength = hits.getLength();
if (searchCommand.getPage() != null) { //设置获取的页,从第一次获取时设置好的方式取得页数
page = searchCommand.getPage();
}
int from = page * iPageSize; //获取的起始记录号
//
if (from > hits.getLength()) {
//
// // 如果起始的条目大于搜索到的条目
from = hits.getLength() - iPageSize;
doProcessBeforeDetach(searchCommand, session, hits, from, hitsLength);
detachedHits = hits.detach(from, hitsLength);
} else if ((from + iPageSize) > hitsLength) {
//
// // 结束的条目大于搜索到的结果
doProcessBeforeDetach(searchCommand, session, hits, from, hitsLength);
detachedHits = hits.detach(from, hitsLength);
} else {
//
// // 中间的页码,直接取出相应的条目
doProcessBeforeDetach(searchCommand, session, hits, from, iPageSize); //高亮显示
detachedHits = hits.detach(from, iPageSize); //取出从FROM 起的IPAGESIZE条记录
}
// doProcessAfterDetach(searchCommand, session, detachedHits);
int numberOfPages = (int) Math.ceil((float) hitsLength / iPageSize);
pages = new CompassSearchResults.Page[numberOfPages]; //设置每页的获取记录的方式
for (int i = 0; i < pages.length; i++) {
System.out.println(((Datacode)hits.data(i)).getDcname());
pages[i] = new CompassSearchResults.Page();
pages[i].setFrom(i * iPageSize + 1);
pages[i].setSize(iPageSize);
pages[i].setTo((i + 1) * iPageSize);
if (from >= (pages[i].getFrom() - 1) && from < pages[i].getTo()) {
pages[i].setSelected(true);
} else {
pages[i].setSelected(false);
}
}
if (numberOfPages > 0) {
CompassSearchResults.Page lastPage = pages[numberOfPages - 1];
if (lastPage.getTo() > hitsLength) {
lastPage.setSize(hitsLength - lastPage.getFrom());
lastPage.setTo(hitsLength);
}
}
}
time = System.currentTimeMillis() - time;
CompassSearchResults searchResults = new CompassSearchResults(detachedHits.getHits(), time, pageSize);
searchResults.setPages(pages);
return searchResults;
}
/**
* 构建Lucene搜索器.
*/
protected CompassQuery buildQuery(CompassSearchCommand searchCommand, CompassSession session) {
CompassQuery query = session.que