The following bits of code in apachesolr_search are all wrong. Suppose you set hl.snippets = 5, meaning that you want 5 snippets for EACH field. That means you want 5 snippets for the content field, just as you want 5 snippets for any other field. Instead, the code below will make $snippets['content'] = THE LAST SNIPPET for the field, ignoring the other fields. my solution, from laziness, was to change the line

$snippets[$hl_param] = $values ---> $snippets[] = $values;

that way the multiple snippets for the content field are tracked separately. however, the developers of the module apparently want to preserve the field names and have $snippets['content'], $snippets['teaser'], and so on. in this case, you'd need

$snippets[$hl_param] = array();
...
$snippets[$hl_param][] = $values;

and then add some new lines to the theme function.

if i have time i'll post a patch which would obviously be better than my quick fix.

function apachesolr_search_process_response($response, DrupalSolrQueryInterface $query) {
 ............
      // Start with an empty snippets array.
      $snippets = array();

      // Find the nicest available snippet.
      foreach ($hl_fl as $hl_param) {
        if (isset($response->highlighting->{$doc->id}->$hl_param)) {
          // Merge arrays preserving keys.
          foreach ($response->highlighting->{$doc->id}->$hl_param as $values) {
            $snippets[$hl_param] = $values;
          }
        }
      }

function theme_apachesolr_search_snippets($vars) {
  $result = '';
  if (is_array($vars['snippets'])) {
    $snippets = $vars['snippets'];
    if (isset($snippets['content'])) {
      $result .= $snippets['content'];
      unset($snippets['content']);
    }
    if (isset($snippets['teaser'])) {
      $result .= (strlen($result) > 0) ? ' ... ' : '';
      $result .= $snippets['teaser'];
      unset($snippets['teaser']);
    }
    if (count($snippets)) {
      $result .= (strlen($result) > 0) ? ' ... ' : '';
      $result .= implode(' ... ', $snippets);
    }
  }
  return $result . ' ...';
}

Comments

nick_vh’s picture

Status: Active » Needs work

I hardly understand any of your explanation and your example code since the code you are posting is exactly the same one as the one in the module.
Please provide a patch and a clear way for anyone to be able to replicate the wanted behavior (important)

heacu’s picture

i will post a patch if/when i have time. to replicate the behavior just set hl.snippets = anything other than 1, for example:

function my_module_apachesolr_query_alter($query) {
$query->replaceParam('hl.snippets', 5);
}

then do a search and see only one snippet being grabbed from field 'content' ... whereas in the SOLR response (directly querying SOLR), you'll see an array of up to 5 snippets for the field.

nick_vh’s picture

Category: bug » feature

This is done like this by design. Changing from bug to feature request. The request is a valid one and we hope you could provide us with a patch for this. For the release of apachesolr we'll try to focus on the bugs first.

nick_vh’s picture

Version: 7.x-1.0-beta9 » 7.x-1.x-dev
nick_vh’s picture

Status: Needs work » Active
nick_vh’s picture

Title: snippets code all wrong if you want more than one snippet per field » As a themer I want to allow more then 1 snippet per result
cpliakas’s picture

I think I understand this request.

For me, I ran into this issue when working on the Rich Snippets module. Specifically, I wanted two separate highlighted fields: The normal highlighted content and the label. See http://drupal.org/files/project-images/date-range-facets_0.jpg for an example of the desired output, noting that matched are highlighted in the label similar to the major search engines.

The problem with the current implementation is that if you add the label as a highlighted field, it is jumbled with the content as this module only expects one snippet. So in the attached image, you can see that the title is appended to at the end of the normal snippet, which is not the desired effect.

Thanks,
Chris

cpliakas’s picture

Issue summary: View changes

Updated issue summary.