From 034171270fb1b7ed4202c8fca304b19b959b3486 Mon Sep 17 00:00:00 2001 From: Darren Inwood Date: Mon, 9 May 2016 10:21:07 +1200 Subject: [PATCH 1/2] Add an extension hook to allow altering search results --- code/solr/SolrIndex.php | 8 +++++++- docs/en/Solr.md | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/code/solr/SolrIndex.php b/code/solr/SolrIndex.php index 759f94f..2eb49a4 100644 --- a/code/solr/SolrIndex.php +++ b/code/solr/SolrIndex.php @@ -795,7 +795,13 @@ abstract class SolrIndex extends SearchIndex } } - return new ArrayData($ret); + $ret = new ArrayData($ret); + + // Enable extensions to add extra data from the response into + // the returned results set. + $this->extend('updateSearchResults', $ret, $res); + + return $ret; } diff --git a/docs/en/Solr.md b/docs/en/Solr.md index 215938c..6af8c2f 100644 --- a/docs/en/Solr.md +++ b/docs/en/Solr.md @@ -335,6 +335,50 @@ The searched term is highlighted with an `` tag by default. Note: It is recommended to strip out all HTML tags and convert entities on the indexed content, to avoid matching HTML attributes, and cluttering highlighted content with unparsed HTML. +### Adding additional information into search results + +Inside the SolrIndex::search() function, the third-party library solr-php-client +is used to send data to Solr and parse the response. Additional information can +be pulled from this response and added to your results object for use in templates +using the `updateSearchResults()` extension hook. + + $index = new MyIndex(); + $query = new SearchQuery(); + $query->search('My Term'); + $results = $index->search($query, -1, -1, array( + 'facet' => 'true', + 'facet.field' => 'SiteTree_ClassName', + )); + +By adding facet fields into the query parameters, our response object from Solr +now contains some additional information that we can add into the results sent +to the page. + + facet_counts)) { + foreach ($response->facet_counts as $facet => $count) { + $facetCounts[] = new ArrayData(array( + 'Name' => $facet, + 'Count' => $count, + )); + } + } + $results->setField('FacetCounts', new ArrayList($facetCounts)); + } + } + +We can now access the facet information inside our templates. + ### Adding Analyzers, Tokenizers and Token Filters When a document is indexed, its individual fields are subject to the analyzing and tokenizing filters that can transform and normalize the data in the fields. For example — removing blank spaces, removing html code, stemming, removing a particular character and replacing it with another From 234eac8308fde757d793d0eccf5e6e4acb393e52 Mon Sep 17 00:00:00 2001 From: Darren Inwood Date: Mon, 9 May 2016 16:19:01 +1200 Subject: [PATCH 2/2] More complete faceting example for docs. --- docs/en/Solr.md | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/docs/en/Solr.md b/docs/en/Solr.md index 6af8c2f..60d9a2e 100644 --- a/docs/en/Solr.md +++ b/docs/en/Solr.md @@ -363,17 +363,26 @@ to the page. * results pages. * @param $response The solr-php-client response object. */ - function updateSearchResults($results, $response) { - $facetCounts = array(); - if (isset($response->facet_counts)) { - foreach ($response->facet_counts as $facet => $count) { - $facetCounts[] = new ArrayData(array( - 'Name' => $facet, - 'Count' => $count, - )); - } + public function updateSearchResults($results, $response) + { + if (!isset($response->facet_counts) || !isset($response->facet_counts->facet_fields)) { + return; } - $results->setField('FacetCounts', new ArrayList($facetCounts)); + $facetCounts = ArrayList::create(array()); + foreach($response->facet_counts->facet_fields as $name => $facets) { + $facetDetails = ArrayData::create(array( + 'Name' => $name, + 'Facets' => ArrayList::create(array()), + )); + foreach($facets as $facetName => $facetCount) { + $facetDetails->Facets->push(ArrayData::create(array( + 'Name' => $facetName, + 'Count' => $facetCount, + ))); + } + $facetCounts->push($facetDetails); + } + $results->setField('FacetCounts', $facetCounts); } }