【问题标题】:Elasticsearch-Rails & Elasticsearch 5.4: Geopoint not updatingElasticsearch-Rails 和 Elasticsearch 5.4:Geopoint 未更新
【发布时间】:2017-11-21 16:54:38
【问题描述】:

所以我正在运行一个使用 elasticsearch-rails Gem 的 Rails 5 应用程序来使用 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

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

【问题讨论】:

  • 你能在某个对象上检查as_indexed_json 以检查是否有位置吗?也许问题是您没有在更新时向 ES 发送位置
  • 还有一件事,你确定当你更新位置时它也会保存/更新到对象上吗?
  • 嗨 Zauzaj,是的,有一个位置(我可以查询它,在 Kibana 中查看它等)。不知道你说的上传位置是什么意思。我在发送到数据库的代码中更新地理编码 lat und lon。 elasticsearch-rails 更新所有属性(位置除外)。如果我从 self.as_json 中的 except 子句中删除 lat, lng,浮点/十进制值确实会正确更新。但同样,Geo_point 没有得到更新。这让我相信存在一个错误,除非有一些我不知道并且我在谷歌或文档上找不到的需要完成的事情:(
  • 我不是 100% 确定,但我猜 as_indexed_json 内部的合并会造成一些混乱。你可以创建一个实例方法location,它会返回 {lat: lat, lon: lng} 然后你可以在as_indexed_json 中包含这样的方法吗?
  • 好主意!你可能在这里有所收获。将在晚上进行测试并在此处发布结果。 :)

标签: ruby-on-rails elasticsearch elasticsearch-rails


【解决方案1】:

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

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

user.__elasticsearch__.delete_document
user.__elasticsearch__.index_document

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

【讨论】:

    【解决方案2】:

    @Georg Keferböck

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

    问候

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-31
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    相关资源
    最近更新 更多