Search code examples
phplaravelmongodbelasticsearch

Convert an Elasticsearch query in Laravel to the MongoDB structure


 private function comparedProduct()
{
    $this->info('Create for compared product');

   $comparedProductParam =
    [
        'meta_field' => 1,
        'with_scroll_id' => 1,
        'scroll' => $this->scrollDuration,
        'scroll_size' => $this->perPage,
        'index' => 'compared_product',
        'sort' => [["created_at" => ["order" => "ASC"]]],
    ];
    $scrollIdForComparedProduct = $this->esProductService->findBy($comparedProductParam);
    $skip = 0;
    $limit = $this->perPage; 

   // $comparedProducts = ComparedProduct::orderBy('created_at', 'asc')->skip($skip)->limit($limit)->get()->toArray();

    $comparedProducts = fillOnUndefined($scrollIdForComparedProduct, 'response', []);

    $this->info('compared product is : 1');

    $this->storeComparedProduct($comparedProducts);

    for ($i=2; $i <= 15; $i++)
    {
        $this->info('compared product is : '.$i);

        $scrollParam =
        [
            'scroll_id' => $scrollIdForComparedProduct['scroll_id'],
            'scroll' => $this->scrollDuration
        ];
        $response = $this->esProductService->scroll($scrollParam);
        if(!$response['response'])
        {
            continue;
        }

        $this->storeComparedProduct($response['response']);
    }

}

I want to convert this Elasticsearch query to the MongoDB structure, but I am having trouble doing it correctly. How can I adapt the scroll method used in Elasticsearch to MongoDB, and how can I correctly provide this within a condition?


Solution

  • all similar activities you'll find in $skip and $limit. after all the query actions, results are paginated using $skip and $limit. but it will be necessary to pass parameters like page, perPage, limit

    ->skip(10)->limit(10) - will be the next 10 entiries.

    1. Create query
    2. where, sort, etc
    3. paginate