使用开始和结束日期查询以获取可用日期

Query to get available dates using start and end date

我正在尝试创建一个查询,其中 returns 可用产品在该日期(或日期范围)没有预订或根本没有预订。这让我发疯。

这是我当前的索引设置映射:

{
  "development_product_instances" : {
    "aliases" : { },
    "mappings" : {
      "product_instance" : {
        "properties" : {
          "reservations" : {
            "type" : "nested",
            "properties" : {
              "end_date" : {
                "type" : "date",
                "format" : "yyyy-MM-dd"
              },
              "start_date" : {
                "type" : "date",
                "format" : "yyyy-MM-dd"
              }
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1503327829680",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "9b9BhF-ITta2dlCKRLrnfA",
        "version" : {
          "created" : "2040499"
        }
      }
    },
    "warmers" : { }
  }
}

以及查询:

  {
    bool: {
      should: [
        {
          nested: {
            path: "reservations",
            filter: {
              bool: {
                must_not: [
                  {
                    range:
                      {
                        "reservations.start_date":
                         {
                           gte: start_date,
                           lte: end_date
                         }
                      }
                  },
                  {
                    range:
                      {
                        "reservations.end_date":
                         {
                           gte: start_date,
                           lt: end_date
                         }
                      }
                  }
                ]
              }
            }
          }
        },
        {
          not: {
            nested: {
              path: "reservations",
              filter: {
                match_all: {}
              }
            }
          }
        }
      ]
    }
  }

当有多个预订时returns全部。

我希望有人能看到里面的错误。也许我在大局中遗漏了一些东西。

您的问题是 must_notnested 查询中。这意味着如果它匹配 any 的嵌套保留,则父文档匹配。因此,当有多个预订时,除非您查询的范围与 all 现有预订重叠,否则您会得到一个匹配项。你可以这样重写(注意当reservations为空时这个查询也匹配):

{
  "query": {
    "bool": {
      "must_not": {
        "nested": {
          "path": "reservations",
          "query": {
            "bool": {
              "should": [
                {
                  "range": {
                    "reservations.start_date": {
                      "gte": start_date,
                      "lt": end_date
                    }
                  }
                },
                {
                  "range": {
                    "reservations.end_date": {
                      "gte": start_date,
                      "lt": end_date
                    }
                  }
                }, 
                {
                  "bool": {
                    "must": [
                      {
                        "range": {
                           "reservations.start_date": {
                             "lt": start_date
                           }
                        }
                      },
                      {
                        "range": {
                           "reservations.end_date": {
                             "gt": end_date
                           }
                        }
                      }
                    ]
                  } 
                }
              ]
            }
          }
        }
      }
    }
  }
}