# Get grouped results along with siblings not matching the filter criteria

**URL:** <https://discuss.elastic.co/t/get-grouped-results-along-with-siblings-not-matching-the-filter-criteria/278003>\
**Category:** Elasticsearch\
**Created:** [July 6, 2021, 10:59pm UTC](https://discuss.elastic.co/t/get-grouped-results-along-with-siblings-not-matching-the-filter-criteria/278003 "2021-07-06T22:59:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Federico\_Martinez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/federico_martinez/32/82537_2.png) [@Federico\_Martinez](https://discuss.elastic.co/u/Federico_Martinez)\
**Post date:** [July 6, 2021, 10:59pm UTC](https://discuss.elastic.co/t/get-grouped-results-along-with-siblings-not-matching-the-filter-criteria/278003/1 "2021-07-06T22:59:14Z")

</div>

Hi, is it possible to query elastic for documents matching a filter criteria, grouped by a field and also get any sibling documents not matching the filter criteria?  
By siblings I mean documents having the same value on the attribute by which results are being grouped by.

Here 's the scenario.

Mapping

```auto
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "groupId": { "type": "keyword" },
      "attr": { "type": "keyword" }
    }
  }
}

```

Documents

```auto
POST my-index-000001/_doc
{
  "groupId": "a",
  "attr": "1"
}

```

```auto
POST my-index-000001/_doc
{
  "groupId": "a",
  "attr": "2"
}

```

```auto
POST my-index-000001/_doc
{
  "groupId": "a",
  "attr": "3"
}

```

```auto
POST my-index-000001/_doc
{
  "groupId": "b",
  "attr": "5"
}

```

```auto
POST my-index-000001/_doc
{
  "groupId": "c",
  "attr": "1"
}

```

```auto
POST my-index-000001/_doc
{
  "groupId": "c",
  "attr": "3"
}

```

Query

```auto
POST /my-index-000001/_search
{
    "query": {
        "term": {
            "attr": "3"
        }
    }
}

```

The result I' d like to get is something like this.

```auto
[{
		"groupId": "a",
		"docs": [{
				"groupId": "a",
				"attr": "1"
			},{
				"groupId": "a",
				"attr": "2"
			},{
				"groupId": "a",
				"attr": "3"
			}
		]
	},{
		"groupId": "c",
		"docs": [{
				"groupId": "c",
				"attr": "1"
			},{
				"groupId": "c",
				"attr": "3"
			}
		]
	}
]

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [July 7, 2021, 2:31pm UTC](https://discuss.elastic.co/t/get-grouped-results-along-with-siblings-not-matching-the-filter-criteria/278003/2 "2021-07-07T14:31:15Z")

</div>

You might want to take a look at the [top\_hits aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/7.13/search-aggregations-metrics-top-hits-aggregation.html).

---

<div class="post-metadata">

**Author:** ![Federico\_Martinez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/federico_martinez/32/82537_2.png) [@Federico\_Martinez](https://discuss.elastic.co/u/Federico_Martinez)\
**Post date:** [July 7, 2021, 3:01pm UTC](https://discuss.elastic.co/t/get-grouped-results-along-with-siblings-not-matching-the-filter-criteria/278003/3 "2021-07-07T15:01:10Z")

</div>

Hey Alex, thanks for replying.

This is what I came up with, I get all documents grouped by groupId  
How would I go about filtering by attr=3 and still getting all docs where groupId equals a and c?

```auto
POST /my-index-000001/_search
{
    "size": 0,
    "aggs": {
        "docs": {
            "terms": {
                "field": "groupId"
            },
            "aggs": {
                "result": {
                    "top_hits": {}
                }
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Federico\_Martinez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/federico_martinez/32/82537_2.png) [@Federico\_Martinez](https://discuss.elastic.co/u/Federico_Martinez)\
**Post date:** [July 7, 2021, 4:37pm UTC](https://discuss.elastic.co/t/get-grouped-results-along-with-siblings-not-matching-the-filter-criteria/278003/4 "2021-07-07T16:37:23Z")

</div>

To give more context, I'd like to be able to get documents individually or grouped by groupId.  
In case of getting documents grouped by groupId, I'd like to get all documents matching the filter criteria and also having the same groupId.  
Hope it makes sense.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [July 8, 2021, 11:00am UTC](https://discuss.elastic.co/t/get-grouped-results-along-with-siblings-not-matching-the-filter-criteria/278003/5 "2021-07-08T11:00:57Z")

</div>

Would it make sense to sent a second search request with a proper filter by group id?

---

<div class="post-metadata">

**Author:** ![Federico\_Martinez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/federico_martinez/32/82537_2.png) [@Federico\_Martinez](https://discuss.elastic.co/u/Federico_Martinez)\
**Post date:** [July 8, 2021, 1:21pm UTC](https://discuss.elastic.co/t/get-grouped-results-along-with-siblings-not-matching-the-filter-criteria/278003/6 "2021-07-08T13:21:00Z")

</div>

You mean to issue a second request filtering by the list of groupIds I get as a result of the first request?

I was hoping to get the data in one go but I guess issuing a second request is my best alternative.

Thanks Alex!

---

<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:** [August 5, 2021, 1:21pm UTC](https://discuss.elastic.co/t/get-grouped-results-along-with-siblings-not-matching-the-filter-criteria/278003/7 "2021-08-05T13:21:59Z")

</div>

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