Searchkick嵌套数据

问题描述 投票:0回答:2

手册指出了这一点[https://github.com/ankane/searchkick#nested-data):

嵌套数据

要查询嵌套数据,请使用点符号。

User.search "san", fields: ["address.city"], where: {"address.zip_code" => 12345}

这根本不起作用,或者给出的示例需要更多的限定条件。

在我的情况下,我有ExpenditureItem其中属于:Expenditure,所以我尝试这样做:

ExpenditureItem.search("*", where: { "expenditure.budget_id": '2'})

没有任何结果。我知道在search_data中可以设置:

budget_id: expenditure.budget_id

但是从该示例看来,最重要的是能够完成快速且容易的嵌套搜索。

我在这里想念东西吗?

elasticsearch searchkick elasticsearch-nested
2个回答
0
投票

嵌套查询只能在映射中指定的嵌套字段上执行。您将必须override您的默认索引,这等效于在ElasticSearch mapping中显式定义它。


0
投票

关联不会自动索引为嵌套数据。

在您的情况下,您需要在search_data方法中明确包含要索引的关联。

例如,要包括ExpenditureItem模型中的所有字段以及Expenditure关联,您可以执行以下操作:

def search_data
  as_json.merge({
    expenditure: expenditure.as_json
  })
end

如果不需要索引所有属性,那么您当然可以修改as_json方法参数以包括或排除索引中所需的属性和实例方法。

此外,要使索引的性能更高,请确保在include the association范围内使用search_import,例如

scope :search_import, -> { includes(:expenditure) }
© www.soinside.com 2019 - 2024. All rights reserved.