ORDER BY 多个值在特定顺序弹性搜索
ORDER BY multiple values in specific order elastic search
我有以下弹性搜索文档结构,客户类型为学生、教师、专业我希望我的搜索结果按学生、教师、专业的顺序排列,即在搜索结果中所有学生的之后是所有教师的然后是所有专业的.
{ "_index": "customer", "_type": "info", "_id": "AWC1xn90RETWF5jlQtczjt", "_version": 1, "_score": 1, "_source": { "name": "name1", "type":"student" } }
{ "_index": "customer", "_type": "info", "_id": "AWC1xnRE23TWF5jlQtczjt", "_version": 1, "_score": 1, "_source": { "name": "name4", "type":"teacher" } }
{ "_index": "customer", "_type": "info", "_id": "AWC1xnRETWF545jlQtczjt", "_version": 1, "_score": 1, "_source": { "name": "name3", "type":"professional" } }
我会通过在索引时为每种类型分配数值来解决这个问题。
所以 student=0,teacher=1,professional=2 并将其命名为 type_id 并按此排序。
但是,如果没有这个选项,您可以使用脚本字段
sort: [
{
_script: {
type: 'number',
script: {
lang: 'painless',
inline: 'switch(doc['type'].value){ case "student" return 0; case "teacher" return 1; case "professional" default return -1}',
},
order: 'desc',
},
}
]
我有以下弹性搜索文档结构,客户类型为学生、教师、专业我希望我的搜索结果按学生、教师、专业的顺序排列,即在搜索结果中所有学生的之后是所有教师的然后是所有专业的.
{ "_index": "customer", "_type": "info", "_id": "AWC1xn90RETWF5jlQtczjt", "_version": 1, "_score": 1, "_source": { "name": "name1", "type":"student" } }
{ "_index": "customer", "_type": "info", "_id": "AWC1xnRE23TWF5jlQtczjt", "_version": 1, "_score": 1, "_source": { "name": "name4", "type":"teacher" } }
{ "_index": "customer", "_type": "info", "_id": "AWC1xnRETWF545jlQtczjt", "_version": 1, "_score": 1, "_source": { "name": "name3", "type":"professional" } }
我会通过在索引时为每种类型分配数值来解决这个问题。 所以 student=0,teacher=1,professional=2 并将其命名为 type_id 并按此排序。
但是,如果没有这个选项,您可以使用脚本字段
sort: [
{
_script: {
type: 'number',
script: {
lang: 'painless',
inline: 'switch(doc['type'].value){ case "student" return 0; case "teacher" return 1; case "professional" default return -1}',
},
order: 'desc',
},
}
]