2017-06-19 34 views
1

所以我正在使用elasticsearch-rails寶石與Elasticsearch 5.4工作一個Rails 5應用程序。Elasticsearch-Rails的&Elasticsearch 5.4:GeoPoint對象不更新

一切工作正常,我遇到的唯一問題是,當我使用地理編碼更新位置來檢索新的經度和緯度時,座標不會在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 

也許有人有這種速戰速決。

+0

你能檢查某個對象,'as_indexed_json'以檢查是否有位置呢?也許問題是你不發送位置ES上更新 – zauzaj

+0

一件事,你確定當你更新它的保存/更新的對象也定位? – zauzaj

+0

嗨Zauzaj,是的,有一個位置(我可以查詢它,看到它在Kibana等)。不知道您的上傳位置是什麼意思。我更新了發送到數據庫的代碼中的地理編碼lat和lon。 elasticsearch-rails更新所有屬性(位置除外)。如果我從self.as_json的except子句中刪除lat,lng,float/decimal值會得到正確更新。但是,Geo_point不會更新。這使我相信這是一個錯誤,除非有一些東西需要做,我不知道,我不能對谷歌或文檔:( –

回答

1

@GeorgKeferböck

你讓這將返回{ lat: lat, lon: lng }一個實例方法的位置,然後你可以只包括內部as_indexed_json這樣的方法。 這樣它會更清潔,你也可以事後再使用它;)

問候

+0

這工作,因爲你的'after_touch(){__elasticsearch __ index_document。}'但誰都應該知道,計算機/派生屬性將不會自動更新,由於這個錯誤:https://github.com/elastic/elasticsearch-導軌/拉/ 182 – Meekohi

+0

好點!謝謝@Meekohi – zauzaj

1

由於我在低流量應用工作,我用一個簡單的解決方法:

由於Geo_points被設置上創建,但不是在更新,我直接刪除該文件,並重新索引它時,緯度或長正在接觸。

user.__elasticsearch__.delete_document 
user.__elasticsearch__.index_document 

如果有人知道更好的解決方案,請發佈。我還用elasticsearch-rails登錄了一個問題。