Is it possible for开发者_开发知识库 hibernate search to sort result according to best match after it has search result from the database
Lucene has a sort functionality (which defaults to relevance).
Hibernate search exposes this functionality (FullTextQuery.sort). if you do not want default behavior, you could pass your own sort object.
Sort sort = new Sort(new SortField("name"));
searchQuery.setSort(sort);
List results = searchQuery.list();
In your case default sort should be sufficient.
By default Hibernate Search will sort results based on the relevance of the results (as doc_180 mentioned) determined by the default Lucene scoring implementation.
However, if you are not satisfied with the way the way it is doing the ranking (e.g. you may want People entities to generally be ranked higher than all the other indexed entities that you have) then you could do one of two things:
- Apply a dynamic or static boost factor to the entities that should be considered more relevant (see the docs on the @Boost and @DynamicBoost annotations), or
- You could write your own custom scoring implementation by extending org.apache.lucene.search.Similarity (see Hibernate Search Advanced Features). The boost factor mentioned in point 1 is just one factor in this overall scoring algorithm.
精彩评论