Howto sort all items found on the same geo_location by created_at date


(Martin Stabenfeldt) #1

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/8f24ee37-469c-4f5e-8e0a-941ae027ee88%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Binh Ly-2) #2

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/7ac31930-8f16-4ebb-9cb0-ed83629ac8f0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Martin Stabenfeldt) #3

On Thu, Apr 3, 2014 at 11:49 PM, Binh Ly binhly_es@yahoo.com wrote:

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

{
"sort": [
{ your_geo_sort },
{ "created_at": {} }
]
}

Perfect, thanks! :slight_smile:

--
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.
To view this discussion on the web visit 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.


(system) #4