我的模特协会:
class CertificatePeriod < ActiveRecord::Base
has_many :certificate_orders
has_many :order_transactions, through: :certificate_orders
end
class CertificateOrder < ActiveRecord::Base
belongs_to :user
belongs_to :certificate_period
has_one :order_transaction
end
class OrderTransaction < ActiveRecord::Base
belongs_to :certificate_order
end
在OrderTransaction模型中:
class OrderTransaction < ActiveRecord::Base
def as_indexed_json(options={})
self.as_json(only: [:id, cardholdername, :userid],
include: {certificate_order: {include: {user: {only: [:first_name, :last_name], methods: :full_name}}}})
end
end
此索引的响应示例:
{"id"=>7235,
"cardholdername"=>"NUR SARUHAN",
"userid"=>"zzzyyyxxxx",
"certificate_order"=>
{"id"=>292,
"user_id"=>13186,
"certificate_enrollment_id"=>768,
"slug"=>"eafe0be5-0b1f-4e4f-8929-d3f9330a4a86f7c25b1199dcd5",
"created_at"=>Mon, 11 Apr 2016 18:30:58 EEST +03:00,
"updated_at"=>Mon, 11 Apr 2016 18:30:58 EEST +03:00,
"user"=>{"first_name"=>"Nur", "last_name"=>"SARUHAN", "full_name"=>"NUR SARUHAN"}}}
我使用过这样的搜索查询:
query: {
bool: {
must: [
{ids: {values: @certificate_period.order_transactions.pluck(:id)}},
{bool: {
should: [
{multi_match: {query: params[:q], fields: ['cardholdername', 'certificate_order.user.first_name']}}
]
}}
]
}
}).records.to_a
我的问题是:
实际上这个搜索查询已经有效,但以下几行返回完全相同的记录。
{multi_match: {query: params[:q], fields: ['cardholdername', 'certificate_order.user.first_name']}}
和
{multi_match: {query: params[:q], fields: ['cardholdername']}}
如果我只是在multi_match字段中使用certificate_order.user.first_name
作为参数,则会返回一个空数组。所以certificate_order.user.first_name
没有效果。我用Google搜索了3天,但我找不到正确的答案。我该怎么做才能获得certificate_order.user.first_name
参数的值?