【问题标题】:How can I make a full text index of the column?如何制作该列的全文索引?
【发布时间】:2017-03-12 00:06:41
【问题描述】:

这是我当前的迁移:

class News extends Migration
{
    public function up()
    {
        Schema::create('News', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('description');
            $table->integer('user_id')->unsigned()->index();
            $table->string('imgPath')->nullable();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('News');
    }
}

现在我需要对这些列分别进行全文索引:titledescription。所以我正在寻找这样的东西:->fulltext()。但是我在 Laravel 文档中没有找到类似的东西。

不管怎样,

  1. 如何在迁移中对单个列进行全文索引?喜欢:(title)
  2. 另外供我参考,如何在迁移中对多个列创建复合全文索引?喜欢:(title, description)

注意:我想要一个 intex,让我可以像这样搜索:. . . match(col) against('value')

【问题讨论】:

标签: php mysql laravel


【解决方案1】:

Laravel 不支持 FULLTEXT 搜索。 但是您可以将原始查询用作:

DB::statement('ALTER TABLE News ADD FULLTEXT search(title, description)');

注意 - 如果您不使用 MySQL 5.6+,我们必须将数据库引擎设置为 MyISAM 而不是 InnoDB。

$table->engine = 'MyISAM'; // means you can't use foreign key constraints

对于搜索,您可以这样做:

$q = Input::get('query');

->whereRaw("MATCH(title,description) AGAINST(? IN BOOLEAN MODE)", array($q))

【讨论】:

【解决方案2】:

注意!之前的答案是正确的,但是如果您正在测试您的应用程序并且您使用的是 sqlite(内存),则此命令将失败! 检查数据库类型

if( getenv('DB_CONNECTION') === 'mysql' ) // or something else

【讨论】:

    【解决方案3】:

    $table->fulltext(['title', 'description']); 你也可以像上面一样使用它 https://laravel.com/docs/8.x/migrations#creating-indexes

    【讨论】:

      猜你喜欢
      • 2012-09-03
      • 2020-04-13
      • 1970-01-01
      • 2022-07-27
      • 1970-01-01
      • 1970-01-01
      • 2016-05-17
      • 2010-11-03
      • 2023-04-01
      相关资源
      最近更新 更多