Elasticsearch | Unique results

Hi,

I working on a use case of something similar to the scenario mentioned below:

  1. Suppose I have indices called "Teacher" and "Student"
    Sample data of "Teacher" and "Student" are -
  2. Teacher

{
"city": "Madrid",
"name": "pranav",
"birth_month": "february",
"TeacherId": "2623"
}

  1. Student

{
"studentId": "2123",
"city": "Madrid",
"name": "abc",
"birth_month": "february"
}

Now I am searching on both the indices at once by mentioning both the indices using comma operator, for birth_month to be february .
Elastic returns these two documents as two different results. But I need results to be unique based on its city i.e., these two documents have to be part of one result based on its city value. Is there any method that I can implement this?

May be something like this:

DELETE teacher,student
PUT teacher/_doc/1
{
  "city": "Madrid",
  "name": "pranav",
  "birth_month": "february",
  "TeacherId": "2623"
}
PUT student/_doc/1
{
  "studentId": "2123",
  "city": "Madrid",
  "name": "abc",
  "birth_month": "february"
}
GET teacher,student/_search
{
  "size": 0, 
  "aggs": {
    "city": {
      "terms": {
        "field": "city.keyword"
      },
      "aggs": {
        "people": {
          "top_hits": {
            "size": 10
          }
        }
      }
    }
  }
}

It gives:

{
  "took" : 37,
  "timed_out" : false,
  "_shards" : {
    "total" : 10,
    "successful" : 10,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "city" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Madrid",
          "doc_count" : 2,
          "people" : {
            "hits" : {
              "total" : 2,
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "student",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_score" : 1.0,
                  "_source" : {
                    "studentId" : "2123",
                    "city" : "Madrid",
                    "name" : "abc",
                    "birth_month" : "february"
                  }
                },
                {
                  "_index" : "teacher",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_score" : 1.0,
                  "_source" : {
                    "city" : "Madrid",
                    "name" : "pranav",
                    "birth_month" : "february",
                    "TeacherId" : "2623"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

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