如何将嵌套索引映射到方法的返回结果?

时间:2014-12-08 21:48:17

标签: tire

假设我有这样的定义(简化为简洁)

class Charlie
  include Mongoid::Document

  include Tire::Model::Search
  include Tire::Model::Callbacks

  field :line1, type:String
  field :line2, type:String

  embedded_in :bravo

  def calculated_field
    ["From", "Charlie"]
  end
end

class Bravo
  include Mongoid::Document
  include Mongoid::Timestamps

  include Tire::Model::Search
  include Tire::Model::Callbacks

  store_in collection: "test123", database: "user_sourced_data"

  embeds_one :charlie, :autobuild => true

  field :first_name, type:String
  field :last_name, type:String

  mapping do
    indexes :first_name
    indexes :last_name
    indexes :outer_field, :as => 'calculated_field'
    indexes :charlie do
      indexes :line1
      indexes :line2
      indexes :embedded_field, :as =>'calculated_field'
    end
  end

  def calculated_field
    ["This", "is", "from", "Bravo"]
  end
end

在弹性搜索中使用值数组正确返回第一个“outer_field”属性,但是,弹性搜索中永远不会填充“embedded_field”

以下是ES的以下JSON输出:

{
_index: bravos
_type: bravo
_id: 54821f2a36b8d92713000001
_score: 0.30685282
_source: {
charlie: {
_id: 54821f2d36b8d92713000002,
line1: "ABC",
line2: "DEF"
}
first_name: "Johnny",
last_name: "Walker",
my_other_field: [
"This",
"is",
"from",
"Bravo"
]
}
}

我需要修改什么才能让它返回嵌入式类(而不是外部类)中“calculated_field”属性的结果

编辑:对象实例的to_indexed_json输出如下:

  {
   "charlie"   =>   {  
      "_id"      =>"54821f2d36b8d92713000002",
      "line1"      =>"ABC",
      "line2"      =>"DEF"
   },
   "first_name"   =>"Whiskey",
   "last_name"   =>"Tango",
   "my_other_field"   =>   [  "This", "is", "from", "Bravo" ]
   }

0 个答案:

没有答案