[英]How to combine bool must and sort for elasticsearch
i am trying to sort some data, where in my base skeleton my sorting is not working and if i remove the sorting it works fine. 我正在尝试对一些数据进行排序,在我的基本骨架中,我的排序不起作用,如果我删除了排序,它会很好地工作。
So how can i put sorting in my base skeleton and sort some data. 所以我该如何在我的基本框架中进行排序并对一些数据进行排序。
i can't put just 我不能放
$params['body'] = [
'sort' => [['title' => ['order' => 'asc']]]];
$results = $client->search($params);
Because i have other condition where i need the must condition. 因为我有其他条件,我需要必须条件。 Can anyone knows how it can be solve. 有谁知道如何解决。
Any advice will be really appreciate. 任何建议将不胜感激。
// my base skeleton
$params = array(
'index' => "myIndex",
'type' => "myType",
'body' => array(
'query' => array(
'bool' => array(
'must' => array(
// empty should clause for starters
)
)
),
'sort' => array()
)
);
// sorting is not working with bool and must
if ($request->request->get('salarySort')) {
$params['body']['query']['bool']['must'][] = array(
'sort' => array(
"title" => array('order' => 'asc')
)
);
}
this is what i get as a json_encode --- 这就是我作为json_encode得到的-
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1066,
"max_score": null,
"hits": [
{
"_index": "myIndex",
"_type": "myType",
"_id": "pe065319de73937aa6ef46413afd7aac26a58a611",
"_score": null,
"_source": {
"title": "Smarason trycker ",
"content": "HIF gör 2-0 mot Halmstad.",
"tag": [
"Soprts"
],
"category": [
"Sports"
]
},
"sort": [
"0"
]
},
{
"_index": "myIndex",
"_type": "myType",
"_id": "pebc44a70008f53f74f23ab23f8a1f79b2b729448",
"_score": null,
"_source": {
"title": "Anders Svenssons tips gav 1-0",
"content": "Anders Svenssons tips i halvtid Kalmar FF.",
"source": "Unknown",
"tag": [
"Soprts"
],
"category": [
"Sports"
]
},
"sort": [
"0"
]
}
]
}
}
query in JSON --- JSON查询-
{
"index": "myIndex",
"type": "myType",
"size": 30,
"body": {
"query": {
"match_all": []
},
"sort": [
{
"title": "asc"
}
]
}
}
You're almost there. 你快到了。 You've correctly placed the empty sort
array at the same level as your query
, which is correct. 您已将空的sort
数组正确放置在与query
相同的级别,这是正确的。
The issue comes later when you try to feed it as a bool/must
constraint instead of in the empty sort
array. 稍后当您尝试将其作为bool/must
约束而不是空的sort
数组来提供时,就会出现此问题。
// sorting is not working with bool and must
if ($request->request->get('salarySort')) {
$params['body']['sort'][] = array( <---- this line needs to be changed
"Salary" => 'asc' <---- this line needs to be changed, too
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.