简体   繁体   中英

Elasticsearch-Rails & Elasticsearch 5.4: Geopoint not updating

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.

As I am working on a low traffic application I use a simple work-around:

As Geo_points are being set on create, but not on update, I simply delete the document and reindex it whenever lat or long is being touched.

user.__elasticsearch__.delete_document
user.__elasticsearch__.index_document

If anyone knows a better solution, please post. I also logged an issue with elasticsearch-rails .

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM