【问题标题】:How to pass search query to elasticsearch from laravel controller?如何将搜索查询从 laravel 控制器传递给 elasticsearch?
【发布时间】:2016-03-10 20:33:18
【问题描述】:

我想将嵌套查询从我的 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"}}
          ]
        }
      }
    }   
  }
}

我是 elasticsearch 新手,所以请给一些建议。

【问题讨论】:

    标签: php laravel elasticsearch


    【解决方案1】:

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

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

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      你可以像下面这样传递嵌套查询

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

      【讨论】:

      • 您好先生,在您的回答中,您已通过 $params['size'] = $per_page, $params['from'] = $from 进行分页。在哪里设置 $from 和 $per_page 以及如何在视图页面上显示分页链接?你能回答我的stackoverflow.com/questions/35719728/… 问题吗?
      • $per_page = $request->get('limit', 10); $from = ($request->get('page', 1) - 1) * $per_page;
      • 查看以上答案
      【解决方案3】:

      在控制器 iam 中使用 laravel longaware 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->render()!!}

      【讨论】:

      • 我收到类似 Class Modules\Catalog\Http\Controllers\Request 在这一行不存在的错误 $per_page = $request->get('limit', 10);
      • 我使用的是 laravel 5.1
      • 我收到 Class 'Modules\Catalog\Http\Controllers\LengthAwarePaginator' not found 错误
      • 如果我使用 $request 是什么,我也收到该错误,请帮助我
      • use Illuminate\Http\Request;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      相关资源
      最近更新 更多