elasticsearch - 弹性搜索按布尔字段排序
问题描述
我想在一个名为trusted的字段中按真实值对我的列表进行排序。
我发现排序选项不支持布尔排序。
我怎样才能做到这一点?
解决方案
如果我很好地理解了您的问题,我尝试在 ES 版本 7.8 上进行本地测试,并在我的索引中摄取了以下数据:
“内容”:“这是一个测试”,“可信”:真
"content": "这是一个新的测试", "trusted": true
“内容”:“这不是测试”,“可信”:假
这是索引的映射:
"mappings" : {
"properties" : {
"content" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"trusted" : {
"type" : "boolean"
}
}
}
这是何时查询"order" : "desc"
:
{
"sort": [
{
"trusted": {
"order": "desc"
}
}
]
}
响应:
"hits" : [
{
"_index" : "boolean-sorting",
"_type" : "_doc",
"_id" : "B-YleHQBsTCl1BZvrFdA",
"_score" : null,
"_source" : {
"content" : "This is a test",
"trusted" : true
},
"sort" : [
1
]
},
{
"_index" : "boolean-sorting",
"_type" : "_doc",
"_id" : "CeYleHQBsTCl1BZvtFdJ",
"_score" : null,
"_source" : {
"content" : "This is a new test",
"trusted" : true
},
"sort" : [
1
]
},
{
"_index" : "boolean-sorting",
"_type" : "_doc",
"_id" : "DOYleHQBsTCl1BZvvVfl",
"_score" : null,
"_source" : {
"content" : "This is not a test",
"trusted" : false
},
"sort" : [
0
]
}
]
当 时"order":"asc"
,响应为:
"hits" : [
{
"_index" : "boolean-sorting",
"_type" : "_doc",
"_id" : "DOYleHQBsTCl1BZvvVfl",
"_score" : null,
"_source" : {
"content" : "This is not a test",
"trusted" : false
},
"sort" : [
0
]
},
{
"_index" : "boolean-sorting",
"_type" : "_doc",
"_id" : "B-YleHQBsTCl1BZvrFdA",
"_score" : null,
"_source" : {
"content" : "This is a test",
"trusted" : true
},
"sort" : [
1
]
},
{
"_index" : "boolean-sorting",
"_type" : "_doc",
"_id" : "CeYleHQBsTCl1BZvtFdJ",
"_score" : null,
"_source" : {
"content" : "This is a new test",
"trusted" : true
},
"sort" : [
1
]
}
]
链接: https ://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html
请让我知道如果我回答错误,我很乐意提供帮助。
推荐阅读
- java - 存储的drawable在Android中发生了变化
- clojure - 如何使用 lein exec 要求 clojure.java.io?
- python-3.x - IDLE 中的光标位置
- c++ - 提示用户输入一个单词,然后检查它是否有双字符
- javascript - 在视图和 JS 之间传递变量;姜戈
- sql - 如何在 WHERE 子句中将 SQL OR 运算符与 IN SELECT 子查询一起使用?
- android - vs代码中的扩展不加载
- full-text-search - 电报组过滤
- ffmpeg - 使用 ffmpeg 为 Cloud Speech-to-Text 录制流音频
- php - Laravel Passport API 上传多张图片