Search code examples
javahibernatesortinglucenehibernate-search

Can you sort and search on same field with Hibernate/Lucene?


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;

And a snippet from the search function

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();

Solution

  • 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.