0

该手册说明了这一点(https://github.com/ankane/searchkick#nested-data):

嵌套数据

要查询嵌套数据,请使用点表示法。

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

这根本行不通,或者给出的示例需要更多资格。

就我而言,我有ExpenditureItemwhich belongs_to:Expenditure所以我试试这个:

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

结果什么都没有。我知道search_data我可以设置:

budget_id: expenditure.budget_id

但从这个例子看来,重点是能够完成快速简单的嵌套搜索。

我在这里错过了什么吗?

4

2 回答 2

0

Associations are not automatically indexed as nested data.

In your case, you would need to specifically include the associations that you want to index in your search_data method.

For example, to include all fields in your ExpenditureItem model as well as the Expenditure association, you could do:

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

If you don't need to index all attributes, ou can then of course modify the as_json method params to include or exclude the attributes and instance methods that you require in your index.

Also, to make indexing more performant, be sure to include the association in the search_import scope, e.g.

scope :search_import, -> { includes(:expenditure) }
于 2020-03-10T03:05:52.887 回答
0

嵌套查询只能在嵌套字段上执行,在您的映射中指定。您将不得不覆盖您的默认索引,这相当于在您的ElasticSearch 映射中明确定义它。

于 2020-03-08T22:37:43.220 回答