Elasticsearch:使用处理器对数组进行排序
2023年2月12日 | by mebius
如果你想知道是否可以对数组进行排序,答案是肯定的。使用 sort 处理器,我们可以按升序或降序对元素数组进行排序。对数组的元素进行升序或降序排序。 同构数字数组将按数字排序,而字符串数组或字符串+数字的异构数组将按字典顺序排序。 当字段不是数组时抛出错误。
让我们运行两个示例:字符串数组和数值数组。
数组字符串
在 array_field_to_sort 字段中,我们有值 amber、simon、back,我们将按升序排序以获得排序:amber”、back、simon。
POST /_ingest/pipeline/_simulate?verbose=true
{
"tgcodepipeline": {
"description": "sort array string",
"processors": [
{
"sort": {
"field": "array_field_to_sort",
tgcode "order": "asc"
}
}
]
},
"docs": [
{
"_index": "index",
"_id": "id",
"_source": {
"array_field_to_sort": [
"amber", "simon", "back"
]
}
}
]
}
上述命令的输出为:
{
"docs": [
{
"processor_results": [
{
"processor_type": "sort",
"status": "success",
"doc": {
"_index": "index",
"_id": "id",
"_version": "-3",
"_source": {
"array_field_to_sort": [
"amber",
"back",
"simon"
]
},
"_ingest": {
"pipeline": "_simulate_pipeline",
"timestamp": "2023-02-03T07:51:04.618494501Z"
}
}
}
]
}
tgcode ]
}
从输出中,我们可以看出来字符串是安装升序来进行排列的。
数值数组
现在让我们使用一个内部数组。 元素 5、2、1、12 ,20 将按 1、2、5、12 ,20 的升序排序。
POST /_ingest/pipeline/_simulate?verbose=true
{
"pipeline": {
"description": "sort array numeric",
"processors": [
{
"sort": {
"field": "array_field_to_sort",
"order": "asc"
}
}
]
},
"docs": [
{
"_index": "index",
"_id": "id",
"_source": {
"array_field_to_sort": [
5,
2,
1,
12,
20
]
}
}
]
}
上述命令的输出为:
{
"docs": [
{
"processor_results": [
{
"processor_type": "sort",
"status": "success",
"doc": {
"_index": "index",
"_id": "id",
"_version": "-3",
"_source": {
"array_field_to_sort": [
1,
2,
5,
12,
20
]
},
"_ingest": {
"pipeline": "_simulate_pipeline",
"timestamp": "2023-02-03T07:53:13.517706963Z"
}
}
}
]
}
]
}
文章来源于互联网:Elasticsearch:使用处理器对数组进行排序
相关推荐: Kibana:圣诞老人使用 Kibana Dashboards 驾驶他的雪橇!
又到了每年一度的圣诞节了。圣诞老人今年开始向各个国家发放礼物了。他驾驶着自己的雪橇挨个国家发放礼物了。我们收集了去世界过个国家的首都的地理位置信息,并按照一定的顺序来发放礼物。我们可以轻松地使用 Elastic Stack 中的 Kibana 来创建一个 Ma…