Drupal Apache Solr Module Search Results Missing Title

After finally getting my Apache Solr EC2 instance connected I was able to index and search for results on my Drupal 7 website except low and behold, when I did a search the search results appeared without titles.

 

The first thing I did was look in the search result template file to see if the title was being called properly.

It was.

So I had to figure out why the title variable might not be populating.

I tuned on visible error logging for this Drupal site and saw a number of errors having to do with arrays.

Error messages give you all sorts of hints. I then dove into the module code to try to decipher what was going on. Why not start right where the error message says around line 1372 of the file, apachesolr_search.module

 

Here is the offending code:

function apachesolr_search_process_response($response, DrupalSolrQueryInterface $query) {

$fields = (array) $doc;
// Define our url options. They depend on the document language.
$url_options = array(‘absolute’ => TRUE);
if (isset($doc->ss_language) && isset($languages[$doc->ss_language])) {
$url_options[‘language’] = $languages[$doc->ss_language];
}
$result = array(
// link is a required field, so handle it centrally.
‘link’ => url($doc->path, $url_options),
// template_preprocess_search_result() runs check_plain() on the title
// again. Decode to correct the display.
‘title’ => htmlspecialchars_decode($doc->label, ENT_QUOTES),
// These values are not required by the search module but are provided
// to give entity callbacks and themers more flexibility.
‘score’ => $doc->score,
‘snippets’ => $snippets,
‘snippet’ => $snippet,
‘fields’ => $fields,
‘entity_type’ => $doc->entity_type,
‘bundle’ => $bundle,
);

You can see here that this code is treating the results as objects.

‘title’ => htmlspecialchars_decode($doc->label, ENT_QUOTES),

However, if you look at the JSON response of Apache Solr 6.x you will see the results are mostly array elements not objects. Note the brackets.

 


"response":{"numFound":11871,"start":0,"docs":[
{
"id":"noim8l/node/3050",
"site":["http://yeahchemistry.com/"],
"hash":["noim8l"],
"entity_id":[3050],
"entity_type":["node"],
"bundle":["forum"],
"bundle_name":["Questions"],
"ss_language":["und"],
"path":["node/3050"],
"url":["http://yeahchemistry.com/questions/electron-orbitalsperiodic-trends-there-book-covers"],
"path_alias":["questions/electron-orbitalsperiodic-trends-there-book-covers"],
"label":["Electron Orbitals/Periodic Trends- is there a book that covers this"],
"content":[" We've started our Atomic Theory unit and have just completed electron configuration/orbital properties/shapes and have moved on to periodic trends (shielding, Zeff, etc.). Needless to say, I am thoroughly confused. Is there any website or book (besides my chem book--Zumdahl 5th Edition) that I can look to for help? I think that chemistry is really interesting, I am just struggling a lot. It's discouraging, but I'm not giving up! "],
"teaser":[" We've started our Atomic Theory unit and have just completed electron configuration/orbital properties/shapes and have moved on to periodic trends (shielding, Zeff, etc.). Needless to say, I am thoroughly confused. Is there any website or book (besides my chem book--Zumdahl 5th Edition) that I"],
"ss_name":["caileywailey"],
"tos_name":["caileywailey"],
"ss_name_formatted":["caileywailey"],
"tos_name_formatted":["caileywailey"],
"is_uid":[1784],
"bs_status":[1],

So it stands to reason that if the JSON element “label” is returning an array not an object then I should do the same in the Drupal module code.

$fields = (array) $doc;
// Define our url options. They depend on the document language.
$url_options = array(‘absolute’ => TRUE);
if (isset($doc->ss_language) && isset($languages[$doc->ss_language])) {
$url_options[‘language’] = $languages[$doc->ss_language];
}
$result = array(
// link is a required field, so handle it centrally.
‘link’ => url($doc->path[0], $url_options),
// template_preprocess_search_result() runs check_plain() on the title
// again. Decode to correct the display.
‘title’ => htmlspecialchars_decode($doc->label[0], ENT_QUOTES),
// These values are not required by the search module but are provided
// to give entity callbacks and themers more flexibility.
‘score’ => $doc->score,
‘snippets’ => $snippets,
‘snippet’ => $snippet,
‘fields’ => $fields,
‘entity_type’ => $doc->entity_type,
‘bundle’ => $bundle,
);

 

And now we are correctly populating the title variable and we get our search result title links.

 

To clean up all the errors you need to do this is many places. Let the errors lead you there, but you get the gist of it.

..

Leave a Reply

Your email address will not be published. Required fields are marked *