PHP code example of savks / e-search

1. Go to this page and download the library: Download savks/e-search library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

savks / e-search example snippets




namespace App\ESearch\Resources;

use Closure;
use App\Models\Product;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

use Savks\ESearch\Support\Resource;
use Savks\ESearch\Support\Config;

class ProductResource extends Resource
{
    public static function id(): string
    {
        return 'product';
    }

    public static function configure(Config $config): void
    {
        //
    }


    public function seed(?array $ids, int $limit, Closure $callback, Closure $resolveCount, array $criteria = []): void
    {
        $query = Product::query()
            ->with([
                'categories',
            ]);

        if ($ids) {
            $query->whereIn('id', $ids);
        }

        $resolveCount(
            $query->count()
        );

        $query->chunk($limit, $callback);
    }


    public function clean(int $limit, Closure $callback, Closure $resolveCount, array $criteria = []): void
    {
    }


    public function mapTo(array $result): Collection
    {
        $ids = Arr::pluck($result['hits']['hits'], '_source.id');

        return Product::whereIn($ids)
            ->sortBy(function (Product $vehicle) use ($ids) {
                return \array_search($vehicle->id, $ids, true);
            })
            ->values();
    }


    public function buildDocument($vehicle): array
    {
        $isActive = $product->category_id
            && $product->is_active
            && $product->category->is_active;

        $data = [
            'id' => $product->id,
            'name_uk' => $product->getTranslation('name', 'uk'),
            'name_ru' => $product->getTranslation('name', 'ru'),
            'manufacturer_code' => mb_strtolower($product->manufacturer_code),
            'price' => $product->price,
            'category_id' => $product->categories->pluck('id'),
            'views' => $product->views ?: 0,
            'available' => $product->available,
            'is_active' => $isActive ? 1 : 0,
        ];

        return $data;
    }

    public function mapping(): array
    {
        return [
            'properties' => [
                'name_uk' => [
                    'type' => 'text',
                    // 'analyzer' => 'ukrainian',
                ],
                'name_ru' => [
                    'type' => 'text',
                    // 'analyzer' => 'russian',
                ],
                'manufacturer_code' => [
                    'type' => 'keyword'
                ],
                'price' => [
                    'type' => 'scaled_float',
                    'scaling_factor' => 100,
                ],
                'category_id' => [
                    'type' => 'long'
                ],
                'views' => [
                    'type' => 'integer'
                ],
                'available' => [
                    'type' => 'boolean'
                ],
                'is_active' => [
                    'type' => 'boolean'
                ],
            ]
        ];
    }

    private function calcWeight(Vehicle $vehicle): int
    {
    }
}

return [
    'resources' => [
        'product' => App\ESearch\Resources\ProductResource::class,

        // or

        App\ESearch\Resources\ProductResource::class,
    ],
]