我正在我的模型中实现 ElasticSearch 集成:
require 'elasticsearch/model'
class MissionDef < ActiveRecord::Base
# field: name (String(40))
# field: icon (String(2000))
# field: definition (String)
# field: public, boolean
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
def as_indexed_json(options={})
self.as_json(
only: [:id, :name]
)
end
end
在我的 Rails 控制台中,我做了:
MissionDef.import
MissionDef.first
MissionDef id: 226, ade_channel_key: "d403d658313e6c35ce", name: "Test Mission", icon: "/app/assets/images/badges/Showedup.png", definition: "{}", deleted_at: nil, created_at: "2015-08-04 11:30:08", updated_at: "2015-08-04 11:30:08", container_id: 883, public: true
我的查询
1) 当我使用除名称和 id 以外的其他字段值进行搜索时,它会为我提供搜索结果。例如:
result = MissionDef.search 'app' --- 有效 result.records.count => 1
我猜不应该是这种情况。
2) Missiondef.first.as_indexed_json => 不能正常工作,它给了我 o/p 作为 JSON 的整个对象
{"id"=>226, "ade_channel_key"=>"d403d658313e6c35ce", "name"=>"测试任务", "icon"=>"/app/assets/images/badges/Showedup.png", "定义" =>“{}”,“created_at”=>2015 年 8 月 4 日星期二 11:30:08 UTC +00:00,“updated_at”=>2015 年 8 月 4 日星期二 11:30:08 UTC +00:00,“ container_id"=>883, "public"=>true}