0

我有一个弹性搜索索引,它有一个名为可用性的嵌套对象,它有一个日期和一个布尔字段,映射模式如下

    {
       "hotel_nested" : {
        "mappings" : {
          "doc" : {
            "properties" : {
               "amenities" : {
                   "type" : "text",
                   "fields" : {
                   "keyword" : {
                        "type" : "keyword",
                        "ignore_above" : 256
                         }     
                     }
               },
              "availability" : {
                    "type" : "nested",
                     "properties" : {
                            "date" : {
                               "type" : "date",
                               "fields" : {
                               "keyword" : {
                                 "type" : "keyword",
                                 "ignore_above" : 256
                               }
                           },
                            "format" : "MM/dd/yyyy HH:mm:ss||MM/dd/yyyy||epoch_millis"
          },
                          "status" : {
                              "type" : "boolean"
                            }
        }
      },
      "dailyRate" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      },
      "destination" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      },
      "hotelName" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      },
      "location" : {
        "properties" : {
          "lat" : {
            "type" : "float"
          },
          "lon" : {
            "type" : "float"
          }
        }
      },
      "maxOccupancy" : {
        "type" : "long"
      },
      "propertyType" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      },
      "roomType" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      },
      "starRating" : {
        "type" : "float"
         }
       }
     }
   }
 }
}

