1

我正在尝试在弹性搜索中按价格范围过滤酒店房间。房间有默认的每晚价格,也可以为特定日期设置自定义价格。

我将nightlyPrice自定义价格的嵌套对象与日期一起存储。映射是 smt。喜欢:

{
  "adverts": {
    "mappings": {
      "advert": {
        "properties": {
          "nightlyPrice": {"type": "float"},
          "customPrices": {
            "type": "nested",
            "properties": {
              "date": {"type": "date"},
              "price": {"type": "float"}
            }
          }
        }
      }
    }
  }
}

例如,我想在 7 月 1 日至 7 日期间获得价格在 100 美元和 200 美元之间的房间。

所以我想出了这个逻辑:

  1. customPrices.date必须在 2019-07-01 和 2019-07-07之间customPrices.price以及 100 和 200 之间。
  2. 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": []
          }
        ]
      }
    ]
  }
}

非常感谢任何帮助或想法...

4

1 回答 1

3

如果您仔细查看第一个must子句,您似乎没有提到该字段的整个路径。

{  
   "range":{  
      "date":{               <-- must be "customPrices.date"
         "from":"2019-07-01",
         "to":"2019-07-07"
      }
   }
},
{  
   "range":{  
      "price":{             <-- must be "customPrices.price"
         "from":100,
         "to":200
      }
   }
}

以下是查询应该如何并且应该适用于您的用例。

询问

POST <your_index_name>/_search
{  
   "query":{  
      "bool":{  
         "filter":{  
            "term":{  
               "status":"active"
            }
         },
         "must":[  
            {  
               "bool":{  
                  "should":[  
                     {  
                        "bool":{  
                           "must":[  
                              {  
                                 "nested":{  
                                    "path":"customPrices",
                                    "query":{  
                                       "bool":{  
                                          "must":[  
                                             {  
                                                "range":{  
                                                   "customPrices.date":{  
                                                      "gte":"2019-07-01",
                                                      "lte":"2019-07-09"
                                                   }
                                                }
                                             },
                                             {  
                                                "range":{  
                                                   "customPrices.price":{  
                                                      "gte":100,
                                                      "lte":200
                                                   }
                                                }
                                             }
                                          ]
                                       }
                                    }
                                 }
                              }
                           ]
                        }
                     },
                     {  
                        "bool":{  
                           "must":[  
                              {  
                                 "range":{  
                                    "nightlyPrice":{  
                                       "gte":100,
                                       "lte":200
                                    }
                                 }
                              }
                           ],
                           "must_not":[  
                              {  
                                 "nested":{  
                                    "path":"customPrices",
                                    "query":{  
                                       "range":{  
                                          "customPrices.date":{  
                                             "gte":"2019-07-05",
                                             "lte":"2019-07-07"
                                          }
                                       }
                                    }
                                 }
                              }
                           ]
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}

希望能帮助到你!

于 2019-03-20T20:19:59.170 回答