日期:2014-05-19 浏览次数:20593 次
@Entity @Searchable public class Department implements Serializable { /** * */ private static final long serialVersionUID = 1L; private int id; private String name; private String content; private Set<Employee> employee = new HashSet<Employee>(); @Id @GeneratedValue @Column(length=16) @SearchableId public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(length=32 , nullable=false) @SearchableProperty(index=Index.NOT_ANALYZED,store=Store.YES) public String getName() { return name; } public void setName(String name) { this.name = name; } @OneToMany(mappedBy ="department",cascade=CascadeType.ALL,fetch=FetchType.LAZY) public Set<Employee> getEmployee() { return employee; } public void setEmployee(Set<Employee> employee) { this.employee = employee; } @SearchableProperty(index=Index.NOT_ANALYZED,store=Store.YES) public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
@Service public class DepartmentServiceBean extends DaoSupport<Department> implements DepartmentService{ private CompassTemplate compassTemplate = null; @Resource public void setCompass(Compass compass){ this.compassTemplate = new CompassTemplate(compass); } //某个产品类别下的产品 public QueryResult<Department> searchDeparment(String keyword, int firstIndex, int maxResult) { QueryResult<Department> qr = new QueryResult<Department>(); try{ qr = compassTemplate.execute(new QueryCallback(keyword, firstIndex, maxResult)); } catch (CompassException e) { e.printStackTrace(); }finally { } return qr; } }
public class QueryCallback implements CompassCallback<QueryResult<Department>>{ private String keyword; private int firstIndex; private int maxResult; public QueryCallback(String keyword, int firstIndex, int maxResult) { this.keyword = keyword; this.firstIndex = firstIndex; this.maxResult = maxResult; } public QueryResult<Department> doInCompass(CompassSession session) throws CompassException { QueryResult<Department> queryResult = new QueryResult<Department>(); try { CompassHits hits = session.find(keyword); int lenght = firstIndex + maxResult>hits.length()?hits.length():firstIndex + maxResult; queryResult.setTotalrecord(hits.length()); List <Department> departList = new ArrayList<Department>(); for (int i = firstIndex; i < lenght; i++) { Department department = (Department) hits.data(i); System.out.println("department="+department==null); if(hits.highlighter(i).fragment("name")!=null){ department.setName(hits.highlighter(i).fragment("name")); } if(hits.highlighter(i).fragment("content")!=null){ department.setContent(hits.highlighter(i).fragment(&