ELASTICSEARCH - Count unique value with a condition

I would like a query which it returns the number of times a field is repeated, according to the unique value of another field I have this json:

          "name" : james,
          "city" : "chicago" <----------- same
        },
        {
          "name" : james,
          "city" : "san francisco"
        },
        {
          "name" : james,
          "city" : "chicago" <-----------same
        },
         {
          "name" : Mike,
          "obtained_from" : "chicago"
        },
         {
          "name" : Mike,
          "obtained_from" : "texas"<-----------same
        },
         {
          "name" : Mike,
          "obtained_from" : "texas"<-----------same
        },
         {
          "name" : Peter,
          "obtained_from" : "chicago"
        },

I want to make a query where I count based on the unique value of two fields. For example, james is equal to 2, because there are two equal fields (name: james, city, chicago) and a different field (name: james, city: san francisco) The output would then be the following:

 {
    "key" : "james",
    "doc_count" : 2
  },
  {
    "key" : "Mike",
    "doc_count" : 2
  },
  {
    "key" : "Peter",
    "doc_count" : 3
  },

It is possible to do a single value count of two fields?

the cardinality aggregation only supports a single field. You could use scripting to concatenate those two fields though. after using a terms aggregation on the name field.

would that work in your case?

Thank you for your answer.
I going to try your porpuse and get documentation about scripting

Could we concatenate with a scripting two fiedls?

the example in documentation I linked is doing exactly that.

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