使用Elasticsearch DSL检索calendarItems.minNights第一个值大于2的文档。

huangapple go评论95阅读模式
英文:

Fetching documents with calendarItems.minNights first value greater than 2 using Elasticsearch DSL

问题

我想学习如何使用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"
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
}
英文:

I would like to learn how to fetch documents with the first value of the calendarItems.minNights field greater than 2 using Elasticsearch DSL. The data structure looks like this:

In the above data structure, I want to retrieve documents where the first value of calendarItems.minNights is greater than 2. What should be the Elasticsearch DSL query for this?

Thank you.

Data

{
  "_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

"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"
											}
										}
									}
								]
							}
						}
					}
				}
			]
		}
	}

答案1

得分: 0

我解决了这个问题。

英文:

I solved this

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

}

huangapple
  • 本文由 发表于 2023年6月19日 15:10:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定