loi*_*pez 8 elasticsearch laravel laravel-scout
首先,我使用Laravel Scout的scout-elasticsearch-driver:https : //github.com/babenkoivan/scout-elasticsearch-driver
我逐步遵循了自述文件,创建索引,迁移索引,配置映射并User::search()->get()
返回空数组的步骤。
显然,我的数据库已迁移并填充。
我想通过以下方式搜索用户:
所以我创建了一个IndexConfigurator:
class UserIndexConfigurator extends IndexConfigurator
{
use Migratable;
/**
* @var array
*/
protected $settings = [
//
];
}
Run Code Online (Sandbox Code Playgroud)
创建了一个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
]
]
]
]
];
}
}
Run Code Online (Sandbox Code Playgroud)
相应地配置了我的用户模型:
<?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', ];
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
编辑1: 向elasticsearch群集进行卷曲请求的结果
curl -XGET "http://localhost:9200/user/_search?pretty=true&q=*:*"
Run Code Online (Sandbox Code Playgroud)
编辑2:
curl -XGET "http://localhost:9200/user?pretty=true"
Run Code Online (Sandbox Code Playgroud)
打印此查询所产生的查询:
dd(User::search('lo')->explain());
Run Code Online (Sandbox Code Playgroud)
编辑3 Laravel Scout和elasticsearch驱动程序配置:
scout_elastic.php
curl -XGET "http://localhost:9200/user/_search?pretty=true&q=*:*"
Run Code Online (Sandbox Code Playgroud)
scout.php
curl -XGET "http://localhost:9200/user?pretty=true"
Run Code Online (Sandbox Code Playgroud)
编辑4:更新了elasticsearch映射
dd(User::search('lo')->explain());
Run Code Online (Sandbox Code Playgroud)
给出:
{
"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"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
解决方案是更新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',
],
],
];
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
943 次 |
最近记录: |