Skip to content

Commit

Permalink
added support for changing post excerpt to highlighted snippet return…
Browse files Browse the repository at this point in the history
…ed from solr
  • Loading branch information
jimkeller committed Apr 23, 2020
1 parent 1413a4e commit d329089
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion includes/class-solrpower-wp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ class SolrPower_WP_Query {
*/
public $query;

/**
* Highlighted field snippets
*
* @var array
*/
public $highlighting;

/**
* Grab instance of object.
*
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d329089

Please sign in to comment.