I'm not sure I understand the question. Do you have a full example?
Using a pure Kibana Dev Console script would be ideal as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.
A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.