写在前面
生产上出现个es数据不一致的问题,需要用嵌套查询出所有不一致的数据,但是就是一个简简单单的嵌套查询,对于之前一直用java api的人来说,太难了,所以记录下
正文
假设需要查询的条件如下:
where deleted = 0 and (iiapIftttId =’’ or iiapIftttId is null)
按照单个查询的规律来写,第一次写成了这样
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| { "query": { "bool": {"must":{ "term": { "deleted": "0" } }, "should": [ { "term": { "iiapIftttId": "" } }, { "bool": { "must_not": [ { "exists": { "field": "iiapIftttId" } } ] } } ] } }, "sort": [ { "createdTime": { "order": "desc" } } ] }
|
查询出来后竟然是错误的,经过反复测试和研究,发现这么写默认是or查询,即变成:
where deleted = 0 or (iiapIftttId =’’ or iiapIftttId is null)
所以正确的该这么写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| { "query": { "bool": { "must": [ { "term": { "deleted": "0" } }, { "bool": { "should": [ { "term": { "iiapIftttId": "" } }, { "bool": { "must_not": [ { "exists": { "field": "iiapIftttId" } } ] } } ] } } ] } }, "sort": [ { "updatedTime": { "order": "desc" } } ] }
|
这样就可以按照要求查询出来数据了~