【发布时间】:2019-04-28 12:24:29
【问题描述】:
首先我将 scout-elasticsearch-driver 用于 Laravel Scout:https://github.com/babenkoivan/scout-elasticsearch-driver
我一步步按照自述文件,创建索引,迁移索引,配置映射,User::search()->get() 返回一个空数组。
显然我的数据库已迁移和填充。
我想通过以下方式搜索用户:
- 他的first_name
- 他的姓
- 他的昵称
所以我创建了一个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=*:*"
编辑 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'),
];
scout.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"
}
}
}
}
【问题讨论】:
-
您需要为您的索引提供映射。
-
@ArchitSaxena 感谢您的回复,但我的
User模型中的protected $mapping不是我索引的映射? -
哦,对不起,我一定是在上面闪闪发光。对我来说看起来不错。您能否打印通过您的代码生成的弹性 DSL 查询。您在代码中的映射看起来不错,但只是为了确保您查询的实际索引具有相同的映射,您可以发布
curl -XGET host:9200/user_index的响应吗? -
@ArchitSaxena 感谢您的回复,没关系,请参阅我的编辑 :)
-
嘿,这并不能告诉我太多。试试
curl -XGET host:9200/user这应该会给你索引的实际映射。您添加的是搜索的结果。另外,你能打印出产生User::search()->get()的查询吗?
标签: laravel elasticsearch laravel-scout