Elasticsearch:日期范围小于

问题描述 投票:0回答:1

我正在存储这样的房间对象:

{
    "availability": 3,
    "bookings": [
        {
            "booking_interval": {
                "lt": "2024-05-16",
                "gte": "2024-05-09"
            },
            "quantity": 1
        },
        {
            "booking_interval": {
                "lt": "2024-06-10",
                "gte": "2024-06-01"
            },
            "quantity": 1
        },
        {
            "booking_interval": {
                "lt": "2024-06-12",
                "gte": "2024-06-02"
            },
            "quantity": 2
        }
    ]
}

我希望能够搜索一个日期间隔,如果匹配的数量总和小于可用情况,则该房间类型(即双人房)可用。

类似这样的:

{
    "nested": {
        "path": "bookings",
        "query": {
            "bool": {
                "must": [
                    {
                        "range": {
                            "bookings.booking_interval": {
                                "gte": "2024-06-04",
                                "lt": "2024-06-06"
                            }
                        }
                    }
                ]
            }
        }
    }
}

这只匹配间隔。这不应该返回空房情况,因为酒店有 3 间双人间,并且已经有 2 份 1 间和 2 间客房的预订。

elasticsearch nested
1个回答
0
投票

您的请求/需求不清楚,请详细说明并检查以下内容也许会有所帮助。

如果将

sum_quantity
设置为小于或等于 3,则会返回 0 结果,因为数据中最大
quantity
2

PUT /rooms
{
  "mappings": {
    "properties": {
      "availability": {
        "type": "integer"
      },
      "bookings": {
        "type": "nested",
        "properties": {
          "booking_interval": {
            "type": "date_range"
          },
          "quantity": {
            "type": "integer"
          }
        }
      }
    }
  }
}

POST /rooms/_bulk
{"index":{}}
{"availability": 3,"bookings":[{"booking_interval":{"lt":"2024-05-16","gte":"2024-05-09"},"quantity":1},{"booking_interval":{"lt":"2024-06-10","gte":"2024-06-01"},"quantity":1},{"booking_inter    val":{"lt":"2024-06-12","gte":"2024-06-02"},"quantity":2}]}

GET /rooms/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "bookings",
            "query": {
              "bool": {
                "must": [
                  {
                    "range": {
                      "bookings.booking_interval": {
                        "gte": "2024-06-04",
                        "lt": "2024-06-06"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ],
      "filter": {
        "script": {
          "script": {
            "source": "doc['availability'].value > params.sum_quantity",
            "params": {
              "sum_quantity": 2
            }
          }
        }
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.