mysql - Laravel 5 中的全文搜索

标签 mysql laravel full-text-search

我对 Laravel 中的全文搜索有疑问。

我在各个列中应用了索引。

我有一个关键字

bar soap

现在我需要按以下顺序进行记录。

  • 肥皂
  • 肥皂 block
  • 酒吧
  • 肥皂

我的查询在这里

select `inventories`.*, MATCH (title, short_description, brand, long_description, inventory_code, product_code) AGAINST ('bar soap' IN BOOLEAN MODE) as score from `inventories` where MATCH (title, short_description, brand, long_description, inventory_code, product_code) AGAINST ('bar soap' IN BOOLEAN MODE) order by `score` desc

我也评论了这个doc .

您的帮助将不胜感激。

最佳答案

尝试这样的事情:

    // Text
    $search = 'bar soap';
    $parsedSearch = explode(' ', 'bar soap');

    // Create pattern
    $patterns = [];
    $patterns[] = $search; // bar soap
    $patterns[] = $parsedSearch[1] . ' ' . $parsedSearch[0]; // soap bar
    $patterns[] = $parsedSearch[0]; //bar
    $patterns[] = $parsedSearch[1]; //soap


    // Set Query
    $query = "select `inventories`.*, MATCH (title, short_description, brand, long_description, inventory_code, product_code) AGAINST ('bar soap' IN BOOLEAN MODE) as score from `inventories` where MATCH (title, short_description, brand, long_description, inventory_code, product_code) AGAINST ('bar soap' IN BOOLEAN MODE) order by field(`score`," . implode(',', $patterns) . ")";

    return $query;

您可能想要创建一个函数来创建模式以支持其他搜索,然后使用 MySQL ORDER BY FIELD 函数对其进行排序。

关于mysql - Laravel 5 中的全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57489244/

相关文章:

php - 如何在一次 ajax 调用中返回 html 代码和 php 值

curdate() 函数的 mysql 抛出语法错误

php - 如何在 laravel 迁移文件中使用多个数据库模式?

sql - 在 SQL 中实现子字符串搜索的最佳方法是什么?

mysql - 在 mysql innodb 全文搜索上添加 "dot/period"字符作为可搜索词

MySQL:在 BEFORE INSERT 触发器中执行类型转换

php - 使用 PHP pdo 在 MySQL 中插入和检索数组数据

php - 在 laravel 5.2 的分页链接中添加了随机字符

php - 使用.htaccess阻止通过/public/目录访问Laravel

php - 向 Solr 发布更新会导致重复