user5876639
user5876639

Reputation:

How to pass search query to elasticsearch from laravel controller?

I want to pass nested query to elasticsearch from my laravel controller. my simple query is like

Simple Query

$params = [
            'index' => 'my_index',
            'type' => 'product',
            'body' => [
                    'query'=>[
                        'match'=>[
                            'title'=>'first'
                        ]
                    ]
                ]
            ];
        $response = \Es::Search($params); //passing query from here

It is working perfect.

How can i pass following nested query to \Es::Search($params); ?

My Nested Query :

{   
 "query": {
    "nested": {
      "path": "sku",
      "query": {
        "bool": {
          "must": [
            { "match": {"sku.price": "50"}}
          ]
        }
      }
    }   
  }
}

I am new in elasticsearch so please give some suggestions.

Upvotes: 0

Views: 1518

Answers (3)

Veerendra Borra
Veerendra Borra

Reputation: 1286

In controller iam using laravel lengthaware paginator method for pagination

public function getIndex(Request $request)
    {
         $per_page = $request->get('limit', 10);
            $from = ($request->get('page', 1) - 1) * $per_page;

    $params['size'] = $per_page;
            $params['from'] = $from;
            $params['index'] = config('elastic.Admin_Logs');
            $params['type'] = config('elastic.Admin_Type');
            $params['body']['sort']['meta.datetime']['order'] = "desc";
            $params['body']['query']['filtered']['filter']['bool']['must'][]['match_all'] = [];

                $params['body']['query']['filtered']['filter']['bool']['must'][]['match']['_id'] = $id;


                $params['body']['query']['filtered']['filter']['bool']['must'][]['range']['exceptions.count']['gt'] = 0;

                $params['body']['query']['filtered']['filter']['bool']['must'][]['range']['meta.datetime']['gte'] = $startdate;
                $params['body']['query']['filtered']['filter']['bool']['must'][]['range']['meta.datetime']['lte'] = $enddate;

            $response = $client->search($params);
            $access = $response['hits'];
            $admin_exceptions = new LengthAwarePaginator(
            $access['hits'],
            $access['total'],
            $per_page,
            Paginator::resolveCurrentPage(),
            ['path' => Paginator::resolveCurrentPath()]);
       return view('adminexception.index', compact('admin_exceptions'));
}

In your blade use render method

{!!$admin_exceptions->render()!!}

Upvotes: 0

David Coder
David Coder

Reputation: 1138

You can pass nested search query like :

$params = [
        'index' => 'my_index',
        'type' => 'product',
        'body' => [
                'query'=>[
                'nested'=> [
                    'path'=> 'category',
                        'query'=> [
                            'bool'=> [
                                'must'=> [
                                    'match'=>[
                                        'category.title'=> $catagory
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ];
    $response = \Es::Search($params); 

Hope it will help you.

Upvotes: 1

Veerendra Borra
Veerendra Borra

Reputation: 1286

you can pass nested query like below

$params['size'] = $per_page;
        $params['from'] = $from;
        $params['index'] = config('elastic.Admin_Logs');
        $params['type'] = config('elastic.Admin_Type');
        $params['body']['sort']['meta.datetime']['order'] = "desc";
        $params['body']['query']['filtered']['filter']['bool']['must'][]['match_all'] = [];

            $params['body']['query']['filtered']['filter']['bool']['must'][]['match']['_id'] = $id;


            $params['body']['query']['filtered']['filter']['bool']['must'][]['range']['exceptions.count']['gt'] = 0;

            $params['body']['query']['filtered']['filter']['bool']['must'][]['range']['meta.datetime']['gte'] = $startdate;
            $params['body']['query']['filtered']['filter']['bool']['must'][]['range']['meta.datetime']['lte'] = $enddate;

        $response = $client->search($params);

Upvotes: 1

Related Questions