Hierachical facets with solr

If you have been working with TYPO3 search engine "indexed_search" before, you might perhaps know that there is a selector that lets the user restrict the search results to certain parts of the page tree. Since SOLR seems to be the way to go concerning search with TYPO3, here is a little tutorial on how a similar feature can be implemented with the TYPO3 solr extension.

For a comprehensive introduction on implementing hierarchical facets and filters with SOLR, take a look at:

Storing TYPO3 page-tree information in SOLR index

What we want to have looks like this: Assuming we have a page tree like

1|-2| |-3| | |-4...

so we have a rootline for Page 4 which is 1/2/3/4. As SOLR cannot handle hierarchical data like paths directly we have to do a little trick. We cut the rootline in parts and create snippets for each "depth" of the rootline. Those snippets look like that: 0-1, 1-1/2, 2-1/2/3, 3-1/2/3/4 where the first number encodes the depth of the snippet followed by a "-" and the rootline for this depth.

For TYPO3 solr extension with version prior to 2.0 following changes of the schema are required:

Now we need a field processor class, which has to implement an interface "tx_solr_FieldProcessor". This field processor gets a plain pageUid and transforms it to the rootline snippets which are returned to be inserted into the SOLR index:

The next step is a little ugly, as we have to touch the solr extension code itself. In order to make the rootline processor work, we have to add some line of code to the "tx_solr_fieldprocessor_Service":

Using rootline information for filtering search results

Last but not least, we should think about how to use the information gathered from the facets for restricting our search results. We therefore use a simply filter query on the field rootline:

// all documents with rootline 3-1/27/32
select?q=*:*&fq=rootline:3-1/27/32
// OR query for two different rootlines
select?q=*:*&fq=rootline:3-1/27/32%20OR%20rootline:2-1/41

Query for documents within rootline 3-1/27/32

Fazit

This post should give you an impression on how to realize hierarchical facets with SOLR and TYPO3 solr extension. It is now up to your needs how to render the facets and building a nice user interface for it. Once you start playing around with these features you will surely come up with many possible applications. Have fun!