这是其中一个对象

    {
    "_index" : "hotel_nested",
    "_type" : "doc",
    "_id" : "14",
    "_score" : 1.0,
    "_source" : {
      "roomType" : "Executive",
      "destination" : "Albuquerque",
      "maxOccupancy" : 4,
      "starRating" : 6.8,
      "hotelName" : "Lotte Hotel Seoul",
      "amenities" : [
        "Spa",
        "Internet",
        "Free parking",
        "Air conditioning",
        "Laundry Service",
        "Business Services"
      ],
      "location" : {
        "lat" : 56.76755,
        "lon" : -110.75792
      },
      "propertyType" : "Hotel",
      "dailyRate" : "$178.96",
      "availability" : [
        {
          "date" : "10/1/2017",
          "status" : true
        },
        {
          "date" : "10/2/2017",
          "status" : true
        },
        {
          "date" : "10/3/2017",
          "status" : true
        },
        {
          "date" : "10/4/2017",
          "status" : false
        },
        {
          "date" : "10/5/2017",
          "status" : false
        },
        {
          "date" : "10/6/2017",
          "status" : false
        },
        {
          "date" : "10/7/2017",
          "status" : true
        },
        {
          "date" : "10/8/2017",
          "status" : true
        },
        {
          "date" : "10/9/2017",
          "status" : false
        },
        {
          "date" : "10/10/2017",
          "status" : false
        },
        {
          "date" : "10/11/2017",
          "status" : true
        },
        {
          "date" : "10/12/2017",
          "status" : false
        },
        {
          "date" : "10/13/2017",
          "status" : false
        },
        {
          "date" : "10/14/2017",
          "status" : true
        },
        {
          "date" : "10/15/2017",
          "status" : true
        },
        {
          "date" : "10/16/2017",
          "status" : true
        },
        {
          "date" : "10/17/2017",
          "status" : false
        },
        {
          "date" : "10/18/2017",
          "status" : true
        },
        {
          "date" : "10/19/2017",
          "status" : false
        },
        {
          "date" : "10/20/2017",
          "status" : true
        },
        {
          "date" : "10/21/2017",
          "status" : false
        },
        {
          "date" : "10/22/2017",
          "status" : true
        },
        {
          "date" : "10/23/2017",
          "status" : true
        },
        {
          "date" : "10/24/2017",
          "status" : true
        },
        {
          "date" : "10/25/2017",
          "status" : false
        },
        {
          "date" : "10/26/2017",
          "status" : false
        },
        {
          "date" : "10/27/2017",
          "status" : false
        },
        {
          "date" : "10/28/2017",
          "status" : true
        },
        {
          "date" : "10/29/2017",
          "status" : true
        },
        {
          "date" : "10/30/2017",
          "status" : true
        },
        {
          "date" : "10/31/2017",
          "status" : true
        },
        {
          "date" : "11/1/2017",
          "status" : true
        },
        {
          "date" : "11/2/2017",
          "status" : false
        },
        {
          "date" : "11/3/2017",
          "status" : false
        },
        {
          "date" : "11/4/2017",
          "status" : false
        },
        {
          "date" : "11/5/2017",
          "status" : false
        },
        {
          "date" : "11/6/2017",
          "status" : true
        },
        {
          "date" : "11/7/2017",
          "status" : false
        },
        {
          "date" : "11/8/2017",
          "status" : false
        },
        {
          "date" : "11/9/2017",
          "status" : false
        },
        {
          "date" : "11/10/2017",
          "status" : false
        },
        {
          "date" : "11/11/2017",
          "status" : false
        },
        {
          "date" : "11/12/2017",
          "status" : false
        },
        {
          "date" : "11/13/2017",
          "status" : false
        },
        {
          "date" : "11/14/2017",
          "status" : true
        },
        {
          "date" : "11/15/2017",
          "status" : true
        },
        {
          "date" : "11/16/2017",
          "status" : false
        },
        {
          "date" : "11/17/2017",
          "status" : true
        },
        {
          "date" : "11/18/2017",
          "status" : false
        },
        {
          "date" : "11/19/2017",
          "status" : true
        },
        {
          "date" : "11/20/2017",
          "status" : true
        },
        {
          "date" : "11/21/2017",
          "status" : true
        },
        {
          "date" : "11/22/2017",
          "status" : true
        },
        {
          "date" : "11/23/2017",
          "status" : false
        },
        {
          "date" : "11/24/2017",
          "status" : false
        },
        {
          "date" : "11/25/2017",
          "status" : false
        },
        {
          "date" : "11/26/2017",
          "status" : true
        },
        {
          "date" : "11/27/2017",
          "status" : true
        },
        {
          "date" : "11/28/2017",
          "status" : false
        },
        {
          "date" : "11/29/2017",
          "status" : false
        },
        {
          "date" : "11/30/2017",
          "status" : false
        },
        {
          "date" : "12/1/2017",
          "status" : true
        },
        {
          "date" : "12/2/2017",
          "status" : true
        },
        {
          "date" : "12/3/2017",
          "status" : true
        },
        {
          "date" : "12/4/2017",
          "status" : true
        },
        {
          "date" : "12/5/2017",
          "status" : true
        },
        {
          "date" : "12/6/2017",
          "status" : true
        },
        {
          "date" : "12/7/2017",
          "status" : false
        },
        {
          "date" : "12/8/2017",
          "status" : false
        },
        {
          "date" : "12/9/2017",
          "status" : true
        },
        {
          "date" : "12/10/2017",
          "status" : false
        },
        {
          "date" : "12/11/2017",
          "status" : true
        },
        {
          "date" : "12/12/2017",
          "status" : false
        },
        {
          "date" : "12/13/2017",
          "status" : true
        },
        {
          "date" : "12/14/2017",
          "status" : true
        },
        {
          "date" : "12/15/2017",
          "status" : true
        },
        {
          "date" : "12/16/2017",
          "status" : false
        },
        {
          "date" : "12/17/2017",
          "status" : true
        },
        {
          "date" : "12/18/2017",
          "status" : true
        },
        {
          "date" : "12/19/2017",
          "status" : false
        },
        {
          "date" : "12/20/2017",
          "status" : true
        },
        {
          "date" : "12/21/2017",
          "status" : true
        },
        {
          "date" : "12/22/2017",
          "status" : false
        },
        {
          "date" : "12/23/2017",
          "status" : false
        },
        {
          "date" : "12/24/2017",
          "status" : false
        },
        {
          "date" : "12/25/2017",
          "status" : false
        },
        {
          "date" : "12/26/2017",
          "status" : false
        },
        {
          "date" : "12/27/2017",
          "status" : false
        },
        {
          "date" : "12/28/2017",
          "status" : false
        },
        {
          "date" : "12/29/2017",
          "status" : false
        },
        {
          "date" : "12/30/2017",
          "status" : true
        },
        {
          "date" : "12/31/2017",
          "status" : true
        }
      ]
    }
  }

我的问题是我想在两个日期之间进行搜索,并找出两个日期之间的所有日期的酒店可用性状态是否为真,并且在粗略的水平上也匹配城市名称,我还有其他搜索条件,我认为我可以处理,但是找到两个日期之间所有日期的真实状态让我很烦恼

例如搜索查询参数城市阿尔伯克基,开始日期 10/22/2017 和结束日期 10/24/2017

我创建了一个查询,它给出了不想要的结果,我的查询是

        {
         "query": {
         "bool": {
            "must":[ 
        {
                 "match": {
                      "destination": { 
                            "query":    "Albuquerque" ,
                            "operator": "and"
                         }
                  }
            }

        ],
        "filter": {
              "bool": { 
                 "must": [  {
                "nested": {
                    "path": "availability", 
                        "query": {
                           "bool": {
                             "must": [ 
                                  {
                                 "range": {
                                     "availability.date":{ "gte": "10/22/2017",
                                        "lte":"10/24/2017"}
                }
              },
              {
                "term": {
                  "availability.status": "true"
                }
              }
            ]
          }
        }
      }
    }
            ]
      }
     }
   }
 }
 }

