Pivot faceting

Pivot facets enable Solr to return facet counts across sets of fields. This means that the facet results are multi-level, where each level is a different Solr field. Pivot facets are great for hierarchical or tree faceting.

Tip

Pivot facets are not supported in distributed mode until Solr 4.10.

Pivot faceting is simple to use. The feature itself introduces only two new parameters:

  • facet.pivot: This field is similar to facet.field, but instead of giving it a single field, it expects an ordered comma-delimited list of fields. Each field is recursively faceted from the field listed before it in the list.
  • facet.pivot.mincount: This is similar to facet.mincount, but only for pivot facets. The default value is 1.

It's important to know that the facet.pivot parameter only accepts field names. It won't handle functions, for example. Additionally, the facet.pivot parameter can be specified multiple times.

Time for an example! Let's query the mbtracks core to show artist name, release name and the length of each track using pivot facets.

Note

As mentioned in the beginning of this chapter, it's important that you choose the right field types. The example we're about to show you mostly works because we've explicitly chosen fields that contain only single words. You'll see though, that the values are lowercased as a result of tokenizing/lower-casing—this is for demonstration purposes only.

The query is http://localhost:8983/solr/mbtracks/mb_tracks?omitHeader=true&wt=json&q=t_r_id:116747&rows=0&facet=true&facet.pivot=t_a_name,t_r_name,t_duration.

And here's the resulting response:

{"response":{"numFound":12,"start":0,"docs":[]},
 "facet_counts":{
   "facet_queries":{},
   "facet_fields":{},
   "facet_dates":{},
   "facet_ranges":{},
   "facet_pivot":{
     "t_a_name,t_r_name,t_duration":[{
       "field":"t_a_name",
       "value":"nirvana",
       "count":12,
       "pivot":[{
         "field":"t_r_name",
         "value":"nevermind",
         "count":12,
         "pivot":[
           {"field":"t_duration",
            "value":142,
            "count":1},
           {"field":"t_duration",
            "value":156,
            "count":1},
           {"field":"t_duration",
            "value":176,
           "count":1},
            ...]}]}]}}}

The relevant request and response values are highlighted. The facet.pivot parameter contains the list of fields. Here, we're querying on a release ID of 116747, which happens to be the album "nevermind" by "nirvana". The pivot facets contextually show the artist name, the release name, and the length of each track.

Tip

Solr 5.0 introduces an extremely useful enhancement to pivot facets—pivot facet stats. With this new feature, you can attach stats to each leaf node in the pivot tree. We cover the stats component in Chapter 5, Searching, and more details on the pivot stats feature can be found at https://cwiki.apache.org/confluence/display/solr/Faceting#Faceting-Pivot(DecisionTree)Faceting.

Hierarchical faceting

Pivot facets lend themselves quite well to hierarchical faceting. You could easily imagine a dataset where documents have geographical attributes, such as continent, country, region, and city. On the indexing side, these fields would need to be nontokenized, stored, and indexed string types. This would allow the values to be searched as well as displayed properly. On the output and query side, building a navigational tree with counts is trivial using pivot facets. You'd simply specify your list of fields as described previously facet.pivot=continent,country,region,city. The facet_pivot object in the response would contain all the data needed to build the UI component.

It's worth noting that pivot facets only work when the nodes within a tree have only one parent. The dataset described above works with pivot facets because there's a single path from child to parent. Datasets that have more than one parent (a family tree, and so on) will require a different solution. The Solr Wiki contains a good amount of information on hierarchical faceting and can be found at https://wiki.apache.org/solr/HierarchicalFaceting.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.30.253