Is there a way in hibernate-search to facet by fields where the search term was found?

淺唱寂寞╮ 提交于 2019-12-11 17:25:26

问题


Consider the following indexed entity:

@Entity
@Indexed
public class Document {

    ...

    @Field
    private String title;

    @Field
    private String text;

}

Is there a way to present user a facet that will contain two options title and text with a count of documents where the search term was found in title and text respectively? And the user should be able to select these options to search only by interesting fields.

For example, there are three documents:

{ "title" : "One", "text" : "One" }
{ "title" : "One and Two", "text" : "Two" }
{ "title" : "Three", "text" : "Three and Two" }

And the search query is "one": then the facet will be:

{ "title" : 2, "text" : 1 }

回答1:


There is no such built-in feature in Hibernate Search, but you can do it yourself. Instead of running a single query, run three:

  1. one with a filter on "title OR text", without faceting
  2. one with a filter only on the "title" field, with faceting on the "title" field
  3. one with a filter only on the "text" field, with faceting on the "text" field

Then gather the results from the first query, the "title" facet from the second query, and the "text" facet from the third query.

More information about faceting in Hibernate Search: https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#query-faceting



来源:https://stackoverflow.com/questions/47831166/is-there-a-way-in-hibernate-search-to-facet-by-fields-where-the-search-term-was

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!