[英]Elasticsearch 2.x, query for tag, and sort results by tag weigth
[英]Elasticsearch 2.x sort with missing value treated as 0.0
根據映射,我正在排序的字段是double類型,我期望排序將缺失值視為0,因為我有一些負值,我希望該值會低於缺失值。 (當前缺失低於負值)
我該如何實現?
exists
查詢( https://www.elastic.co/guide/zh-cn/elasticsearch/reference/current/query-dsl-exists-query.html ),您可以將其與function score
查詢中的not
過濾器一起使用( https ://www.elastic.co/guide/zh-CN/elasticsearch/reference/current/query-dsl-function-score-query.html )進行排序,並根據需要將缺失值設為0.0所需的boost / score。
但我建議采用更簡單的解決方案-僅使用等於0.0的字段索引文檔,而不是缺少值,這將使事情變得容易得多-您將能夠對所需字段進行排序。
您似乎在描述丟失或null
值應視為0值。 如果您不需要將它們與實際的“ 0”值區分開,則可以使用null_value
,請參見手冊
如果您在另一種情況下確實必須區分它們,則可能需要添加一個標記或其他內容(因此在第二個字段中)以實際說出“但實際上丟失了”,但這實際上取決於您的用例
一個簡單的例子:
PUT test
{
"mappings": {
"my_type": {
"properties": {
"status_code": {
"type": "integer",
"null_value": 0
}
}
}
}
}
PUT test/my_type/1
{
"status_code": null
}
PUT test/my_type/2
{
"status_code": -1
}
PUT test/my_type/3
{
"status_code": 1
}
GET test/my_type/_search
{
sort" : [
{ "status_code" : "desc" },
]
}
這樣做是為了將單據退還2, 1, 3
預期
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.