如何从laravel控制器传递搜索查询到elasticsearch?

时间:2016-02-27 04:57:27

标签: php laravel elasticsearch

我想将嵌套查询从laravel控制器传递给elasticsearch。 我的简单查询就像

简单查询

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

这是完美的。

如何将以下嵌套查询传递给\Es::Search($params);

我的嵌套查询:

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

我是弹性搜索的新手,所以请提出一些建议。

3 个答案:

答案 0 :(得分:1)

您可以传递嵌套查询,如下所示

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

答案 1 :(得分:1)

您可以传递嵌套搜索查询,如:

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

希望它会对你有所帮助。

答案 2 :(得分:0)

在控制器iam中使用laravel lengthaware paginator方法进行分页

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

在您的刀片中使用渲染方法

{!! $ admin_exceptions->渲染()!}