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

发布于 2025-01-18 01:38:51 字数 770 浏览 2 评论 0原文

假设我有这些文档:

[
  {
    "_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" ]
    }
  }
}

由于 id 字段中间有 {RANDOM UUID} 我无法按 < code>{REAL ID}

是否可以删除 some_obj.{RANDOM UUID}: 部分进行排序或按 : 拆分它并使用第二部分进行排序或其他一些能够排序的方式{真实身份}

Let's say I have these documents:

[
  {
    "_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}"
    }
  },
  { ... }
]

I trying to get these objects by using query:

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

Because of {RANDOM UUID} in the middle of id field I can't order by {REAL ID}

Is it possible to remove some_obj.{RANDOM UUID}: part for sorting or split it by : and use second part for sorting or some another way to be able sort by {REAL ID}?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2025-01-25 01:38:51

以下查询应该这样做。我假设id您的映射中的字段是keyword{真实ID}是数字。如果是字符串,则应将查询中的类型更改为string,然后从返回语句中删除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"
      }
    }
  }
}

Following query should do it. I assumed id field in your mapping is keyword and {REAL ID}is numeric. If it is a string, you should change type in the query to string and remove Long.parse from the return statement.

{
  "sort": [
    {
      "_script": {
        "type": "number",
        "script": {
          "lang": "painless",
          "source": """
            return Long.parseLong(doc['id'].value.splitOnToken(':')[1]);
          """
        },
        "order": "asc"
      }
    }
  ],
  "query": {
    "term": {
      "parent_id": {
        "value": "1"
      }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文