这个查询的第一个命中是

        {
            "_index": "hotel_nested",
            "_type": "doc",
            "_id": "305",
            "_score": 4.815987,
            "_source": {
                "roomType": "Executive",
                "destination": "Albuquerque",
                "maxOccupancy": 2,
                "starRating": 4.2,
                "hotelName": "Sheraton San Diego Hotel & Marina",
                "amenities": [
                    "Kitchen",
                    "Air conditioning",
                    "Laundry Service",
                    "Business Services",
                    "Free parking",
                    "Spa"
                ],
                "location": {
                    "lat": 54.92887,
                    "lon": -101.31256
                },
                "propertyType": "Hotel",
                "dailyRate": "$462.59",
                "availability": [
                    {
                        "date": "10/1/2017",
                        "status": false
                    },
                    {
                        "date": "10/2/2017",
                        "status": true
                    },
                    {
                        "date": "10/3/2017",
                        "status": false
                    },
                    {
                        "date": "10/4/2017",
                        "status": true
                    },
                    {
                        "date": "10/5/2017",
                        "status": true
                    },
                    {
                        "date": "10/6/2017",
                        "status": true
                    },
                    {
                        "date": "10/7/2017",
                        "status": true
                    },
                    {
                        "date": "10/8/2017",
                        "status": false
                    },
                    {
                        "date": "10/9/2017",
                        "status": false
                    },
                    {
                        "date": "10/10/2017",
                        "status": true
                    },
                    {
                        "date": "10/11/2017",
                        "status": true
                    },
                    {
                        "date": "10/12/2017",
                        "status": false
                    },
                    {
                        "date": "10/13/2017",
                        "status": true
                    },
                    {
                        "date": "10/14/2017",
                        "status": false
                    },
                    {
                        "date": "10/15/2017",
                        "status": true
                    },
                    {
                        "date": "10/16/2017",
                        "status": false
                    },
                    {
                        "date": "10/17/2017",
                        "status": true
                    },
                    {
                        "date": "10/18/2017",
                        "status": false
                    },
                    {
                        "date": "10/19/2017",
                        "status": false
                    },
                    {
                        "date": "10/20/2017",
                        "status": true
                    },
                    {
                        "date": "10/21/2017",
                        "status": true
                    },
                    {
                        "date": "10/22/2017",
                        "status": true
                    },
                    {
                        "date": "10/23/2017",
                        "status": false
                    },
                    {
                        "date": "10/24/2017",
                        "status": false
                    },
                    {
                        "date": "10/25/2017",
                        "status": false
                    },
                    {
                        "date": "10/26/2017",
                        "status": true
                    },
                    {
                        "date": "10/27/2017",
                        "status": true
                    },
                    {
                        "date": "10/28/2017",
                        "status": false
                    },
                    {
                        "date": "10/29/2017",
                        "status": true
                    },
                    {
                        "date": "10/30/2017",
                        "status": false
                    },
                    {
                        "date": "10/31/2017",
                        "status": false
                    },
                    {
                        "date": "11/1/2017",
                        "status": false
                    },
                    {
                        "date": "11/2/2017",
                        "status": false
                    },
                    {
                        "date": "11/3/2017",
                        "status": false
                    },
                    {
                        "date": "11/4/2017",
                        "status": false
                    },
                    {
                        "date": "11/5/2017",
                        "status": false
                    },
                    {
                        "date": "11/6/2017",
                        "status": true
                    },
                    {
                        "date": "11/7/2017",
                        "status": false
                    },
                    {
                        "date": "11/8/2017",
                        "status": true
                    },
                    {
                        "date": "11/9/2017",
                        "status": true
                    },
                    {
                        "date": "11/10/2017",
                        "status": true
                    },
                    {
                        "date": "11/11/2017",
                        "status": true
                    },
                    {
                        "date": "11/12/2017",
                        "status": false
                    },
                    {
                        "date": "11/13/2017",
                        "status": true
                    },
                    {
                        "date": "11/14/2017",
                        "status": false
                    },
                    {
                        "date": "11/15/2017",
                        "status": true
                    },
                    {
                        "date": "11/16/2017",
                        "status": true
                    },
                    {
                        "date": "11/17/2017",
                        "status": false
                    },
                    {
                        "date": "11/18/2017",
                        "status": true
                    },
                    {
                        "date": "11/19/2017",
                        "status": false
                    },
                    {
                        "date": "11/20/2017",
                        "status": true
                    },
                    {
                        "date": "11/21/2017",
                        "status": false
                    },
                    {
                        "date": "11/22/2017",
                        "status": false
                    },
                    {
                        "date": "11/23/2017",
                        "status": false
                    },
                    {
                        "date": "11/24/2017",
                        "status": false
                    },
                    {
                        "date": "11/25/2017",
                        "status": false
                    },
                    {
                        "date": "11/26/2017",
                        "status": false
                    },
                    {
                        "date": "11/27/2017",
                        "status": false
                    },
                    {
                        "date": "11/28/2017",
                        "status": false
                    },
                    {
                        "date": "11/29/2017",
                        "status": false
                    },
                    {
                        "date": "11/30/2017",
                        "status": true
                    },
                    {
                        "date": "12/1/2017",
                        "status": false
                    },
                    {
                        "date": "12/2/2017",
                        "status": false
                    },
                    {
                        "date": "12/3/2017",
                        "status": false
                    },
                    {
                        "date": "12/4/2017",
                        "status": true
                    },
                    {
                        "date": "12/5/2017",
                        "status": true
                    },
                    {
                        "date": "12/6/2017",
                        "status": false
                    },
                    {
                        "date": "12/7/2017",
                        "status": true
                    },
                    {
                        "date": "12/8/2017",
                        "status": false
                    },
                    {
                        "date": "12/9/2017",
                        "status": true
                    },
                    {
                        "date": "12/10/2017",
                        "status": true
                    },
                    {
                        "date": "12/11/2017",
                        "status": false
                    },
                    {
                        "date": "12/12/2017",
                        "status": true
                    },
                    {
                        "date": "12/13/2017",
                        "status": false
                    },
                    {
                        "date": "12/14/2017",
                        "status": true
                    },
                    {
                        "date": "12/15/2017",
                        "status": false
                    },
                    {
                        "date": "12/16/2017",
                        "status": true
                    },
                    {
                        "date": "12/17/2017",
                        "status": true
                    },
                    {
                        "date": "12/18/2017",
                        "status": false
                    },
                    {
                        "date": "12/19/2017",
                        "status": false
                    },
                    {
                        "date": "12/20/2017",
                        "status": false
                    },
                    {
                        "date": "12/21/2017",
                        "status": true
                    },
                    {
                        "date": "12/22/2017",
                        "status": true
                    },
                    {
                        "date": "12/23/2017",
                        "status": false
                    },
                    {
                        "date": "12/24/2017",
                        "status": false
                    },
                    {
                        "date": "12/25/2017",
                        "status": true
                    },
                    {
                        "date": "12/26/2017",
                        "status": false
                    },
                    {
                        "date": "12/27/2017",
                        "status": true
                    },
                    {
                        "date": "12/28/2017",
                        "status": false
                    },
                    {
                        "date": "12/29/2017",
                        "status": false
                    },
                    {
                        "date": "12/30/2017",
                        "status": true
                    },
                    {
                        "date": "12/31/2017",
                        "status": false
                    }
                ]
            }
        }

在 2017 年 10 月 23 日和 2017 年 10 月 24 日的命中状态中,有人可以指导我正确的方向,或者给出一个示例搜索查询来解决这个问题

4

1 回答 1

0

您需要将嵌套查询放在must子句下。inner_hits在嵌套查询的底部添加键并_source在头部启用特定字段(因为我们不关心其余与嵌套查询不匹配的子文档)如下:

{
  "_source": [
    "hotelName",
    "dailyRate"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "destination": {
              "query": "Albuquerque",
              "operator": "and"
            }
          }
        },
        {
          "nested": {
            "path": "availability",
            "query": {
              "bool": {
                "must": [
                  {
                    "range": {
                      "availability.date": {
                        "gte": "10/22/2017",
                        "lte": "10/24/2017"
                      }
                    }
                  },
                  {
                    "term": {
                      "availability.status": "true"
                    }
                  }
                ]
              }
            },
            "inner_hits": {
              "size": 10
            }
          }
        }
      ]
    }
  }
}

请注意,现在您将仅获得符合条件的嵌套文档,例如,对于您的示例,结果将是:

{
  "_index": "hotel_nested",
  "_type": "doc",
  "_id": "305",
  "_score": 4.815987,
  "_source": {
    "hotelName": "Sheraton San Diego Hotel & Marina",
    "dailyRate": "$462.59"
  },
  "inner_hits": {
    "availability": {
      "hits": {
        "total": 1,
        "hits": [
          {
            "date": "10/22/2017",
            "status": true
          }
        ]
      }
    }
  }
}

所以现在,您必须检查 10 月 22-24 范围内的所有日子是否都存在。

一种快捷方式是标记"inner_hits":{"size":0}并仅查看结果并检查数量是否inner_hits等于 10 月份 22-24 范围内的天数:

"inner_hits"."availability"."hits"."total" == 3
于 2017-11-20T09:26:07.943 回答