我已经使用“babenkoivan/scout-elasticsearch-driver”包在 laravel 5.5 中实现了 elasticsearch 。
作曲家.json
"require": {
"babenkoivan/scout-elasticsearch-driver": "^2.2",
"elasticsearch/elasticsearch": "^5.1",
"laravel/scout": "^3.0"
}
GGIndexConfigurator 文件用于创建弹性搜索索引。
<?php
namespace App;
use ScoutElastic\IndexConfigurator;
use ScoutElastic\Migratable;
class GGIndexConfigurator extends IndexConfigurator
{
use Migratable;
protected $settings = [
//
];
protected $defaultMapping = [
'properties' => [
'id' => [
'type' => 'integer',
],
'title' => [
'type' => 'string',
],
'img' => [
'type' => 'string',
],
'url' => [
'type' => 'string',
],
'type' => [
'type' => 'string',
],
'location' => [
'type' => 'geo_point',
],
'created_at' => [
'type' => 'date',
'format' => 'yyyy-MM-dd HH:mm:ss'
]
]
];
}
MySearchRule 文件告诉 elastic 在标题字段上进行搜索。
<?php
namespace App;
use ScoutElastic\SearchRule;
class MySearchRule extends SearchRule
{
public function buildQueryPayload()
{
return [
'must' => [
'match' => [
'title' => $this->builder->query
]
]
];
}
}
MegaBrand 文件以相同的模式存储/更新/删除弹性搜索中的记录,如添加 id、title、url、img 等。
<?php
namespace App;
use Carbon\Carbon;
use ScoutElastic\Searchable;
use Illuminate\Database\Eloquent\Model;
class MegaBrand extends Model
{
use Searchable;
protected $table = 'brands';
protected $primaryKey = 'brand_id';
protected $indexConfigurator = GlobalGarnerIndexConfigurator::class;
protected $searchRules = [
MySearchRule::class
];
protected $mapping = [
//
];
/**
* Get the index name for the model.
*
* @return string
*/
public function searchableAs()
{
return 'brands';
}
/**
* Get the indexable data array for the model.
*
* @return array
*/
public function toSearchableArray()
{
//$array = $this->toArray();
return [
'id' => $this->brand_id,
'title' => $this->brand_name,
'img' => $this->image,
'url' => $this->website,
'type' => 'brands',
'location' => [],
'created_at' => Carbon::now()->toDateTimeString()
];
}
}
以上所有代码用于弹性搜索文件和配置。现在我运行命令来创建弹性搜索索引。
php 工匠弹性:创建索引 App\GGIndexConfigurator
索引已成功创建。
品牌模型文件
<?php
namespace App\Model;
use App\GlobalGarnerIndexConfigurator;
use app\MySearchRule;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use ScoutElastic\Searchable;
class GiftIndiaModel extends Model
{
use SoftDeletes;
use Searchable;
protected $table = 'brands';
protected $primaryKey = 'brand_id';
protected $fillable = ['hash', 'brand_name', 'category', 'description', 'terms', 'image', 'discount', 'tat', 'isOnline', 'website', 'status', 'gg_brand_status'];
protected $indexConfigurator = GlobalGarnerIndexConfigurator::class;
protected $searchRules = [
MySearchRule::class
];
protected $mapping = [
//
];
/**
* Get the index name for the model.
*
* @return string
*/
public function searchableAs()
{
return 'brands';
}
/**
* Get the indexable data array for the model.
*
* @return array
*/
public function toSearchableArray()
{
//$array = $this->toArray();
return [
'id' => $this->brand_id,
'title' => $this->brand_name,
'img' => $this->image,
'url' => $this->website,
'type' => 'brand',
'location' => [],
'created_at' => Carbon::now()->toDateTimeString()
];
}
到目前为止一切都很好,没有任何问题。现在我创建了新功能以使用 eloquent 在数据库中添加品牌,并且该品牌也被添加到弹性搜索中:)
public function addBrand(){
$brand = new GiftIndiaModel([
'hash' => 'qwertyy123',
'brand_name' => 'microsoft',
'category' => 'laptop',
'description' => 'its good brand',
'terms' => 'lorum ipsum',
'image' => 'www.dell.com/image',
'discount' => 5,
'tat' => 2,
'isOnline' => 'yes',
'website' => 'www.dell.com',
'status' => 1
]);
if($brand->save()){
return response()->json(true , 200);
}
return response()->json(false , 400);
}
所以addBrand函数工作正常,我尝试php artisan scout:import App\\Brand
了在弹性搜索中添加品牌的命令并取得了成功。
真正的问题是当我软删除这个品牌时,它不会影响弹性搜索索引,而且我的软删除品牌在弹性搜索中仍然可用。
更新品牌功能
public function updateBrand(){
return GiftIndiaModel::where('brand_id', 3)->delete();
}
当我运行此功能时,品牌已成功从表中软删除,并且 deleted_at 字段存储 detele 日期时间。但它没有反映到弹性索引。
有什么建议吗?