2017-04-06 61 views
0

我在elasticsearch文件是這樣的:Elasticsearch常規令牌

{ 
    "user_id": 295, 
    "pictures": [ 
    {"type_id": 201201, "picture_id": 543}, 
    {"type_id": 201202, "picture_id": 544} 
    ] 
} 

有目的的尋找用戶與特定的圖像類型我運行此查詢:

curl -XGET localhost:9201/z/u/_search -d '{ 
    "post_filter": { 
    "bool": { 
     "must": [ 
     { 
      "script": { 
      "script": "doc['"'"'pictures.type_id'"'"'].values.contains(201201L)" 
      } 
     } 
     ] 
    } 
    } 
}' 

它完美的作品!但是,當我運行此查詢:

curl -XGET localhost:9201/z/u/_search -d '{ 
    "post_filter": { 
    "bool": { 
     "must": [ 
     { 
      "script": { 
      "script": "doc['"'"'pictures.type_id'"'"'].values.contains(201201)" 
      } 
     } 
     ] 
    } 
    } 
}' 

它不會工作,查詢之間的區別是201201L and 201201
有人能解釋嗎?

回答

2

201201L中,L之後的數字表示它屬於Long這一事實。如果沒有L,則該號碼默認爲Integer。這是一個Java約定。

因此,如果映射中的pictures.type_id的類型爲long那麼您需要將其與Long值進行比較,而不是整數值。 Long.equals()方法(其中List.contains()內部調用)中的比較首先檢查參數是Long類型,如果不是這種情況,則比較失敗。

+0

非常感謝! –

+0

很高興幫助! – Val