title: How to create subquery with elasticsearch
body: <p>I need to create a query on elasticsearch for the condition below.</p>
  
  <p>When the last element added with the "rabbitmq.queue.name" attribute is equal to "service_test_error" and the value from "rabbitmq.queue.messages.total.count"  is different of "0"</p>
  
  <p>The sql query below works for my search, but I could not to do the same query with elasticsearch</p>
  
  <pre><code>select * from metric where rabbitmq.queue.messages.total.count != '0' and rabbitmq.queue.name = 'service_test_error' and timestamp = (select max(timestamp) from metric where rabbitmq.queue.name = 'service_test_error')
  </code></pre>
  
  <p>These records below are examples that exist in my metric-xpto index</p>
  
  <pre><code>[
    {
      "_index": "metric-xpto",
      "_type": "_doc",
      "_id": "jYP1WnEBmYyEo7K68Zme",
      "_version": 1,
      "_score": null,
      "_source": {
        "@timestamp": "2020-04-08T18:03:14.899Z",
        "rabbitmq": {
          "queue": {
            "name": "service_test_error",
            "messages": {
              "total": {
                "count": 0
              }
            }
          }
        }
      }
    },
    {
      "_index": "metric-xpto",
      "_type": "_doc",
      "_id": "jYP1WnEBmYyEo7K68Zme",
      "_version": 1,
      "_score": null,
      "_source": {
        "@timestamp": "2020-04-07T18:03:14.899Z",
        "rabbitmq": {
          "queue": {
            "name": "service_test_error",
            "messages": {
              "total": {
                "count": 3
              }
            }
          }
        }
      }
    },
    {
      "_index": "metric-xpto",
      "_type": "_doc",
      "_id": "jYP1WnEBmYyEo7K68Zme",
      "_version": 1,
      "_score": null,
      "_source": {
        "@timestamp": "2020-04-03T17:03:14.899Z",
        "rabbitmq": {
          "queue": {
            "name": "service_alpha_test_error",
            "messages": {
              "total": {
                "count": 8
              }
            }
          }
        }
      }
    },
    {
      "_index": "metric-xpto",
      "_type": "_doc",
      "_id": "jYP1WnEBmYyEo7K68Zme",
      "_version": 1,
      "_score": null,
      "_source": {
        "@timestamp": "2020-04-03T18:03:14.899Z",
        "rabbitmq": {
          "queue": {
            "name": "service_test_error",
            "messages": {
              "total": {
                "count": 8
              }
            }
          }
        }
      }
    }
  ]
  </code></pre>
  
  <p>How can I create a similar query using elasticsearch?</p>