0

I have 2 queries I am trying to perform:

1) I want to get Frank's birthday. (The name of the person is the id of the document).

2) Frank's boss is Bob. I need to get the number of people (including Frank) that have Bob as their boss.

For #1 I am doing:

GET /myindex/_search
{
  "_source": [
    "birthday"
  ],
  "query": {
    "term": {"_id" : "Frank"}
  }
}

For #2 I am doing:

GET /myindex/_count
{
 "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "boss": "Bob"
          }
        }
      ]
    }
  }
}

It seems like a waste to get #1 then immediately get #2. Is there a way to combine the query into 1 query?

My attempt:

GET /myindex/_search
    {
      "_source": [
        "birthday"
      ],
      "query": {
        "term": {"_id" : "Frank"},
        "bool": {
          "filter": [
            {
              "term": {
                "boss": "Bob"
              }
            }
          ]
        }
      }
    }

This obviously is wrong and returns an error: [term] malformed query, expected [END_OBJECT] but found [FIELD_NAME]

1 Answers1

1

Until you DO NOT need result from first query inside of your other queries you can use Multi Search Api

Vova Bilyachat
  • 18,765
  • 4
  • 55
  • 80