我使用" babenkoivan / scout-elasticsearch-driver" 包在laravel 5.5中实现了弹性搜索。
composer.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文件告诉弹性来搜索标题字段。
<?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 artisan elastic:create-index App \ GGIndexConfigurator
索引是使用。
成功创建的BrandModel文件
<?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
命令在弹性搜索中添加品牌,但都取得了成功。
真正的问题是当我软件删除此品牌时,它不会影响弹性搜索索引,而且我的softdelete品牌仍然可用于弹性搜索。
更新品牌功能
public function updateBrand(){
return GiftIndiaModel::where('brand_id', 3)->delete();
}
当我运行此功能时,品牌已成功从表格中删除并且deleted_at字段存储了拒绝日期时间。但它没有反映弹性索引。
有什么建议吗?
答案 0 :(得分:0)
正如@Babenko在邮件中告诉我的那样,目前不支持软删除。
我找到其他方法来做到这一点。
public function updateBrand()
{
$brand = GiftIndiaModel::where('brand_id', 3)->first();
$brand->deleted_at = date("Y-m-d");
$brand->save();
$brand->unsearchable();
}
在上面的代码中,您可以看到我使用 unsearchable()方法在软删除后从弹性搜索索引中删除项目。
我还在github中创建了问题/功能请求。
如果在弹性搜索中更新/插入记录时有任何问题,我建议您使用 save()模型方法。