# Elasticsearch | Unique results

**URL:** <https://discuss.elastic.co/t/elasticsearch-unique-results/170449>\
**Category:** Elasticsearch\
**Created:** [March 1, 2019, 7:23am UTC](https://discuss.elastic.co/t/elasticsearch-unique-results/170449 "2019-03-01T07:23:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pranav24](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@pranav24](https://discuss.elastic.co/u/pranav24)\
**Post date:** [March 1, 2019, 7:23am UTC](https://discuss.elastic.co/t/elasticsearch-unique-results/170449/1 "2019-03-01T07:23:00Z")

</div>

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?

---

<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 1, 2019, 11:37am UTC](https://discuss.elastic.co/t/elasticsearch-unique-results/170449/2 "2019-03-01T11:37:35Z")

</div>

May be something like this:

```auto
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:

```auto
{
  "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"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

```

---

<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:** [March 29, 2019, 11:37am UTC](https://discuss.elastic.co/t/elasticsearch-unique-results/170449/3 "2019-03-29T11:37:49Z")

</div>

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