# Separate result by category

**URL:** <https://discuss.elastic.co/t/separate-result-by-category/340479>\
**Category:** Elasticsearch\
**Created:** [August 9, 2023, 5:05pm UTC](https://discuss.elastic.co/t/separate-result-by-category/340479 "2023-08-09T17:05:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Murilo\_Livorato](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/murilo_livorato/32/123848_2.png) [@Murilo\_Livorato](https://discuss.elastic.co/u/Murilo_Livorato)\
**Post date:** [August 9, 2023, 5:05pm UTC](https://discuss.elastic.co/t/separate-result-by-category/340479/1 "2023-08-09T17:05:07Z")

</div>

Hello , I have nested filed .  
And I want to bring the result separated by broker , something like this -

```auto
[ "broker_one" => [
   result
],
"broker_two" => [
     result
]

```

I know that I can aggregate the result , and count them by aggregation .  
but what I wold like to do is to separate the result , I dont know if with aggregation . wold be possible .

My Search -

```auto

GET /metric-property-broker-daily-2023-05/_search
{
  "size": 100,
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "real_estate",
            "query": {
              "match": {
                "real_estate.id": 1
              }
            }
          }
        },
        {
          "nested": {
            "path": "broker",
            "query": {
              "bool": {
                "should": [
                  {
                    "match": {
                      "broker.id": 103
                    }
                  },
                  {
                    "match": {
                      "broker.id": 109
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "range": {
            "timestamp": {
              "gte": "2023-05-04",
              "lte": "2023-06-05"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "brokers": {
      "nested": {
        "path": "broker"
      },
      "aggs": {
        "broker_one": {
          "filter": {
            "bool": {
              "filter": [
                {
                  "term": {
                    "broker.id": 103
                  }
                }
              ]
            }
          }
        },
        "broker_two": {
          "filter": {
            "bool": {
              "filter": [
                {
                  "term": {
                    "broker.id": 109
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}

```

The result -

```auto
{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 3,
    "hits": [
      {
        "_index": "metric-property-broker-daily-2023-05",
        "_id": "Mlbyz4kBgvOTcbqQPyPj",
        "_score": 3,
        "_source": {
          "timestamp": "2023-05-31",
          "count_property": 0,
          "sun_price": 0,
          "real_estate": {
            "id": 1,
            "name": "Real Estate One"
          },
          "broker": {
            "id": 103,
            "name": "Bob "
          }
        }
      },
      {
        "_index": "metric-property-broker-daily-2023-05",
        "_id": "NFbyz4kBgvOTcbqQPyPj",
        "_score": 3,
        "_source": {
          "timestamp": "2023-05-04",
          "count_property": 0,
          "sun_price": 0,
          "real_estate": {
            "id": 1,
            "name": "Real Estate One"
          },
          "broker": {
            "id": 103,
            "name": "Bob "
          }
        }
      },
      {
        "_index": "metric-property-broker-daily-2023-05",
        "_id": "NVbyz4kBgvOTcbqQPyPj",
        "_score": 3,
        "_source": {
          "timestamp": "2023-05-05",
          "count_property": 0,
          "sun_price": 0,
          "real_estate": {
            "id": 1,
            "name": "Real Estate One"
          },
          "broker": {
            "id": 109,
            "name": "James "
          }
        }
      },
          ]
  },
  "aggregations": {
    "brokers": {
      "doc_count": 3,
      "broker_one": {
        "doc_count": 2
      },
      "broker_two": {
        "doc_count": 1
      }
    }
  }
}

```

Does have any how to separate the result like this -  
separating -  
broker\_one: [{ result }], broker\_two: [{ result }]

```auto
{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 3,
    "hits": [{
    "broker_one": [
  {
        "_index": "metric-property-broker-daily-2023-05",
        "_id": "Mlbyz4kBgvOTcbqQPyPj",
        "_score": 3,
        "_source": {
          "timestamp": "2023-05-31",
          "count_property": 0,
          "sun_price": 0,
          "real_estate": {
            "id": 1,
            "name": "Real Estate One"
          },
          "broker": {
            "id": 103,
            "name": "Bob "
          }
        }
      },
      {
        "_index": "metric-property-broker-daily-2023-05",
        "_id": "NFbyz4kBgvOTcbqQPyPj",
        "_score": 3,
        "_source": {
          "timestamp": "2023-05-04",
          "count_property": 0,
          "sun_price": 0,
          "real_estate": {
            "id": 1,
            "name": "Real Estate One"
          },
          "broker": {
            "id": 103,
            "name": "Bob "
          }
        }
      }

    ],

     "broker_two": [
      {
        "_index": "metric-property-broker-daily-2023-05",
        "_id": "NVbyz4kBgvOTcbqQPyPj",
        "_score": 3,
        "_source": {
          "timestamp": "2023-05-05",
          "count_property": 0,
          "sun_price": 0,
          "real_estate": {
            "id": 1,
            "name": "Real Estate One"
          },
          "broker": {
            "id": 109,
            "name": "James "
          }
        }
      }
      ]
     }]
  },
  "aggregations": {
    "brokers": {
      "doc_count": 3,
      "broker_one": {
        "doc_count": 2
      },
      "broker_two": {
        "doc_count": 1
      }
    }
  }
}

```

---

<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:** [September 6, 2023, 5:05pm UTC](https://discuss.elastic.co/t/separate-result-by-category/340479/2 "2023-09-06T17:05:32Z")

</div>

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