Search code examples
phpsolrfaceted-search

How to add more details per result to solr facet?


I am returning a result with a facet from solr 6.4.1 where my goal is to have:

  1. dial
  2. dial_id
  3. count of dial (how many results)

Because I want to display the name (e.g. "black") as facete with the amount of results, AND create a link that will later on filter for it.

e.g.:

/search?dial_id=48

The reason behind it, is that I do not want to create a URL like this:

/search?dial=Grün

The facete looks like this at the moment:

<lst name="dial">
   <int name="Rot">5</int>
   <int name="Grün">4</int>
</lst>

Created via PHP:

foreach($this->setFacet AS $facet) $this->query->addFacetField(''.$facet.''); 

Clearly missing the ID

My code for retrieving currently looks like this:

function facete_dial(){
    foreach( $this->response->facet_counts->facet_fields->dial AS $dial => $count){
        $this->dial_facetes[$dial] = $count;    
    }
    return $this->dial_facetes;
}

Successfully returning the color and amount of results.

How can I add the dial_id to the facete in order to retrieve it later? Or is there a better aproach to this task?


Solution

  • The straight forward solution would be to create a secondary field - with the combined value of both the display value and the dial_id, combined with a separator in between.

    I.e. index the value 48|Grün, then do list($dial_id, $dial_name) = explode('|', $facet_name) in your PHP code.

    Since faceting is "how many different tokens and their counts exists in this field", there isn't any inherent correlation between different fields and their values.

    Creating a combined field and then extracting values from that field solves that issue.

    The other option is to perform a secondary look up of the dial_id => dial_name information, for example in MySQL or in a key/value store. Or if the information is updated seldomly, generate a PHP/JSON file with an associative array each time it's updated and include/read that in your application to resolve ids to names.