Spring集成Copass问题,高手请进!查询索引的时候查询的结果为0,包导入正确,xml配置Copy的,应该没有问题!
我参照传智播客视频配置的,配置Copass的Xml代码是直接复制的,配置中指定创建的索引文件夹也创建了。但查询的时候查询不到关键字
我Department.java配置
@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; } }
@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; }}
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; }}
@org.junit.Test public void testAdd() { for (int i = 0; i < 10; i++) { Department department = new Department(); department.setName("国防部"+i); department.setContent("国防部"+i); departmentService.save(department); } } @org.junit.Test public void testquery() { QueryResult<Department> queryResult = departmentService.searchDeparment("国防部", 0, 10); for (Department department :queryResult.getResultlist()) { System.out.println("部门名字:"+department.getName()); } System.out.println("查询命中数:"+queryResult.getTotalrecord()); System.out.println("++++++++++++++++++++++++"); queryResult = search("国防部", 0, 10); System.out.println("查询命中数:"+queryResult.getTotalrecord()); }