1

因此,我正在使用elasticsearch-rails Gem 运行 Rails 5 应用程序以使用 Elasticsearch 5.4。

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

模型/关注/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

也许有人对此有一个快速的解决方法。

4

2 回答 2

1

当我正在开发一个低流量应用程序时,我使用了一个简单的解决方法:

由于 Geo_points 是在创建时设置的,而不是在更新时设置的,所以我只需删除文档并在触及 lat 或 long 时重新索引它。

user.__elasticsearch__.delete_document
user.__elasticsearch__.index_document

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

于 2017-06-20T11:11:42.400 回答
1

@Georg Keferböck

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

问候

于 2017-07-07T14:21:19.863 回答