是否可以访问排序中的 script_field?
我有
search_body["script_fields"] = {
"test": {
"script": {
"lang": "painless",
"inline": skill_rating_algorithm.replace("\n", ""),
"params" : {
"param1": {....}
}
}
}
}
按顺序:
search_body["sort"] = {
"_script": {
"type": "string",
"order": "desc",
"script": {
"lang": "painless",
"inline": "def c = params._fields['test']; return c;",
}
},
"number_of_years_of_experience": {"order": "desc"},
"value.raw": {"order": "asc"},
}
但这只会返回错误。 我可以做一个排序脚本,但我需要文档中返回的值作为单独的键。
我的问题是: 我可以访问排序中的 script_field“测试”吗? (更多的 script_fields 将会出现,我将需要不同的排序)
是的,可以在排序中访问运行时字段(不是脚本字段)。让我们按月份名称降序对日期进行排序
带日期的文件
PUT /runtime_field_sort/_bulk
{"create":{"_id":1}}
{ "date": "2015-01-01" }
{"create":{"_id":2}}
{ "date": "2015-08-01" }
{"create":{"_id":3}}
{ "date": "2015-12-01" }
使用
runtime_mappings
查询并按月份名称降序排序
GET /runtime_field_sort/_search?filter_path=hits.hits
{
"runtime_mappings": {
"month": {
"type": "keyword",
"script": {
"source": """
ZonedDateTime date = doc[params.date_field].value;
Month month = date.getMonth();
emit(month.toString().toLowerCase());
""",
"params": {
"date_field": "date"
}
}
}
},
"fields": [
"month", "date"
],
"_source": false,
"sort": [
{
"month": {
"order": "desc"
}
}
]
}
回应
{
"hits" : {
"hits" : [
{
"_index" : "runtime_field_sort",
"_type" : "_doc",
"_id" : "1",
"_score" : null,
"fields" : {
"date" : [
"2015-01-01T00:00:00.000Z"
],
"month" : [
"january"
]
},
"sort" : [
"january"
]
},
{
"_index" : "runtime_field_sort",
"_type" : "_doc",
"_id" : "3",
"_score" : null,
"fields" : {
"date" : [
"2015-12-01T00:00:00.000Z"
],
"month" : [
"december"
]
},
"sort" : [
"december"
]
},
{
"_index" : "runtime_field_sort",
"_type" : "_doc",
"_id" : "2",
"_score" : null,
"fields" : {
"date" : [
"2015-08-01T00:00:00.000Z"
],
"month" : [
"august"
]
},
"sort" : [
"august"
]
}
]
}
}