2016-04-26 42 views
0

需要您的幫助,瞭解基於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.誰能告訴我我做錯了什麼?

注意到某些字段已被刪除以保密。

+0

哪個ES版都返回true您運行? – Val

+0

我正在使用版本1.6.2 – Lee

回答

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( '消息')永遠不會