You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Elasticsearch DSL获取calendarItems.minNights首值>2的文档

解决方案

要检索calendarItems数组第一个元素的minNights值大于2的文档,需根据calendarItems的字段类型(普通对象数组/nested类型)选择对应的查询方式:

情况1:calendarItems为普通对象数组(非nested类型)

直接通过脚本访问原始文档的_source数组,定位第一个元素并判断条件:

{
  "query": {
    "bool": {
      "must": [
        {
          "script": {
            "script": {
              "source": "params._source.calendarItems[0].minNights > 2",
              "lang": "painless"
            }
          }
        }
      ]
    }
  }
}

情况2:calendarItems为nested类型

从你提供的参考查询来看,calendarItems大概率是nested类型。此时需要结合nested查询,通过_nested.position获取元素在数组中的索引(从0开始),定位第一个元素并判断minNights条件:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "calendarItems",
            "query": {
              "bool": {
                "must": [
                  {
                    "script": {
                      "script": {
                        "source": "_nested.position == 0 && doc['calendarItems.minNights'].value > 2",
                        "lang": "painless"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

结合你现有查询的调整版本

如果要在你当前的查询逻辑基础上,添加“第一个元素minNights>2”的条件,可修改nested查询内的脚本部分:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "calendarItems",
            "query": {
              "bool": {
                "must": [
                  {
                    "range": {
                      "calendarItems.actualDate": {
                        "gte": "2023-07-07T00:00:00+03:00",
                        "lte": "2023-07-12T23:59:59+03:00"
                      }
                    }
                  },
                  {
                    "term": {
                      "calendarItems.status": "available"
                    }
                  },
                  {
                    "script": {
                      "script": {
                        "source": "_nested.position == 0 && doc['calendarItems.minNights'].value > 2",
                        "lang": "painless"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ],
      "must_not": [
        {
          "nested": {
            "path": "calendarItems",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "calendarItems.status": "booked"
                    }
                  },
                  {
                    "range": {
                      "calendarItems.actualDate": {
                        "gte": "2023-07-07T00:00:00+03:00",
                        "lte": "2023-07-12T23:59:59+03:00"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

关键说明

  • _nested.position:仅在nested查询的脚本中生效,用于获取当前nested元素在原始数组中的索引,通过_nested.position == 0可精准定位数组第一个元素。
  • 普通数组无需nested查询,直接访问params._source.calendarItems[0]即可获取第一个元素。

内容的提问来源于stack exchange,提问作者Engin Kartal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 14:54:52