ELastic query

is there a way to acheive this using elastic query builders for java

select id1,IF (count(id2)>0,count(id2),id2_name) as id2
from index where id1_name = 'xxxxxx'
and id_code=2 group by id1