我想根据公司和用户的名字和姓氏过滤用户。
这是我的代码 -
@Entity
@Table(name = "user")
@Indexed
@FullTextFilterDefs({ @FullTextFilterDef(name = "UserFilterByCompanyId", impl = UserFilterFactoryByCompany.class) })
public class User implements Principal {
@Id
@GeneratedValue
@Column(name = "id")
private long id;
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
@Column(name = "first_name")
private String firstName;
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
@Column(name = "last_name")
private String lastName;
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
@Column(name = "mobile")
private String mobile;
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
@Column(name = "type")
private String type;
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
@Column(name = "email", unique = true)
private String email;
@Column(name = "password")
private String password;
@Transient
@Column(name = "project_ids")
private List<Long> projectIds;
@Transient
@Column(name = "company_name")
private String companyName;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "company_id")
private Company company;
@Column(name = "company_id", insertable = false, updatable = false)
private Long companyId;
public User() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<Long> getProjectIds() {
return projectIds;
}
public void setProjectIds(List<Long> projectIds) {
this.projectIds = projectIds;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
public Long getCompanyId() {
return companyId;
}
public void setCompanyId(Long companyId) {
this.companyId = companyId;
}
}
FulltextSearch 实施类
public PageResult search(int start, int size, String content, long companyId) {
EntityManager entityManager = Persistence.createEntityManagerFactory("HibernatePersistence")
.createEntityManager();
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
try {
try {
fullTextEntityManager.createIndexer().startAndWait();
} catch (InterruptedException e) {
e.printStackTrace();
}
QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(User.class).get();
org.apache.lucene.search.Query query = qb.keyword().onFields("firstName", "lastName", "mobile", "email")
.matching(content).createQuery();
javax.persistence.Query fullTextQuery = fullTextEntityManager.createFullTextQuery(query, User.class);
((FullTextQuery) fullTextQuery).enableFullTextFilter("UserFilterByCompanyId").setParameter("companyId",
companyId);
fullTextQuery.setFirstResult(start);
fullTextQuery.setMaxResults(size);
int count = fullTextQuery.getResultList().size();
List<User> userResult = fullTextQuery.getResultList();
if (userResult.size() != 0) {
PageResult pageResults = new PageResult();
pageResults.setData(userResult);
pageResults.setTotalResult(count);
return pageResults;
} else {
logger.error("does not found any matched record with content:-" + content);
throw new EntityNotFoundException("No record matching with " + content);
}
} catch (HibernateException exception) {
logger.error("abnormal ternination, search() of user");
throw new HibernateException("unable to process your request");
} finally {
if (fullTextEntityManager != null) {
fullTextEntityManager.close();
logger.info("session closed successfully");
}
fullTextEntityManager = null;
}
}
最后这里是过滤逻辑
public class UserFilterFactoryByCompany {
private long companyId;
public void setCompanyId(long companyId) {
this.companyId = companyId;
}
public long getCompanyId() {
return companyId;
}
@Key
public FilterKey getKey() {
StandardFilterKey key = new StandardFilterKey();
key.addParameter(companyId);
return key;
}
@Factory
public Filter getFilter() {
Query query = (Query) new TermQuery(new Term("company_id", companyId+"".toString()));
return new CachingWrapperFilter(new QueryWrapperFilter((org.apache.lucene.search.Query) query));
}
}
一切正常但我没有得到任何结果(意味着在结果中得到null)。谁能帮助我。