Hierarchical Facets¶
Concept¶
Solr can'thandle hierarchies natively. However, there are approaches to search hierarchical structures in Solr. The implementation in EXT:solr is based on an article in Lucid Imagination's blog: http://www.lucidimagination.com/search/document/20d3fe5a6c9c0aea/hierarchical_faceting#75e7efcea99f6a87 .
Here's a short summary:
Imagine the following hierarchy of categories:
sport ____ cricket ____ skateboarding ____ ____ vert ____ ____ street finance
In a flat represantation this would look like these paths:
sport sport/cricket sport/skateboarding sport/skateboarding/vert sport/skateboarding/street finance
Most likely, in TYPO3 instead of names, you'll use UIDs, but for easier understanding we take strings here.
For each category we want to index one document. By the approach described in the blog post we would have to index these paths into a multi-value field with the paths transformed into one value per hierarchy level and prepended with the depth level:
- docA: category = 0-sport
- docb: category = 0-sport, 1-sport/cricket
- docC: category = 0-sport, 1-sport/skateboarding
- docD: category = 0-sport, 1-sport/skateboarding, 2-sport/skateboarding/vert
- docE: category = 0-sport, 1-sport/skateboarding, 2-sport/skateboarding/street
- docF: category = 0-finance
This means that each documents gets multiple categories assigned: One for each level in its category-rootline. The first element (before the -) is always the current level depth minus 1.
Configuration¶
The configuration of a hierarchical facet happens in two step. First we need to index the hierarchy according to the description above. In the second step we define the facet and that its data should be handled as a hierarchy.
Indexing¶
The TypoScript configuration to index a hierarchical facet for a page and its location within the page tree would look like this:
plugin.tx_solr.index {
queue {
pages {
fields {
// using a dynamic field of type string. The M defines the field as multi-valued
pageHierarchy_stringM = TEXT
pageHierarchy_stringM {
// this builds the original page path / hierarchy
value = {leveltitle:1}/{leveltitle:2}/{leveltitle:3}/
insertData = 1
}
}
}
}
fieldProcessingInstructions {
// tells the indexer that the field named "pageHierarchy_stringM" should be processed with the "pathToHierarchy" function.
pageHierarchy_stringM = pathToHierarchy
}
}
So first we set the "pageHierarchy_stringM"'s value to a simple humanreadable path. In the second step we instruct the indexer to process the field with the "pathToHierarchy" function. That function takes care of transforming the simple path string into the more complex data structure described above so you don't have to do it yourself.
Rendering¶
Finally we need to define a facet on the new field and configure it to be handled as a hierarchical facet:
plugin.tx_solr.search.faceting.facets {
pageHierarchy {
field = pageHierarchy_stringM
label = Page Hierarchy
// define the facet's type to be a hierarchy
type = hierarchy
// the hierarchy facet type is configured using TYPO3's HMENU content object
hierarchy = HMENU
hierarchy {
1 = TMENU
1 {
NO = 1
NO {
wrapItemAndSub = <li>|</li>
}
}
2 < .1
2.wrap = <ul>|</ul>
3 < .2
}
}
}
The menu structure provided to the HMENU is build by the solr Extension and for each menu item it provides the following fields you can use when building your custom menu:
- title
- facetKey
- numberOfResults
- _OVERRIDE_HREF
- ITEM_STATE