简体   繁体   English

如何在模型中为Algolia搜索Laravel配置索引

[英]How to configure index in model for Algolia search Laravel

I'm having a mini website with Algolia search, I want to search for item in Model 'Item' (means a table names 'item'). 我有一个具有Algolia搜索功能的小型网站,我想在“项目”模型中搜索项目(即表名称为“项目”)。 This is my code in Controller: 这是我在Controller中的代码:

    public function search(Request $request) {
    $query = $request->only(['query']);

    $items = Item::search($query['query'])->paginate(12);
    $total = $items->count();

    return view('shop.search', [
        'items'      => $items,
        'total'      => $total,
    ]);
}

I was able to search after run the following code: 运行以下代码后,我可以进行搜索:

php artisan scout:import "App\Item"

and then, It work fine. 然后,它工作正常。 But when I insert a new Item, I cannot search for that new Item. 但是,当我插入新项目时,无法搜索该新项目。 I have tried this code to configure "Index" (I think it can help) but it always show error :( 我已经尝试过使用此代码来配置“索引”(我认为它可以提供帮助),但是它始终显示错误:(

 public function searchableAs()
{
    return 'posts_index';
}

And the error is: 错误是:

Index posts_index does not exist

or 要么

indexName is not valid

Can I have any way to configure it? 有什么方法可以配置它吗?

searchableAs defines your index name in Algolia. searchableAs在Algolia中定义您的索引名称。 At this point, you imported your data in the items index, and you are searching in posts_index . 此时,您已将数据导入了items索引中,并且正在搜索posts_index That's why you get this error. 这就是为什么您会收到此错误。 You need to either remove the searchableAs override or reimport the data. 您需要删除searchableAs覆盖或重新导入数据。

About the data not being updated, it could be that: 关于未更新的数据,可能是:

  • You are using the queue 您正在使用队列
  • You are updating data without Eloquent 您在没有口才的情况下更新数据

If you run $item->update() it will update Algolia. 如果您运行$item->update() ,它将更新Algolia。 If you do Item::update(['title' => 'new title']) , you will need to chain the searchable() method. 如果执行Item::update(['title' => 'new title']) ,则需要链接searchable()方法。 https://laravel.com/docs/5.5/scout#updating-records https://laravel.com/docs/5.5/scout#updating-records

Configuring Model Indexes 配置模型索引

Each Eloquent model is synced with a given search "index", which contains all of the searchable records for that model. 每个Eloquent模型都与给定的搜索“索引”同步,该索引包含该模型的所有可搜索记录。 In other words, you can think of each index like a MySQL table. 换句话说,您可以将每个索引视为一个MySQL表。 By default, each model will be persisted to an index matching the model's typical "table" name. 默认情况下,每个模型都将保留到与模型的典型“表”名称匹配的索引。 Typically, this is the plural form of the model name; 通常,这是模型名称的复数形式。 however, you are free to customize the model's index by overriding the searchableAs method on the model: 但是,您可以通过覆盖模型上的searchableAs方法来自定义模型的索引:

<?php

        namespace App;

        use Laravel\Scout\Searchable;
        use Illuminate\Database\Eloquent\Model;

        class Post extends Model
        {
               use Searchable;

               /**
               * Get the index name for the model.
               *
               * @return string
               */
               public function searchableAs()
              {
                      return 'posts_index';
              }
 }

Algolia new index create "posts_index" and try it 阿尔戈利亚新索引创建“ posts_index”并尝试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM