使用Elasticsearch DSL获取calendarItems.minNights的文档第一个值大于2

mfpqipee  于 2023-06-29  发布在  ElasticSearch
关注(0)|答案(1)|浏览(101)

我想学习如何使用Elasticsearch DSL获取calendarItems.minNights字段的第一个值大于2的文档。数据结构如下所示:
在上面的数据结构中,我希望检索calendarItems.minNights的第一个值大于2的文档。对此,Elasticsearch DSL查询应该是什么?
谢谢你。
数据

{
  "_index": "list",
  "_id": "5150",
  "_version": 1,
  "_seq_no": 0,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "id": 5150,
    "title": "test title",
    "calendarItems": [
      {
        "actualDate": "2023-07-10T00:00:00+03:00",
        "price": 458,
        "minNights": 4,
        "status": "booked",
        "reason": null,
        "isBlock": null
      },
      {
        "actualDate": "2023-07-11T00:00:00+03:00",
        "price": 458,
        "minNights": 2,
        "status": "available",
        "reason": null,
        "isBlock": null
      },
      {
        "actualDate": "2023-07-12T00:00:00+03:00",
        "price": 458,
        "minNights": 2,
        "status": "available",
        "reason": null,
        "isBlock": null
      },
      {
        "actualDate": "2023-07-12T00:00:00+03:00",
        "price": 458,
        "minNights": 2,
        "status": "booked",
        "reason": null,
        "isBlock": null
      }
    ]
  }
}

查询

"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": "2 >= doc['calendarItems.minNights'].value",
                                                "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"
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }
k2fxgqgv

k2fxgqgv1#

我解决了这个

{
"query": {
    "bool": {
        "must": [
            {
                "nested": {
                    "path": "calendarItems",
                    "query": {
                        "bool": {
                            "must": [
                                {
                                    "range": {
                                        "calendarItems.actualDate": {
                                            "gte": "2023-07-07T00:00:00+03:00",
                                            "lte": "2023-07-15T23:59:59+03:00"
                                        }
                                    }
                                },
                                {
                                    "term": {
                                        "calendarItems.status": "available"
                                    }
                                }
                            ]
                        }
                    }
                }
            },
            {
                "nested": {
                    "path": "calendarItems",
                    "query": {
                        "bool": {
                            "must": [
                                {
                                    "range": {
                                        "calendarItems.actualDate": {
                                            "gte": "2023-07-07T00:00:00+03:00",
                                            "lte": "2023-07-07T23:59:59+03:00"
                                        }
                                    }
                                },
                                {
                                    "term": {
                                        "calendarItems.status": "available"
                                    }
                                },
                                {
                                    "script": {
                                        "script": {
                                            "source": "8 >= doc['calendarItems.minNights'].value",
                                            "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-15T23:59:59+03:00"
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
}

}

相关问题