[英]How to pass search query to elasticsearch from laravel controller?
我想將嵌套查詢從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的新手,請提出一些建議。
您可以通過嵌套查詢,如下所示
$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 = [
'index' => 'my_index',
'type' => 'product',
'body' => [
'query'=>[
'nested'=> [
'path'=> 'category',
'query'=> [
'bool'=> [
'must'=> [
'match'=>[
'category.title'=> $catagory
]
]
]
]
]
]
]
];
$response = \Es::Search($params);
希望對您有幫助。
在控制器IAM中,使用laravel lengthaware分頁器方法進行分頁
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'));
}
在刀片中使用render方法
{!! $ admin_exceptions-> render()!!}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.