2017-11-21 6 views
0

"babenkoivan/scout-elasticsearch-driver"パッケージを使用して、laravel 5.5でelasticsearchを実装しました。Laravel - 弾性検索Laravel Scoutパッケージsoftdeleteは機能していません

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 
      ] 
     ] 
    ]; 
} 
} 

IMGなどのIDを追加するように/更新/同じパターンで弾性検索でレコードを削除し保存するMegaBrandファイル、タイトル、URL、...すべてのコード上で

<?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

インデックスがで正常に作成することです。この時点件まで

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() 
    ]; 
} 

すべてがすべての問題を持っていない良いです。今私は雄弁使用してデータベースにブランドを追加するための新しい関数を作成し、そのブランドはまた、弾性検索に追加されます:)

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日付time.Butから削除されて実行した場合には、弾性インデックスに反映していません。

アドバイスはありますか?

+0

クエリdeleted_atを検索することはできません。 –

+0

@HoàngĐăngはい、ブランドがソフトデリートされている場合、私は弾性検索では表示したくありません。 – Dhaval

+0

コードが仲間に動作していないhttps://laravel.com/docs/5.5/scout#removing-records –

答えて

0

@Babenkoはソフト削除は現在サポートされていないとメールで教えてください。

私はこれを行うために他の方法を見つけます。

public function updateBrand() 
{ 

    $brand = GiftIndiaModel::where('brand_id', 3)->first(); 

    $brand->deleted_at = date("Y-m-d"); 

    $brand->save(); 

    $brand->unsearchable(); 

} 

は、上記のコードでは、私が後にソフト削除弾性検索インデックスから項目を削除するには検索不可能()メソッドを使用している見ることができます。

また、私はgithubの中の問題/機能要求を作成しています。

github

更新/弾性検索でレコードを挿入しながら、誰もが問題を抱えている場合、私はモデルの方法)(保存を使用することをお勧めいたします。

関連する問題