So I am running a Rails 5 application using elasticsearch-rails Gem to work with Elasticsearch 5.4.
Everything works fine, the only problem I am having is that when I update the location using Geocoding to retrieve the new longitude and latitude, the coordinates do not get updated in Elasticsearch (using Geopoint). It is however correctly reflected in the database, meaning Geocoding, etc. works fine.
models/concerns/user_searchable.rb
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
index_name Rails.application.class.parent_name.underscore
document_type self.name.downcase
settings index: { number_of_shards: 1 } do
mapping dynamic: false do
indexes :description, analyzer: 'english'
indexes :tagline, analyzer: 'english'
indexes :username
indexes :location, type: 'geo_point'
indexes :active, type: 'boolean'
...
end
end
after_touch() { __elasticsearch__.index_document }
def as_indexed_json(_options = {})
self.as_json(
except: [:email, :lat, :lng, :status, :termsofuse, :v_code],
include: {
photos: { only: [:name, :caption, :active, :image_data] }
).merge(
location: {lat: lat, lon: lng},
age: birthday.nil? ? 18 : ((Date.today - birthday.to_date) / 365.25).floor
)
end
Perhaps someone has a quick fix for this.
@Georg Keferböck
You make a instance method location which will return { lat: lat, lon: lng }
and then you can just include such method inside as_indexed_json
.
That way it will be much more cleaner and also you can reuse it afterwards ;)
Regards