# Howto sort all items found on the same geo\_location by created\_at date

**URL:** <https://discuss.elastic.co/t/howto-sort-all-items-found-on-the-same-geo-location-by-created-at-date/16799>\
**Category:** Elasticsearch\
**Created:** [April 3, 2014, 6:03pm UTC](https://discuss.elastic.co/t/howto-sort-all-items-found-on-the-same-geo-location-by-created-at-date/16799 "2014-04-03T18:03:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Stabenfeldt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martin_stabenfeldt/32/1360_2.png) [@Martin\_Stabenfeldt](https://discuss.elastic.co/u/Martin_Stabenfeldt)\
**Post date:** [April 3, 2014, 6:03pm UTC](https://discuss.elastic.co/t/howto-sort-all-items-found-on-the-same-geo-location-by-created-at-date/16799/1 "2014-04-03T18:03:05Z")

</div>

Hi,

I have an Rails application like Craigs List. I'm using Rails4, ES 1.0 and  
elasticsearch-rails.  
When listing Ads, I want items that has the same latitude and longitude to  
be sorted by date.

How can I get ES to sort the ads that has the same geocoordinates by the  
created\_at field?

A ruby controller test that explains what I want:

it "Ads that have the same coordinates, are sorted by the newest first" do  
@oslo1 = Fabricate(:ad, title: 'oslo1', lat: A28\_LAT, long: A28\_LONG)  
@oslo2 = Fabricate(:ad, title: 'oslo2', lat: A28\_LAT, long: A28\_LONG)  
@oslo3 = Fabricate(:ad, title: 'oslo3', lat: A28\_LAT, long: A28\_LONG)

```
  @trondheim1 = Fabricate(:ad, title: 'trondheim1', lat: 63.4291382, long: 10.3941603)
  @trondheim2 = Fabricate(:ad, title: 'trondheim2', lat: 63.4291382, long: 10.3941603)
  @trondheim3 = Fabricate(:ad, title: 'trondheim3', lat: 63.4291382, long: 10.3941603)

  get :index, lat: A28_LAT, long: A28_LONG
  assigns(:ads).to_a.should eq [@oslo3, @oslo2, @oslo1, 
                                @trondheim3, @trondheim2, @trondheim1]

```

# 

\*# The query that should \*_list all active ads, sorted by distance._  
_#_

def self.list(lat, long)  
{  
query: {  
match\_all: {}  
},  
filter: {  
term: { state: "active" }  
},  
sort: [  
{  
\_geo\_distance: {  
coordinates: "#{lat}, #{long}",  
unit: "km"  
}  
}  
]  
}  
end

_My mapping_  
{  
"ads-production" : {  
"ad" : {  
"dynamic" : "false",  
"properties" : {  
"coordinates" : {  
"type" : "geo\_point"  
},  
"created\_at" : {  
"type" : "date",  
"format" : "dateOptionalTime"  
},  
"description" : {  
"type" : "string",  
"analyzer" : "norwegian"  
},  
"state" : {  
"type" : "string"  
},  
"title" : {  
"type" : "multi\_field",  
"fields" : {  
"title" : {  
"type" : "string",  
"analyzer" : "norwegian"  
},  
"title.ngram" : {  
"type" : "string",  
"index\_analyzer" : "ngram",  
"search\_analyzer" : "ngram\_search",  
"include\_in\_all" : false  
}  
}  
}  
}  
}  
}  
}

Cheers,  
Martin Stabenfeldt

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8f24ee37-469c-4f5e-8e0a-941ae027ee88%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8f24ee37-469c-4f5e-8e0a-941ae027ee88%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [April 3, 2014, 9:49pm UTC](https://discuss.elastic.co/t/howto-sort-all-items-found-on-the-same-geo-location-by-created-at-date/16799/2 "2014-04-03T21:49:29Z")

</div>

You should be able to do a multi-sort, like:

{  
"sort": [  
{ your\_geo\_sort },  
{ "created\_at": {} }  
]  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/7ac31930-8f16-4ebb-9cb0-ed83629ac8f0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7ac31930-8f16-4ebb-9cb0-ed83629ac8f0%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Martin\_Stabenfeldt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martin_stabenfeldt/32/1360_2.png) [@Martin\_Stabenfeldt](https://discuss.elastic.co/u/Martin_Stabenfeldt)\
**Post date:** [April 4, 2014, 8:19am UTC](https://discuss.elastic.co/t/howto-sort-all-items-found-on-the-same-geo-location-by-created-at-date/16799/3 "2014-04-04T08:19:43Z")

</div>

On Thu, Apr 3, 2014 at 11:49 PM, Binh Ly [binhly\_es@yahoo.com](mailto:binhly_es@yahoo.com) wrote:

> You should be able to do a multi-sort, like:
> 
> {  
> "sort": [  
> { your\_geo\_sort },  
> { "created\_at": {} }  
> ]  
> }

Perfect, thanks! 🙂

--  
Martin Stabenfeldt

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAPVOyG%3D4%2BqjnNihF8u-iE8JEhkKbVVGGmQ\_h4Yi7GS3VEEw7tA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPVOyG%3D4%2BqjnNihF8u-iE8JEhkKbVVGGmQ_h4Yi7GS3VEEw7tA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:38am UTC](https://discuss.elastic.co/t/howto-sort-all-items-found-on-the-same-geo-location-by-created-at-date/16799/4 "2017-07-06T01:38:14Z")

</div>


