我正在尝试在弹性搜索中按价格范围过滤酒店房间。房间有默认的每晚价格,也可以为特定日期设置自定义价格。
我将nightlyPrice
自定义价格的嵌套对象与日期一起存储。映射是 smt。喜欢:
{
"adverts": {
"mappings": {
"advert": {
"properties": {
"nightlyPrice": {"type": "float"},
"customPrices": {
"type": "nested",
"properties": {
"date": {"type": "date"},
"price": {"type": "float"}
}
}
}
}
}
}
}
例如,我想在 7 月 1 日至 7 日期间获得价格在 100 美元和 200 美元之间的房间。
所以我想出了这个逻辑:
customPrices.date
必须在 2019-07-01 和 2019-07-07之间customPrices.price
以及 100 和 200 之间。- 或
nightlyPrice
必须在 100 到 200 之间,并且 nocustomPrices.date
设置在 7 月 5 日到 7 日之间。
但是我无法将此逻辑应用于弹性搜索,我猜嵌套对象/查询有点棘手。
这是我提出的最后一个查询:
{
"query": {
"bool": {
"filter": [
{
"term": {
"status": "active"
}
}
],
"must": [
{
"bool": {
"should": [
{
"nested": {
"path": "customPrices",
"query": {
"bool": {
"must": [
{
"range": {
"date": {
"from": "2019-07-01",
"to": "2019-07-07"
}
}
},
{
"range": {
"price": {
"from": 100,
"to": 200
}
}
}
]
}
}
}
},
{
"bool": {
"must": [
{
"range": {
"nightlyPrice": {
"from": 100,
"to": 200
}
}
}
],
"must_not": [
{
"nested": {
"path": "customPrices",
"query": {
"range": {
"customPrices.date": {
"from": "2019-07-01",
"to": "2019-07-07"
}
}
}
}
}
]
}
}
]
}
}
]
}
}
}
此查询的问题是,如果 customPrices.date 匹配日期范围,则无论价格范围如何,它都不会与文档匹配。我尝试了 1 - 100000 美元的价格范围,但仍然不匹配。
试图使用解释 API 来理解为什么特定文档不匹配但我不明白,它说user requested match_none
查询但有这个should
查询所以它应该匹配嵌套查询(第一个):
{
"_index": "adverts",
"_type": "advert",
"_id": "13867",
"matched": false,
"explanation": {
"value": 0.0,
"description": "Failure to meet condition(s) of required/prohibited clause(s)",
"details": [
{
"value": 0.0,
"description": "no match on required clause (+(ToParentBlockJoinQuery (MatchNoDocsQuery(\"User requested \"match_none\" query.\")) (+nightlyPrice:[100.0 TO 200.0] -ToParentBlockJoinQuery (customListingPrices.date:[1561939200000 TO 1562543999999]))) #status:active",
"details": [
{
"value": 0.0,
"description": "Failure to meet condition(s) of required/prohibited clause(s)",
"details": [
{
"value": 0.0,
"description": "no match on required clause (ToParentBlockJoinQuery (MatchNoDocsQuery(\"User requested \"match_none\" query.\")) (+nightlyPrice:[100.0 TO 200.0] -ToParentBlockJoinQuery (customListingPrices.date:[1561939200000 TO 1562543999999])))",
"details": [
{
"value": 0.0,
"description": "No matching clauses",
"details": []
}
]
},
{
"value": 0.0,
"description": "match on required clause, product of:",
"details": [
{
"value": 0.0,
"description": "# clause",
"details": []
},
{
"value": 1.0,
"description": "status:active",
"details": []
}
]
}
]
}
]
},
{
"value": 0.0,
"description": "match on required clause, product of:",
"details": [
{
"value": 0.0,
"description": "# clause",
"details": []
},
{
"value": 1.0,
"description": "DocValuesFieldExistsQuery [field=_primary_term]",
"details": []
}
]
}
]
}
}
非常感谢任何帮助或想法...