我参照传智播客视频配置的,配置Copass的Xml代码是直接复制的,配置中指定创建的索引文件夹也创建了。但查询的时候查询不到关键字
我Department.java配置
- Java code
@Entity @Searchablepublic 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; } }
DepartmentServiceBean.java
- Java code
@Servicepublic 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; }}
QueryCallback.java
- Java code
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("content")); } departList.add(department); } queryResult.setResultlist(departList); } catch (Exception e) { e.printStackTrace(); } return queryResult; }}