# Sending multiple \_count requests in one go

**URL:** <https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456>\
**Category:** Elasticsearch\
**Created:** [March 8, 2019, 8:43am UTC](https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456 "2019-03-08T08:43:23Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yuval\_Khalifa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuval_khalifa/32/9604_2.png) [@Yuval\_Khalifa](https://discuss.elastic.co/u/Yuval_Khalifa)\
**Post date:** [March 8, 2019, 8:43am UTC](https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456/1 "2019-03-08T08:43:23Z")

</div>

Is it possible to send multiple requests to the \_count API in one go? I saw this post [Does count API support searching by multiple TERMs and return multiple COUNTs?](https://discuss.elastic.co/t/does-count-api-support-searching-by-multiple-terms-and-return-multiple-counts/10183/2) but from what I can understand, the recommendation to use the multi-search API and set the search\_type to count won't work since that it can only be set to either "dfs\_query\_then\_fetch" or "query\_then\_fetch". Is that correct?

What I was hoping for is to be able to send a request like this to Elastic:  
{"index": "logstash-_"}  
{"query": {"query\_string": {"query": "destination\_ip:"10.253.0.10""}}}  
{"index": "logstash-_"}  
{"query": {"query\_string": {"query": "destination\_ip:"10.253.0.172""}}}  
{"index": "logstash-_"}  
{"query": {"query\_string": {"query": "destination\_ip:"10.253.0.141""}}}  
{"index": "logstash-_"}  
{"query": {"query\_string": {"query": "destination\_ip:"10.253.0.153""}}}

and receive as a response the counts for each query in an array or something similar.

Is this doable?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 8, 2019, 8:59am UTC](https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456/2 "2019-03-08T08:59:18Z")

</div>

You can run multiple filter aggregations or you can use the multisearch API.

---

<div class="post-metadata">

**Author:** ![Yuval\_Khalifa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuval_khalifa/32/9604_2.png) [@Yuval\_Khalifa](https://discuss.elastic.co/u/Yuval_Khalifa)\
**Post date:** [March 8, 2019, 9:16am UTC](https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456/3 "2019-03-08T09:16:09Z")

</div>

Thank you for your quick reply...

How can I get only the counts with the multisearch API? I couldn't find any example of that? For example, how would my example above would be written if I wanted to get only the counts from the multisearch API?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 8, 2019, 10:53am UTC](https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456/4 "2019-03-08T10:53:01Z")

</div>

Reading the doc here: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html)

```auto
$ cat requests

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.10\""}}}

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.172\""}}}

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.141\""}}}

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.153\""}}}

```

```auto
$ curl -H "Content-Type: application/x-ndjson" -XGET localhost:9200/logstash-*/_msearch --data-binary "@requests"; echo

```

That should work I think.

---

<div class="post-metadata">

**Author:** ![Yuval\_Khalifa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuval_khalifa/32/9604_2.png) [@Yuval\_Khalifa](https://discuss.elastic.co/u/Yuval_Khalifa)\
**Post date:** [March 8, 2019, 11:20am UTC](https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456/5 "2019-03-08T11:20:00Z")

</div>

Thanks a lot. It worked, although I had to escape the quotes like this:

```
$ cat tmp_bulk_queries_example 
{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.10\""}}}
{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.172\""}}}
{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.141\""}}}
{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.153\""}}}

```

Thanks a LOT!!  
Yuval

---

<div class="post-metadata">

**Author:** ![Yuval\_Khalifa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuval_khalifa/32/9604_2.png) [@Yuval\_Khalifa](https://discuss.elastic.co/u/Yuval_Khalifa)\
**Post date:** [March 8, 2019, 11:25am UTC](https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456/6 "2019-03-08T11:25:30Z")

</div>

Ehmmm.... I tried it now and I noticed that I only received responses for two of my queries and I'm wondering where are the rest of them:

```
$ cat tmp_bulk_queries_example 
{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.10\""}}}
{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.172\""}}}
{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.141\""}}}
{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.153\""}}}

$ curl -H "Content-Type: application/x-ndjson" -XGET 'http://10.253.0.153:9200/logstash-*/_msearch?pretty' --data-binary "@tmp_bulk_queries_example" 
{
  "responses" : [
    {
      "took" : 1992,
      "timed_out" : false,
      "_shards" : {
        "total" : 95,
        "successful" : 95,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 2259,
        "max_score" : 0.0,
        "hits" : []
      },
      "status" : 200
    },
    {
      "took" : 1987,
      "timed_out" : false,
      "_shards" : {
        "total" : 95,
        "successful" : 95,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 36,
        "max_score" : 0.0,
        "hits" : []
      },
      "status" : 200
    }
  ]
}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 8, 2019, 11:39am UTC](https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456/7 "2019-03-08T11:39:56Z")

</div>

The blank lines I added in my example are super important. Don't skip them.

---

<div class="post-metadata">

**Author:** ![Yuval\_Khalifa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuval_khalifa/32/9604_2.png) [@Yuval\_Khalifa](https://discuss.elastic.co/u/Yuval_Khalifa)\
**Post date:** [March 8, 2019, 11:43am UTC](https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456/8 "2019-03-08T11:43:00Z")

</div>

Well if add them back like so:

```
$ cat tmp_bulk_queries_example 
{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.10\""}}}

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.172\""}}}

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.141\""}}}

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.153\""}}}

