Skip to content

Instantly share code, notes, and snippets.

@vkuzmov
Created September 12, 2016 12:16
Show Gist options
  • Save vkuzmov/375ea81a380764f8ec8a3991ac2d6869 to your computer and use it in GitHub Desktop.
Save vkuzmov/375ea81a380764f8ec8a3991ac2d6869 to your computer and use it in GitHub Desktop.
// FULLTEXT search
if (! empty($filters['q'])) {
$pattern = 'leastone';
$term = format_search_string($filters['q'], $pattern);
$mode = ' IN BOOLEAN MODE';
$full_text_bool_expr = array(
"(5*MATCH(`tenders`.`title`) AGAINST ('{$term}'{$mode}))",
"(3*MATCH(`tenders`.`description`) AGAINST ('{$term}'{$mode}))",
);
$bool_relevance = \DB::raw(
'('.implode(' + ', $full_text_bool_expr).')'
);
$mode = '';
$full_text_natural_expr = array(
"(5*MATCH(`tenders`.`title`) AGAINST ('{$term}'{$mode}))",
"(3*MATCH(`tenders`.`description`) AGAINST ('{$term}'{$mode}))",
);
$natural_relevance = \DB::raw(
'('.implode(' + ', $full_text_natural_expr).')'
);
$query
->addSelect(\DB::raw("{$bool_relevance} as `bool_relevance`"))
->addSelect(\DB::raw("{$natural_relevance} as `natural_relevance`"))
->whereRaw("MATCH (`tenders`.`searchable`) AGAINST ('{$term}' IN BOOLEAN MODE)")
->orderBy('bool_relevance', 'desc')
->orderBy('natural_relevance', 'desc');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment