我正在使用相当古老的elasticsearch 2.5。我在每个文档中都有酒店的可用性信息。有一个名为"可用性"其映射如下:
"availabilities":{
"type": "nested",
"dynamic": "strict",
"properties": {
"start": { "type": "date", "format": "yyyy-MM-dd" },
"end": { "type": "date", "format": "yyyy-MM-dd" }
}
}
其中一个示例文档(剥离版本)如下:
{
"name": "Seaside hotel",
"availabilities": [
{
"start": "2018-03-01",
"end": "2018-10-01"
},
{
"start": "2018-10-04",
"end": "2018-10-04"
},
{
"start": "2018-10-06",
"end": "2018-10-06"
},
{
"start": "2018-10-08",
"end": "2018-10-17"
},
{
"start": "2018-10-21",
"end": "2018-10-28"
},
{
"start": "2018-10-30",
"end": "2018-10-31"
},
{
"start": "2018-11-03",
"end": "2018-11-10"
},
{
"start": "2018-11-13",
"end": "2019-03-01"
},
{
"start": "2019-03-04",
"end": "2019-03-04"
},
{
"start": "2019-03-06",
"end": "2020-02-29"
}
]
}
我想找到所有这些酒店' doc,来自"2018-10-01" (YYYY-MM-DD) to "2018-10-10"
的可用性。我的搜索查询如下:
以毫秒1539154800000 milliseconds = 2018-10-10 and 1538377200000 = 2018-10-01
GET hotels/_search
{
"query": {
"filtered": {
"filter": {
"query": {
"bool": {
"must": [{
"nested": {
"query": {
"bool": {
"must": [{
"script": {
"script": "return (doc['availabilities.end'].date.getMillis() <= 1539154800000 && doc['availabilities.start'].date.getMillis() >= 1538377200000)"
}
}]
}
},
"path": "availabilities"
}
}],
"must_not": null
}
}
}
}
}
}
当我运行此查询时,我最终得到了上述&#34; Seaside hotel&#34;在结果集中,虽然它不应该在那里,因为它在2018-10-01到2018-10-10之间没有任何可用性。
现在我将查询更改为不使用脚本,我在这里搜索的酒店有2018-10-09到2018-10-16的可用性
GET hotels/_search
{
"query": {
"filtered": {
"filter": {
"query": {
"bool": {
"must": [{
"nested": {
"query": {
"bool": {
"must": [{
"range": {
"availabilities.end": {
"gte": "2018-10-16"
}
}
}, {
"range": {
"availabilities.start": {
"lte": "2018-10-09"
}
}
}]
}
},
"path": "availabilities"
}
}],
"must_not": null
}
}
}
}
}
}
这个查询应该带给我&#34; Seaside hotel&#34;结果中的doc,因为它有每个搜索日期的可用性,但搜索没有给我这个酒店。
我的目的是查询用户在指定的可用日期搜索酒店。知道我做错了什么或者我怎样才能实现目标?
答案 0 :(得分:0)
你好朋友你的查询正在返回正确的结果。我正如你所提到的那样在我的机器上进行了测试,它返回了我正确的文件
首先我把索引
PUT hotels
然后我将名称类型的酒店索引与提到的映射
一起使用PUT hotels/_mapping/name
{
"name": {
"properties": {
"availabilities": {
"type": "nested",
"dynamic": "strict",
"properties": {
"start": {
"type": "date",
"format": "yyyy-MM-dd"
},
"end": {
"type": "date",
"format": "yyyy-MM-dd"
}
}
},
"name":{
"type": "string"
}
}
}
}
我把数据
PUT hotels/name/1
{
"name": "Seaside hotel",
"availabilities": [
{
"start": "2018-03-01",
"end": "2018-10-01"
},
{
"start": "2018-10-04",
"end": "2018-10-04"
},
{
"start": "2018-10-06",
"end": "2018-10-06"
},
{
"start": "2018-10-08",
"end": "2018-10-17"
},
{
"start": "2018-10-21",
"end": "2018-10-28"
},
{
"start": "2018-10-30",
"end": "2018-10-31"
},
{
"start": "2018-11-03",
"end": "2018-11-10"
},
{
"start": "2018-11-13",
"end": "2019-03-01"
},
{
"start": "2019-03-04",
"end": "2019-03-04"
},
{
"start": "2019-03-06",
"end": "2020-02-29"
}
]
}
我运行查询
GET hotels/name/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"inner_hits":{},
"query": {
"bool": {
"must": [
{
"range": {
"availabilities.end": {
"gte": "2018-10-16"
}
}
},
{
"range": {
"availabilities.start": {
"lte": "2018-10-09"
}
}
}
]
}
},
"path": "availabilities"
}
}
]
}
}
}
输出
{
"_index": "hotels",
"_type": "name",
"_id": "1",
"_score": 1.4142135,
"_source": {
"name": "Seaside hotel",
"availabilities": [
{
"start": "2018-03-01",
"end": "2018-10-01"
},
{
"start": "2018-10-04",
"end": "2018-10-04"
},
{
"start": "2018-10-06",
"end": "2018-10-06"
},
{
"start": "2018-10-08",
"end": "2018-10-17"
},
{
"start": "2018-10-21",
"end": "2018-10-28"
},
{
"start": "2018-10-30",
"end": "2018-10-31"
},
{
"start": "2018-11-03",
"end": "2018-11-10"
},
{
"start": "2018-11-13",
"end": "2019-03-01"
},
{
"start": "2019-03-04",
"end": "2019-03-04"
},
{
"start": "2019-03-06",
"end": "2020-02-29"
}
]
}
}
请检查。