【发布时间】: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