问题
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:
- one with a filter on "title OR text", without faceting
- one with a filter only on the "title" field, with faceting on the "title" field
- 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