# Group by + Consolidation + Filter + sort

**URL:** <https://discuss.elastic.co/t/group-by-consolidation-filter-sort/52853>\
**Category:** Elasticsearch\
**Created:** [June 15, 2016, 11:21am UTC](https://discuss.elastic.co/t/group-by-consolidation-filter-sort/52853 "2016-06-15T11:21:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![brightvip](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@brightvip](https://discuss.elastic.co/u/brightvip)\
**Post date:** [June 15, 2016, 11:21am UTC](https://discuss.elastic.co/t/group-by-consolidation-filter-sort/52853/1 "2016-06-15T11:21:42Z")

</div>

hi, I would like to complete this operation

data：

```
{
    "city": "Los Angeles",
    "name": "1",
    "time": "1464240096"
}   
{
    "city": "Los Angeles",
    "name": "2",
    "time": "1464240096"
}  
{
    "city": "Los Angeles",
    "name": "3",
    "time": "1464240096"
}  
{
    "city": "Los Angeles",
    "name": "4",
    "time": "1464240096"
} 
{
    "city": "Los Angeles",
    "name": "5",
    "time": "1464240096"
} 
{
    "city": "Los Angeles",
    "name": "6",
    "time": "1464240096"
} 
{
    "city": "Los Angeles",
    "name": "7",
    "time": "1464240096"
} 
{
    "city": "Los Angeles",
    "name": "8",
    "time": "1464240096"
}  
{
    "city": "Los Angeles",
    "name": "9",
    "time": "1464240096"
} 
{
    "city": "Los Angeles",
    "name": "10",
    "time": "1464240096"
}  
{
    "city": "Los Angeles",
    "name": "11",
    "time": "1464240096"
} 
{
    "city": "Los Angeles",
    "name": "12",
    "time": "1464240096"
} 
{
    "city": "new York",
    "name": "21",
    "time": "1464240091"
}  
{
    "city": "new York",
    "name": "22",
    "time": "1464240091"
} 
{
    "city": "new York",
    "name": "23",
    "time": "1464240091"
} 
{
    "city": "new York",
    "name": "24",
    "time": "1464240091"
}  
{
    "city": "new York",
    "name": "25",
    "time": "1464240091"
} 
{
    "city": "new York",
    "name": "26",
    "time": "1464240091"
}  
{
    "city": "new York",
    "name": "27",
    "time": "1464240091"
} 
{
    "city": "new York",
    "name": "28",
    "time": "1464240091"
} 
{
    "city": "new York",
    "name": "29",
    "time": "1464240091"
} 
{
    "city": "new York",
    "name": "30",
    "time": "1464240091"
} 
{
    "city": "new York",
    "name": "31",
    "time": "1464240091"
} 
{
    "city": "new York",
    "name": "32",
    "time": "1464240091"
}

```

then return：

```
[
    {
        "city": "new York",
        "name": "21",
        "time": "1464240091",
        "names": [
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8",
            "9",
            "10",
            "11",
            "12"
        ]
    },
    {
        "city": "Los Angeles",
        "name": "1",
        "time": "1464240096",
        "names": [
            "21",
            "22",
            "23",
            "24",
            "25",
            "26",
            "27",
            "28",
            "29",
            "30",
            "31",
            "32"
        ]
    }
]

```

the ultimate result I need meets :  
firstly: sorted by time ，  
secondly ：merge into 2 records finally

What should I do

---

<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:** [July 5, 2017, 10:43pm UTC](https://discuss.elastic.co/t/group-by-consolidation-filter-sort/52853/2 "2017-07-05T22:43:41Z")

</div>

Closed on July 5, 2017, 10:43pm UTC


