简体   繁体   中英

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

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 }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM