From d329089eb74a66c873d68c5b0822d9756537ecb8 Mon Sep 17 00:00:00 2001 From: Jim Keller Date: Thu, 23 Apr 2020 01:37:20 -0400 Subject: [PATCH] added support for changing post excerpt to highlighted snippet returned from solr --- includes/class-solrpower-wp-query.php | 29 ++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/includes/class-solrpower-wp-query.php b/includes/class-solrpower-wp-query.php index 4408dfbc..bfad4522 100644 --- a/includes/class-solrpower-wp-query.php +++ b/includes/class-solrpower-wp-query.php @@ -59,6 +59,13 @@ class SolrPower_WP_Query { */ public $query; + /** + * Highlighted field snippets + * + * @var array + */ + public $highlighting; + /** * Grab instance of object. * @@ -199,7 +206,10 @@ function posts_request( $request, $query ) { if ( $search->getFacetSet() ) { $this->facets = $search->getFacetSet()->getFacets(); } - $search = $search->getData(); + + $this->highlighting = $search->getHighlighting(); + + $search = $search->getData(); $search_header = $search['responseHeader']; $search = $search['response']; @@ -258,6 +268,23 @@ private function parse_results( $search ) { continue; } + if ( 'solr_id' === $key ) { + if ( $this->highlighting ) { + $highlighted_doc = $this->highlighting->getResult($value); + + if ( $highlighted_doc ) { + $snippet = ''; + + foreach ( $highlighted_doc as $field => $highlight ) { + $snippet .= implode(' ... ', $highlight); + } + + $post->excerpt = $snippet; + continue; + } + } + } + $post->$key = $value; } $post->solr = true;