# Select distinct value from multiple indexes

**URL:** <https://discuss.elastic.co/t/select-distinct-value-from-multiple-indexes/258696>\
**Category:** Elasticsearch\
**Created:** [December 15, 2020, 10:17am UTC](https://discuss.elastic.co/t/select-distinct-value-from-multiple-indexes/258696 "2020-12-15T10:17:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmedbrandver](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@ahmedbrandver](https://discuss.elastic.co/u/ahmedbrandver)\
**Post date:** [December 15, 2020, 10:17am UTC](https://discuss.elastic.co/t/select-distinct-value-from-multiple-indexes/258696/1 "2020-12-15T10:17:59Z")

</div>

Hello, how can I add a aggregation to get data based on unique field of multiple indexes. Let's say I have three different indexes in which they have a unique code but one unique code can be exists in different field of different index for say: I have unique code 10001 it can be exists as gtin in one index and barcode in another index. I want to get unique data base on gtin and barcode. So far after searching I am able to come up with a query but it didn't work.

```auto
{
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "should": [
                            {
                                "query_string": {
                                    "query": "8809263701127"
                                }
                            },
                            {
                                "nested": {
                                    "path": "attributes",
                                    "query": {
                                        "bool": {
                                            "must": [
                                                {
                                                    "term": {
                                                        "attributes.value": "8809263701127"
                                                    }
                                                }
                                            ]
                                        }
                                    },
                                    "score_mode": "max",
                                    "ignore_unmapped": true
                                }
                            }
                        ],
                        "minimum_should_match": 1
                    }
                }
            ],
            "filter": [
                {
                    "bool": {
                        "should": [
                            {
                                "bool": {
                                    "must_not": {
                                        "term": {
                                            "_index": "custom-products"
                                        }
                                    }
                                }
                            },
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "term": {
                                                "merchant.bvid": "b9daa4b4-9f89-4240-9892-c7df70774c18"
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                }
            ]
        }
    },
    "aggs": {
        "unique_set_1": {
            "terms": {
                "field": "gtin"
            },
            "aggregations": {
                "unique_set_2": {
                    "terms": {
                        "field": "barcode"
                    }
                }
            }
        }
    }
}

```

---

<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:** [January 12, 2021, 10:18am UTC](https://discuss.elastic.co/t/select-distinct-value-from-multiple-indexes/258696/2 "2021-01-12T10:18:08Z")

</div>

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