Elasticsearch-Rails & Elasticsearch 5.4:地理点不更新

Elasticsearch-Rails & Elasticsearch 5.4: Geopoint not updating

所以我是 运行 一个 Rails 5 应用程序,使用 elasticsearch-rails Gem 与 Elasticsearch 5.4 一起工作。

一切正常,我遇到的唯一问题是当我使用地理编码更新位置以检索新的经度和纬度时,坐标不会在 Elasticsearch 中更新(使用 Geopoint)。然而,它正确地反映在数据库中,这意味着地理编码等工作正常。

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

也许有人可以快速解决这个问题。

由于我正在处理低流量应用程序,因此我使用了一个简单的解决方法:

由于 Geo_points 是在创建时设置的,而不是在更新时设置的,我只是删除文档并在触及纬度或经度时重新索引它。

user.__elasticsearch__.delete_document
user.__elasticsearch__.index_document

如果有人知道更好的解决方案,请post。我还记录了 elasticsearch-rails.

的问题

@Georg Keferböck

你创建一个实例方法位置,它将 return { lat: lat, lon: lng } 然后你可以在 as_indexed_json 中包含这样的方法。 这样它会更干净,而且你以后可以重复使用它;)

此致