是否可以按 Elasticsearch (Opensearch) 中值的子字符串排序?

Is it possible to sort by substring of value in Elasticsearch (Opensearch)?

假设我有这些文件:

[
  {
    "_index" : "index_name",
    "_type" : "_doc",
    "_id" : "some_obj.{RANDOM UUID}:{REAL ID}",
    "_source" : {
      "id" : "some_obj.{RANDOM UUID}:{REAL ID}",
      "parent_id": "{SOME ID}"
      "name" : "{some name}"
    }
  },
  { ... }
]

我试图通过查询获取这些对象:

GET index_name/_search
{
  "sort" : [
    { "id": {"order": "asc"}}
  ],
  "query": {
    "terms": {
      "parent_id": [ "1" ]
    }
  }
}

因为 {RANDOM UUID}id 字段中间,我无法按 {REAL ID}

排序

是否可以删除 some_obj.{RANDOM UUID}: 部分进行排序或按 : 拆分并使用第二部分进行排序或使用其他方式按 {REAL ID} 进行排序?

下面的查询应该可以做到。我假设映射中的 id 字段是 keyword 并且 {REAL ID} 是数字。如果是字符串,则应将查询中的 type 更改为 string 并从 return 语句中删除 Long.parse

{
  "sort": [
    {
      "_script": {
        "type": "number",
        "script": {
          "lang": "painless",
          "source": """
            return Long.parseLong(doc['id'].value.splitOnToken(':')[1]);
          """
        },
        "order": "asc"
      }
    }
  ],
  "query": {
    "term": {
      "parent_id": {
        "value": "1"
      }
    }
  }
}