2017-04-06 62 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