因此,我正在使用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
也许有人对此有一个快速的解决方法。