0
votes

I have implemented elasticsearch in laravel 5.5 using "babenkoivan/scout-elasticsearch-driver" package.

composer.json

"require": {
 "babenkoivan/scout-elasticsearch-driver": "^2.2",
 "elasticsearch/elasticsearch": "^5.1",
 "laravel/scout": "^3.0"
}

GGIndexConfigurator file use to creat elastic search index.

<?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 file tell elastic to search on title field.

<?php

namespace App;

use ScoutElastic\SearchRule;

class MySearchRule extends SearchRule
{
 public function buildQueryPayload()
{
    return [
        'must' => [
            'match' => [
                'title' => $this->builder->query
            ]
        ]
    ];
}
}

MegaBrand file to store / update / remove record in elastic search in same pattern like add id , title , url , img etc..

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

above all code is for elastic search files and configurations. Now I run commend to create elastic search index.

php artisan elastic:create-index App\GGIndexConfigurator

index is create successfully with.

BrandModel file

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

Upto this point all is good not have any issue. now I create new function to add brand in database using eloquent and that brand is also added in elastic search :)

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);

}

So addBrand function is working fine and I have tried php artisan scout:import App\\Brand command to add brand in elastic search with all success.

Real issue is when I softdelete this brand it doesn't affect elastic search index and my softdelete brand is still available in elastic search.

Update Brand function

public function updateBrand(){

    return GiftIndiaModel::where('brand_id', 3)->delete();

}

When I run this function brand is successfully soft deleted from table and deleted_at field store detele date time.But it's not reflecting to elastic index.

Any advice ?

1
So you can not search for the query deleted_at?Felix
@HoàngĐăng yes if brand is softdeleted I don't want to show in elastic search.Dhaval
according to document your code should be worked laravel.com/docs/5.5/scout#removing-recordsFelix
not working bro. any suggestion ?Dhaval

1 Answers

0
votes

As @Babenko tell me in mail that soft deleting is not supported right now.

I find other way to do this.

public function updateBrand()
{

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

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

  $brand->save();

  $brand->unsearchable();

}

In above code you can see I have used unsearchable() method to remove item from elastic search index after soft delete.

I have also create issue/feature request in github.

github

If anyone having issue while updating / inserting record in elastic search I recommend you to use save() method of model.