在Elastic Search中将嵌套查询与布尔查询结合起来

时间:2019-03-20 18:53:55

标签: elasticsearch elasticsearch-nested

我正在尝试在Elastic Search中按价格范围过滤酒店客房。这些房间有默认的每晚价格,还可以为特定日期设置自定义价格。

我要存储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之间,并且在7月5日至7月7日之间没有设置customPrices.date

但是我无法将此逻辑应用于Elastic Search,我猜嵌套对象/查询有点棘手。

这是我提出的最后一个查询:

{
  "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 $的价格范围,但仍然不匹配。

试图使用explain 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": []
          }
        ]
      }
    ]
  }
}

任何帮助或想法都将不胜感激...

1 个答案:

答案 0 :(得分:0)

如果仔细查看第一个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"
                                          }
                                       }
                                    }
                                 }
                              }
                           ]
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}

希望有帮助!

相关问题