5

I'm seeing an interesting anomaly (?) I'm trying to get to the bottom of. I've set up a view with an exposed filter Search: Search Terms. I am trying to mimic the result set offered by standard drupal search module, but am finding interesting results based on the score.

When I search for "term1" (no quotes) in both standard site search, and within the view:

  • result set is similar, but not the same.
  • actual score returned for items in sitewide search, and views search are different.

So I guess, my question really is "where is the search modules' results score coming from, and how can I recreate it within a view?"

I should add I am not using any special search modules at all, just core search module.

Core search sql, as viewed from devel:

SELECT i.type AS type, i.sid AS sid, SUM((45.0578814438 * i.score * t.count)) AS calculated_score 
FROM search_index i 
INNER JOIN node n ON n.nid = i.sid 
INNER JOIN search_total t ON i.word = t.word 
INNER JOIN search_dataset d ON i.sid = d.sid AND i.type = d.type 
WHERE (n.status = :db_condition_placeholder_0) AND( (i.word = :db_condition_placeholder_1) )AND (i.type = :db_condition_placeholder_2) AND( (d.data LIKE :db_condition_placeholder_3 ESCAPE '\\') ) GROUP BY i.type, i.sid 
HAVING (COUNT(*) >= :matches) 
ORDER BY calculated_score DESC 
LIMIT 10 OFFSET 0

From the created view:

SELECT node.title AS node_title, node.nid AS nid, node.type AS node_type, SUM(search_index.score * search_total.count) AS score, 'node' AS field_data_body_node_entity_type, 'search_test:page' AS view_name
FROM 
{node} node
LEFT JOIN {search_index} search_index ON node.nid = search_index.sid
LEFT JOIN {search_total} search_total ON search_index.word = search_total.word
WHERE (( (node.status = '1') AND( (search_index.type = 'node') AND( (search_index.word = 'test') ))))
GROUP BY search_index.sid, score, node_title, nid, node_type, field_data_body_node_entity_type, view_name
HAVING (( (COUNT(*) >= '1') ))
ORDER BY score DESC
LIMIT 10 OFFSET 0
0

There are a few things affecting the difference in results. I'm sure you noticed the queries differ slightly.

  • The first and core issue is that the search module's main table is search_index, so each row is built around entries in that table and then joined outward to get more information. Your view is built around the node table and joined the other direction to search index.
  • The core search uses inner joins to get node data, which will be more limiting in its result sets (see this answer.)
  • The search_dataset table isn't joined from the Views search (see this answer for an explanation of why.)
  • Core search multiplies the search scores by relevance, which shouldn't affect their ranking, but looks confusing. See line 496-500 of search.extender.inc.

I know I only answered the first half of your question, but I hope this helps. I'm not sure it's possible to get a View to exactly mirror your search results without considerable customization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.