您可以使用Hibernate/Lucene在同一字段上进行排序和搜索吗? [英] Can you sort and search on same field with Hibernate/Lucene?
问题描述
我有以下带注释的类,我正在尝试对lucene/休眠搜索查询的结果进行排序.我终于使查询正常工作,但是似乎当我实现必要的注释(在jobStatus上看到)以对该列进行排序时,便无法再搜索该列.我将根据我发现的说明进行操作
I have the following annotated class that I am trying to sort the results from a lucene/hibernate search query. I finally have the query working properly but it seems that when I implement the necessary annotations (seen on jobStatus) to sort that column, it makes it impossible to then search that column. I am basing this off the instructions I found here on google. I have been having issues figuring this whole hibernate search and sort thing out, now that I finally figured out how to sort and search all I need is to be able to do them together.
@Entity
@Table(name="jobReq")
@Indexed
public class JobReq {
@Id
@DocumentId
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Field(index = Index.YES)
@Column(name="jobId", nullable=false, unique=true)
private String jobId;
@Field(index = Index.YES)
@Column(name="jobTitle", nullable=false)
private String jobTitle;
@Field(index = Index.YES)
@Column(name="jobContract", nullable=false)
private String contract;
@Field(index = Index.YES)
@Column(name="jobProject", nullable=true)
private String project;
@Field(index = Index.YES)
@Column(name="jobLaborCategory", nullable=false)
private String laborCategory;
@Field(index = Index.YES)
@Column(name="jobSummary", nullable=false)
private String summary;
@Field(index = Index.YES)
@Column(name="jobDescription", nullable=false)
private String jobDescription;
@Fields({@Field, @Field(analyze = Analyze.NO, name = "jobStatus")})
@Column(name="jobStatus", nullable=false)
private String status;
@Field(index = Index.YES)
@Column(name="TTONumber", nullable=false)
private String TTONumber;
@Field(index = Index.YES)
@Column(name="jobPostedDate", nullable=false)
@Type(type="date")
private Date postedDate;
还有搜索功能的摘录
Field[] allFields = this.type.getDeclaredFields();
SortField field =new SortField(sortColumn, SortField.STRING, reverseSort);
Sort sort = new Sort(field);
hibQuery = fullTextSession.createFullTextQuery(bq, this.type).setSort(sort);
results = hibQuery.list();
推荐答案
事实证明,您无法在同一字段上进行排序和搜索,因为这本冬眠书中的文章有点误导人.因此,对于修复程序,我在休眠论坛上找到了解决方案因为您创建了一个影子"列,该列是重复的,所以对其中一个进行注释以进行搜索,而对另一个进行注释以进行排序.
Turns out you cannot sort and search on the same field, that article from the hibernate book was a tad misleading. So as for a fix I found a solution on the hibernate forums in that you create a 'shadow' column that is a duplicate, one is annotated for searching while the other is annotated for sorting.
花了我一段时间才找到这种解决方案,主要是因为答案似乎有点骇人听闻",尽管相当简单直接,重复数据在我的培训中一直是不行的.但是话又说回来,我想你每天都会学到一些新东西.
It took me a while to find this solution mostly because the answer seems a bit 'hack-y' though fairly straight forward and simple, duplicating data has always been a no-no in my training. But then again I guess you learn something new everyday.
这篇关于您可以使用Hibernate/Lucene在同一字段上进行排序和搜索吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!