```

The result is this:

```
$ curl -H "Content-Type: application/x-ndjson" -XGET 'http://10.253.0.153:9200/logstash-*/_msearch?pretty' --data-binary "@tmp_bulk_queries_example" 2>/dev/null
{
  "error" : {
    "root_cause" : [
      {
        "type" : "parsing_exception",
        "reason" : "Expected [START_OBJECT] but found [null]",
        "line" : 1,
        "col" : 0
      }
    ],
    "type" : "parsing_exception",
    "reason" : "Expected [START_OBJECT] but found [null]",
    "line" : 1,
    "col" : 0
  },
  "status" : 400
}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 8, 2019, 11:44am UTC](https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456/9 "2019-03-08T11:44:00Z")

</div>

Read my example again. To be clear:

```auto
$ cat requests

```

Gives:

```auto

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.10\""}}}

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.172\""}}}

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.141\""}}}

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.153\""}}}

```

---

<div class="post-metadata">

**Author:** ![Yuval\_Khalifa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuval_khalifa/32/9604_2.png) [@Yuval\_Khalifa](https://discuss.elastic.co/u/Yuval_Khalifa)\
**Post date:** [March 8, 2019, 11:50am UTC](https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456/10 "2019-03-08T11:50:54Z")

</div>

I'm sorry if I'm missing something stupid here... but I tried again using exactly your example (the only thing that I changed was the filename and the fact that I added '?pretty' to the URL)

```
$ cat tmp_bulk_queries_example 

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.10\""}}}

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.172\""}}}

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.141\""}}}

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.153\""}}}

```

The result is this:

```
$ curl -H "Content-Type: application/x-ndjson" -XGET 'http://10.253.0.153:9200/logstash-*/_msearch?pretty' --data-binary "@tmp_bulk_queries_example" 2>/dev/null
{
  "error" : {
    "root_cause" : [
      {
        "type" : "parsing_exception",
        "reason" : "Expected [START_OBJECT] but found [null]",
        "line" : 1,
        "col" : 0
      }
    ],
    "type" : "parsing_exception",
    "reason" : "Expected [START_OBJECT] but found [null]",
    "line" : 1,
    "col" : 0
  },
  "status" : 400
}

```

Am I missing something stupid here?

---

<div class="post-metadata">

**Author:** ![Yuval\_Khalifa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuval_khalifa/32/9604_2.png) [@Yuval\_Khalifa](https://discuss.elastic.co/u/Yuval_Khalifa)\
**Post date:** [March 8, 2019, 11:59am UTC](https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456/11 "2019-03-08T11:59:48Z")

</div>

Ok, I think I figured it out with your help... When I use this syntax it seems to work perfectly:

```
$ cat tmp_bulk_queries_example 
{}
{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.10\""}}}
{}
{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.172\""}}}
{}
{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.141\""}}}
{}
{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.153\""}}}

```

Then, the result looks like this:

```
$ curl -H "Content-Type: application/x-ndjson" -XGET 'http://10.253.0.153:9200/logstash-*/_msearch?pretty' --data-binary "@tmp_bulk_queries_example" 2>/dev/null
{
  "responses" : [
    {
      "took" : 6897,
      "timed_out" : false,
      "_shards" : {
        "total" : 95,
        "successful" : 95,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 788182,
        "max_score" : 0.0,
        "hits" : []
      },
      "status" : 200
    },
    {
      "took" : 6904,
      "timed_out" : false,
      "_shards" : {
        "total" : 95,
        "successful" : 95,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 2259,
        "max_score" : 0.0,
        "hits" : []
      },
      "status" : 200
    },
    {
      "took" : 6844,
      "timed_out" : false,
      "_shards" : {
        "total" : 95,
        "successful" : 95,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 612,
        "max_score" : 0.0,
        "hits" : []
      },
      "status" : 200
    },
    {
      "took" : 6848,
      "timed_out" : false,
      "_shards" : {
        "total" : 95,
        "successful" : 95,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 36,
        "max_score" : 0.0,
        "hits" : []
      },
      "status" : 200
    }
  ]
}

```

and if I use jq it looks fine:

```
$ curl -H "Content-Type: application/x-ndjson" -XGET 'http://10.253.0.153:9200/logstash-*/_msearch?pretty' --data-binary "@tmp_bulk_queries_example" 2>/dev/null | jq '.responses[]["hits"]["total"]'
788182
2259
612
36

```

Thanks a LOT!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 8, 2019, 12:07pm UTC](https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456/12 "2019-03-08T12:07:07Z")

</div>

Yeah sorry. It probably requires at least a non blank line.

Could you do me a favor? Could you try with this syntax?

```auto
$ cat tmp_bulk_queries_example 
{}
{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.10\""}}}

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.172\""}}}

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.141\""}}}

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.153\""}}}

```

---

<div class="post-metadata">

**Author:** ![Yuval\_Khalifa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuval_khalifa/32/9604_2.png) [@Yuval\_Khalifa](https://discuss.elastic.co/u/Yuval_Khalifa)\
**Post date:** [March 8, 2019, 12:19pm UTC](https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456/13 "2019-03-08T12:19:00Z")

</div>

Well, this input:

```
smartonion@smart-onion:~$ cat tmp_bulk_queries_example 
{}
{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.10\""}}}

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.172\""}}}

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.141\""}}}

{"size": 0, "query": {"query_string": {"query": "destination_ip:\"10.253.0.153\""}}}

$ 

```

resulted in this:

```
$ curl -H "Content-Type: application/x-ndjson" -XGET 'http://10.253.0.153:9200/logstash-*/_msearch?pretty' --data-binary "@tmp_bulk_queries_example" 2>/dev/null
{
  "error" : {
    "root_cause" : [
      {
        "type" : "parsing_exception",
        "reason" : "Expected [START_OBJECT] but found [null]",
        "line" : 1,
        "col" : 0
      }
    ],
    "type" : "parsing_exception",
    "reason" : "Expected [START_OBJECT] but found [null]",
    "line" : 1,
    "col" : 0
  },
  "status" : 400
}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 8, 2019, 1:35pm UTC](https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456/14 "2019-03-08T13:35:08Z")

</div>

What version are you using? I can't reproduce that problem locally on 6.6.1.

```auto
DELETE /test
PUT /test/_doc/1
{
  "foo": "bar"
}

```

```auto
$ cat requests

```

```auto
{}
{ "query": {"match_all": {}}}

{ "query": {"match_all": {}}}

{ "query": {"match_all": {}}}

{ "query": {"match_all": {}}}

```

```auto
curl -H "Content-Type: application/x-ndjson" -XGET "http://127.0.0.1:9200/test/_msearch?pretty" --data-binary "@requests"; echo

```

Gives:

```auto
{
  "responses" : [
    {
      "took" : 6,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 1,
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 1.0,
            "_source" : {
              "foo" : "bar"
            }
          }
        ]
      },
      "status" : 200
    },
    {
      "took" : 4,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 1,
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 1.0,
            "_source" : {
              "foo" : "bar"
            }
          }
        ]
      },
      "status" : 200
    },
    {
      "took" : 5,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 1,
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 1.0,
            "_source" : {
              "foo" : "bar"
            }
          }
        ]
      },
      "status" : 200
    },
    {
      "took" : 4,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 1,
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 1.0,
            "_source" : {
              "foo" : "bar"
            }
          }
        ]
      },
      "status" : 200
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![Yuval\_Khalifa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuval_khalifa/32/9604_2.png) [@Yuval\_Khalifa](https://discuss.elastic.co/u/Yuval_Khalifa)\
**Post date:** [March 8, 2019, 1:39pm UTC](https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456/15 "2019-03-08T13:39:12Z")

</div>

Well it's only 6.6.0 here:

```
$ curl 10.253.0.153:9200
{
  "name" : "N4g8_lf",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "vTGsAaa9T7C8BbQNV0iaiw",
  "version" : {
    "number" : "6.6.0",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "a9861f4",
    "build_date" : "2019-01-24T11:27:09.439740Z",
    "build_snapshot" : false,
    "lucene_version" : "7.6.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}
```

---

<div class="post-metadata">

**Author:** ![Yuval\_Khalifa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuval_khalifa/32/9604_2.png) [@Yuval\_Khalifa](https://discuss.elastic.co/u/Yuval_Khalifa)\
**Post date:** [March 8, 2019, 1:42pm UTC](https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456/16 "2019-03-08T13:42:18Z")

</div>

Perhaps it caused by the copy-pasting to/from the forum. Maybe if you'll attach the queries file to the message it would work correctly.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 8, 2019, 1:43pm UTC](https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456/17 "2019-03-08T13:43:48Z")

</div>

Ok. FYI I sent a doc fix here:

> <https://github.com/elastic/elasticsearch/pull/39841>

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 5, 2019, 1:43pm UTC](https://discuss.elastic.co/t/sending-multiple-count-requests-in-one-go/171456/18 "2019-04-05T13:43:53Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
