0

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.

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

Hello sir, in your ans you have passed $params['size'] = $per_page, $params['from'] = $from for pagination. In that where you set $from and $per_page and how you display pagination link on view page ? can you answer on my stackoverflow.com/questions/35719728/… question ?
$per_page = $request->get('limit', 10); $from = ($request->get('page', 1) - 1) * $per_page;
Check above answer
0

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()!!}

8 Comments

I am getting error like Class Modules\Catalog\Http\Controllers\Request does not exist on this line $per_page = $request->get('limit', 10);
I am using laravel 5.1
I am getting Class 'Modules\Catalog\Http\Controllers\LengthAwarePaginator' not found error
And what is $request if i use that i am getting that error also please help me
use Illuminate\Http\Request;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.