Can you sort and search in the 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. My query finally works fine for me, but it seems that when I implement the necessary annotations (seen on jobStatus) to sort this column, it makes it impossible to search for this column. I base this on the instructions I found here on Google . I'm having trouble calculating all this hibernate search and sort mode, now that I finally figured out how to sort and search all I need is 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(); 
+4
source share
3 answers

It turns out that you cannot sort and search in the same field, this article from the sleep book was a little misleading. As for the fix, I found a solution on the hibernation forums in which you create a shadow column that is a duplicate, one annotated for search, and the other annotated for sorting.

It took me a while to find this solution, mainly because the answer seemed a bit “hacked,” although rather straightforward and simple, the duplicate data was always no-no in my training. But then again, I think you will learn something new every day.

+3
source

Hibernate search documentation provides a solution to this problem similar to that of Adam.

http://docs.jboss.org/hibernate/search/4.5/reference/en-US/html_single/#d0e3165

Basically use the @Fields annotation to index a field twice once with NO analysis for sorting and once with YES analysis for search.

 @Entity @Indexed(index = "Book") public class Book { @Fields( { @Field, @Field(name = "summary_forSort", analyze = Analyze.NO, store = Store.YES) } ) public String getSummary() { return summary; } ... } 

analysis: determines whether the property (Analyze.YES) is analyzed or not (Analyze.NO). The default value is Analyze.YES. Tip Regardless of whether you want to analyze a property, it depends on whether you want to search for an element as is, or by the words contained in it. It makes sense to parse a text field, but probably not a date field. Tip Fields used for sorting or cutting should not be parsed.

+5
source

Two things:

  • Creating indexes in each column can hurt row-by-row performance because index updates are not free. It may also use unnecessary amount of extra storage space. Of course, if this is not really a bottleneck for you, it does not matter.

  • You can sort with Hibernate Criteria , for example:

     Criteria c = session.createCriteria(MyObject.class).addOrder(Order.desc(sortColumn)); Query q = session.createFullTextQuery(bq).setCriteriaQuery(c); 

    Columns of sort keys do not need to be indexed.

+1
source

Source: https://habr.com/ru/post/1496658/


All Articles