Skip to content

Commit

Permalink
[plugin][search][content] give priority on result when title is match…
Browse files Browse the repository at this point in the history
…ed (#20197)

* [plugin][search][content] give priority on result when title is matched

* Missed comma

* Add relevance weighting according to number of words

* Relevance by number of words in title only, removed introtext relevance

* Fix order string concatenation
  • Loading branch information
alikon authored and Michael Babker committed Apr 29, 2018
1 parent 8b480c7 commit 3400df5
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions plugins/search/content/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public function onContentSearch($text, $phrase = '', $ordering = '', $areas = nu
$wheres2[] = 'a.metakey LIKE ' . $text;
$wheres2[] = 'a.metadesc LIKE ' . $text;

$relevance[] = ' CASE WHEN ' . $wheres2[0] . ' THEN 5 ELSE 0 END ';

// Join over Fields.
$subQuery = $db->getQuery(true);
$subQuery->select("cfv.item_id")
Expand Down Expand Up @@ -146,6 +148,8 @@ public function onContentSearch($text, $phrase = '', $ordering = '', $areas = nu
$wheres2[] = 'LOWER(a.metakey) LIKE LOWER(' . $word . ')';
$wheres2[] = 'LOWER(a.metadesc) LIKE LOWER(' . $word . ')';

$relevance[] = ' CASE WHEN ' . $wheres2[0] . ' THEN 5 ELSE 0 END ';

if ($phrase === 'all')
{
// Join over Fields.
Expand Down Expand Up @@ -274,6 +278,12 @@ public function onContentSearch($text, $phrase = '', $ordering = '', $areas = nu
$case_when1 .= ' ELSE ';
$case_when1 .= $c_id . ' END as catslug';

if (!empty($relevance))
{
$query->select(implode(' + ', $relevance) . ' AS relevance');
$order = ' relevance DESC, ' . $order;
}

$query->select('a.title AS title, a.metadesc, a.metakey, a.created AS created, a.language, a.catid')
->select($query->concatenate(array('a.introtext', 'a.fulltext')) . ' AS text')
->select('c.title AS section, ' . $case_when . ',' . $case_when1 . ', ' . '\'2\' AS browsernav')
Expand Down Expand Up @@ -342,6 +352,12 @@ public function onContentSearch($text, $phrase = '', $ordering = '', $areas = nu
$case_when1 .= ' ELSE ';
$case_when1 .= $c_id . ' END as catslug';

if (!empty($relevance))
{
$query->select(implode(' + ', $relevance) . ' AS relevance');
$order = ' relevance DESC, ' . $order;
}

$query->select(
'a.title AS title, a.metadesc, a.metakey, a.created AS created, '
. $query->concatenate(array('a.introtext', 'a.fulltext')) . ' AS text,'
Expand Down

0 comments on commit 3400df5

Please sign in to comment.