Elasticsearch多语言查询与排序奇怪行为的脚本
问题描述:
需要您的帮助,了解基于elasticsearch脚本排序的行为。Elasticsearch多语言查询与排序奇怪行为的脚本
,首先让我贴我的elasticsearch类型的映射:
{
"nestedDateType" : {
"properties" : {
"message" : {
"properties" : {
"date" : {
"type" : "date",
"format" : "dateOptionalTime"
}
}
}
}
},
"nonNestedDateType" : {
"properties" : {
"date" : {
"type" : "date",
"format" : "dateOptionalTime"
}
}
}
}
现在我想要做的就是查询这些2种类型,并根据排序的日期。 问题出现在nestedDateType中,日期路径是“message.date”,在nonNestedDateType中,日期路径是“date”。
据我所知,我必须使用基于脚本的排序来做到这一点。但是,我所做的脚本没有按预期工作。这是我尝试查询:
POST http://locahost:9200/index/nonNestedDateType,nestedDateType/_search?size=5000
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"or": [
{
"range": {
"date": {
"gte": "2015-04-01"
}
}
},
{
"range": {
"message.date": {
"gte": "2015-04-01"
}
}
}
]
}
]
}
}
}
},
"sort": {
"_script": {
"script": "doc.containsKey('message') ? doc.message.date.value : doc.date.value",
"type": "number",
"order": "desc"
}
}
}
和这些是我得到的结果是:
{
"took": 60,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 15,
"max_score": null,
"hits": [
{
"_index": "***",
"_type": "nonNestedDateType",
"_id": "***",
"_score": null,
"_source": {
"docId": "***",
"date": 1461634484557
},
"sort": [
1461634484557
]
},
{
"_index": "***",
"_type": "nonNestedDateType",
"_id": "***",
"_score": null,
"_source": {
"docId": "***",
"date": 1461634483528
},
"sort": [
1461634483528
]
},
{
"_index": "***",
"_type": "nestedDateType",
"_id": "***",
"_score": null,
"_source": {
"docId": "***",
"message": {
"date": 1461548078310
}
},
"sort": [
0
]
}
]
}
}
,你可以从类型nestedDateType的最后结果看,我期待的那种= 1461548078310而不是0.谁能告诉我我做错了什么?
注意到某些字段已被删除以保密。
答
我终于可以做它的工作方式改变
script": "doc.containsKey('message') ? doc.message.date.value : doc.date.value"
到
script": "doc.date.value == 0 ? doc['message.date'].value : doc.date.value"
仍然好奇至于为什么doc.containsKey( '消息')永远不会
哪个ES版都返回true您运行? – Val
我正在使用版本1.6.2 – Lee