9

首先,我将 scout-elasticsearch-driver 用于 Laravel Scouthttps ://github.com/babenkoivan/scout-elasticsearch-driver

我一步一步地按照自述文件,创建索引,迁移索引,配置映射并User::search()->get()返回一个空数组。

显然我的数据库已迁移和填充。

我想通过以下方式搜索用户:

  • 他的名字
  • 他的姓氏
  • 他的昵称

所以我创建了一个IndexConfigurator

class UserIndexConfigurator extends IndexConfigurator
{
   use Migratable;

   /**
   * @var array
   */
   protected $settings = [
     //
   ];
}

创建了一个SearchRule

class UserSearchRule extends SearchRule
{
   public function buildQueryPayload()
   {
       $query = $this->builder->query;

       return [
          'should' => [
              [
                'match' => [
                   'first_name' => [
                              'query' => $query
                          ]
                      ]
                  ],
                  [
                      'match' => [
                          'last_name' => [
                              'query' => $query
                          ]
                      ]
                  ],
                  [
                      'match' => [
                          'nick_name' => [
                              'query' => $query
                          ]
                      ]
                  ]
              ]
          ];
   }
}

相应地配置了我的用户模型

<?php
class User extends Authenticatable

  {
    useSearchable;
    /**
     * @var string
     */
    protected $indexConfigurator = UserIndexConfigurator::class;

    /**
     * @var array
     */
    protected $searchRules = [UserSearchRule::class ];

    /**
     * @var array
     */
    protected $mapping = ['properties' => ['first_name' => ['type' => 'text'], 'last_name' => ['type' => 'text'], 'nick_name' => ['type' => 'text'], ]];
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['id', 'first_name', 'last_name', 'nick_name', ];
  }

我错过了什么吗?

编辑1: 对elasticsearch集群的curl请求的结果

curl -XGET "http://localhost:9200/user/_search?pretty=true&q=*:*"

curl -XGET 在 http://localhost:9200

编辑2:

curl -XGET "http://localhost:9200/user?pretty=true"

用户索引的实际映射

产生的查询的打印:

dd(User::search('lo')->explain());

解释弹性搜索请求

编辑 3 Laravel scout 和 elasticsearch 驱动程序配置:

scout_elastic.php

<?php declare(strict_types = 1);

return [
    'client' => [
        'hosts' => [
            env('SCOUT_ELASTIC_HOST', 'localhost:9200'),
        ],
    ],
    'update_mapping' => env('SCOUT_ELASTIC_UPDATE_MAPPING', true),
    'indexer' => env('SCOUT_ELASTIC_INDEXER', 'single'),
    'document_refresh' => env('SCOUT_ELASTIC_DOCUMENT_REFRESH', 'wait_for'),
];

侦察兵.php

<?php declare(strict_types = 1);

return [

    /*
    |--------------------------------------------------------------------------
    | Default Search Engine
    |--------------------------------------------------------------------------
    |
    | This option controls the default search connection that gets used while
    | using Laravel Scout. This connection is used when syncing all models
    | to the search service. You should adjust this based on your needs.
    |
    | Supported: "algolia", "null", "elastic"
    |
    */

    'driver' => env('SCOUT_DRIVER', 'elastic'),

    /*
    |--------------------------------------------------------------------------
    | Index Prefix
    |--------------------------------------------------------------------------
    |
    | Here you may specify a prefix that will be applied to all search index
    | names used by Scout. This prefix may be useful if you have multiple
    | "tenants" or applications sharing the same search infrastructure.
    |
    */

    'prefix' => env('SCOUT_PREFIX', ''),

    /*
    |--------------------------------------------------------------------------
    | Queue Data Syncing
    |--------------------------------------------------------------------------
    |
    | This option allows you to control if the operations that sync your data
    | with your search engines are queued. When this is set to "true" then
    | all automatic data syncing will get queued for better performance.
    |
    */

    'queue' => env('SCOUT_QUEUE', true),

    /*
    |--------------------------------------------------------------------------
    | Chunk Sizes
    |--------------------------------------------------------------------------
    |
    | These options allow you to control the maximum chunk size when you are
    | mass importing data into the search engine. This allows you to fine
    | tune each of these chunk sizes based on the power of the servers.
    |
    */

    'chunk' => [
        'searchable' => 500,
        'unsearchable' => 500,
    ],

    /*
    |--------------------------------------------------------------------------
    | Soft Deletes
    |--------------------------------------------------------------------------
    |
    | This option allows to control whether to keep soft deleted records in
    | the search indexes. Maintaining soft deleted records can be useful
    | if your application still needs to search for the records later.
    |
    */

    'soft_delete' => false,

    /*
    |--------------------------------------------------------------------------
    | Algolia Configuration
    |--------------------------------------------------------------------------
    |
    | Here you may configure your Algolia settings. Algolia is a cloud hosted
    | search engine which works great with Scout out of the box. Just plug
    | in your application ID and admin API key to get started searching.
    |
    */

    'algolia' => [
        'id' => env('ALGOLIA_APP_ID', ''),
        'secret' => env('ALGOLIA_SECRET', ''),
    ],

];

编辑 4:更新了弹性搜索映射

php artisan elastic:update-mapping App\\Models\\User

给出:

{
  "user" : {
    "aliases" : {
      "user_write" : { }
    },
    "mappings" : {
      "users" : {
        "properties" : {
          "first_name" : {
            "type" : "text",
            "analyzer" : "standard"
          },
          "id" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "last_name" : {
            "type" : "text",
            "analyzer" : "standard"
          },
          "nick_name" : {
            "type" : "text",
            "analyzer" : "standard"
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1552675390883",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "hdtsA8ncQNC8rrI5Tr853A",
        "version" : {
          "created" : "6050499"
        },
        "provided_name" : "user"
      }
    }
  }
}
4

2 回答 2

3

解决方案是更新UserSearchRule

<?php declare(strict_types = 1);

namespace App\Models\SearchRules;

use ScoutElastic\SearchRule;

/**
 * Class UserSearchRule
 *
 * @package App\Models\SearchRules
 */
class UserSearchRule extends SearchRule
{

    /**
     * @inheritdoc
     */
    public function buildQueryPayload()
    {
        $query = $this->builder->query;

        return [
            'must'  => [
                'multi_match'   => [
                    'query'     => $query,
                    'fields'    => ['first_name', 'last_name', 'nick_name'],
                    'type'      => 'phrase_prefix',
                ],
            ],
        ];
    }
}
于 2019-04-11T15:58:41.397 回答
-1

我尝试了答案,但对我来说不起作用。

如果答案不起作用,我们可以手动执行此操作并获取命中结果。

   $query = request()->has('q') ? request('q') : '*';
   $results = Product::search($query)->raw()['hits'];
   $products = collect($results['hits'])->pluck('_source');
于 2019-07-01T00:24:47.633 回答