Sort Search results in Hibernate Search 5.5

Source: Internet
Author: User

Sort Search results in Hibernate Search 5.5

"Order, order"-sometimes not only do the distinguished members of the House of Commons need to be shouted for sorting, but also the Hibernate query results need to be sorted in special circumstances.

In this way, special attributes are sorted only before an Sort object is executed in full text query.

FullTextSession session = ...;
QueryParser queryParser = ...;
 
FullTextQuery query = session. createFullTextQuery (queryParser. parse ("summary: lucene"), Book. class );
Sort sort = new Sort (new SortField ("title", SortField. Type. STRING, false ));
Query. setSort (sort );
List <Book> result = query. list ();

Like Lucene 5 (based on Hibernate Search 5.5), if you know the sorting attribute in advance, it can improve the performance. In this example, these sorting attributes are called "text value attributes". These text value attributes have the advantage of fast and low memory consumption compared with the traditional unconverted indexing method.

To achieve that purpose. Hibernate Search provides a new annotation @ SortableField (whose multi-value combination is @ SortableFields) that can mark attributes that can be sorted. The following example shows how it works:

@ Entity
@ Indexed (index = "Book ")
Public class Book {
 
@ Id
Private Integer id;
 
@ Field
@ SortableField
@ DateBridge (resolution = Resolution. DAY)
Private Date publicationDate;
 
@ Fields ({
@ Field,
@ Field (name = "sortTitle", analyze = Analyze. NO, store = Store. NO, index = Index. NO)
})
@ SortableField (forField = "sortTitle ")
Private String title;
 
@ Field
Private String summary;
 
// Constructor, getters, setters...
}

I have used @ SortableField. Next, let's take a look at the @ Field annotation. In this example, a separate field corresponds to an attribute (for example, publicationDate). Using only one special @ SortableField annotation is enough to make this field a sortable field. If multiple existing fields (such as the title attribute) exist, you can use @ SortableField # forField () to implement special field names.

Note that sorting fields cannot be analyzed. In this example, you want to create an index for a specified analysis attribute for search, as long as you add another unanalyzed field to the sorting as the title attribute display. If the field only needs to be sorted, You need to configure it as non-indexed and non-sorted, so you can avoid unnecessary index generation.

Configure the sorting field without changing the query. Set Sort only for the required fields:

FullTextQuery query = ...;
Sort sort = new Sort (
New SortField ("publicationDate", SortField. Type. LONG, false ),
New SortField ("sortTitle", SortField. Type. STRING, false)
);
Query. setSort (sort );

What if you sort a field that you have not explicitly stated the order? For example, migrate a completed application to Hibernate Search 5.5? The good news is that sorting will be set by default using the basic function. When Hibernate Search detects that no sorting field is set, it naturally rolls back to a non-inverted index.

But you need to know that the performance will be much worse (also as memory-intensive for non-reverse memory operations). Maybe this function will be completely removed from the future version of Lucene. Therefore, pay attention to the messages in your log file, as shown below. Follow the suggestions to declare undeclared sorting fields.

WARN ManagedMultiReader: 142-HSEARCH000289: Requested sort field (s) summary_forSort are not configured for entity \
Type org. hibernate. search. test. query. Book mapped to index Book, thus an uninverting reader must be created. You \
Shoshould declare the missing sort fields using @ SortField.

When migrating an existing project, you must re-create an effective index, which is described in detail in the relevant guidance. With all the required sorting fields configured, your query results will be sorted, just as the meeting host shouted to queue British Council members.

Hibernate Search details: click here
Hibernate Search: click here

Original ENGLISH: Order, ooorder! Sorting results in Hibernate Search 5.5

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.