[英]Elasticsearch-Rails & Elasticsearch 5.4: Geopoint not updating
所以我正在运行一个使用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
也许有人对此有快速解决方案。
在开发低流量应用程序时,我使用一种简单的解决方法:
由于Geo_points是在创建时设置的,而不是在更新时设置的,因此我只需删除文档并在碰到经纬度或经度时重新索引它。
user.__elasticsearch__.delete_document
user.__elasticsearch__.index_document
如果有人知道更好的解决方案,请发布。 我还记录了elasticsearch-rails的问题。
@乔治·科费伯克
您创建一个实例方法位置,该位置将返回{ lat: lat, lon: lng }
,然后可以在as_indexed_json
包含这样的方法。 这样一来,它会更加清洁,您以后也可以重复使用它;)
问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.