Ruby on rails ElasticSearch Rails多匹配字段
我的模特儿协会:Ruby on rails ElasticSearch Rails多匹配字段,ruby-on-rails,
elasticsearch,Ruby On Rails,
elasticsearch,我的模特儿协会: 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_peri
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
我使用过类似以下内容的搜索查询:
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']}}
及
如果我只使用
certificate\u order.user.first\u name
作为多匹配字段中的参数,则返回一个空数组。因此,certificate\u order.user.first\u name
不起作用。我在谷歌上搜索了3天,但还没有找到正确的答案。我应该如何获取证书\u order.user.first\u name
参数的值?我建议使用gem。您可以在搜索查询中合并模型之间的不同属性,以帮助更改搜索范围。总的来说,这是一块很好的宝石
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']}}