为什么我的 searchkick has_many 和 HABTM 关联不起作用?
Why do my searchkick has_many and HABTM association not work?
我最近设置了 searchkick,它对我已编制索引的模型的属性非常有效。
然而,当涉及到协会时,它就失败了。
这是我的关联模型和 search_data
方法:
class Profile < ActiveRecord::Base
searchkick
has_and_belongs_to_many :positions
belongs_to :school
def search_data
{
name: name,
bib_color: bib_color,
height: height,
weight: weight,
player_type: player_type,
school_name: school.name,
age: age,
position_name: positions.map(&:name)
}
end
end
我确保 运行 Profile.reindex
,但是当我 运行 查询 Center Back
,这是 position
的名称时,它return当我知道有结果时,这是一个空查询集。
> Profile.search("center back").to_a
Profile Search (27.5ms) curl http://localhost:9200/profiles_development/_search?pretty -d '{"query":{"dis_max":{"queries":[{"match":{"_all":{"query":"center back","boost":10,"operator":"and","analyzer":"searchkick_search"}}},{"match":{"_all":{"query":"center back","boost":10,"operator":"and","analyzer":"searchkick_search2"}}},{"match":{"_all":{"query":"center back","boost":1,"operator":"and","analyzer":"searchkick_search","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}},{"match":{"_all":{"query":"center back","boost":1,"operator":"and","analyzer":"searchkick_search2","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}}]}},"size":1000,"from":0,"timeout":"11s","_source":false}'
=> []
但这里是其他结果:
> p
=> #<Position:0x007fa881566310 id: 1, created_at: Sun, 04 Sep 2016 06:49:45 UTC +00:00, updated_at: Wed, 14 Sep 2016 06:17:02 UTC +00:00, name: "Center Back">
> p.profiles.count
(4.1ms) SELECT COUNT(*) FROM "profiles" INNER JOIN "positions_profiles" ON "profiles"."id" = "positions_profiles"."profile_id" WHERE "positions_profiles"."position_id" = [["position_id", 1]]
=> 5
应该至少有 5 个 profiles
,但结果 return 为空。
我什至试过像这样格式化我的 search_data
:
def search_data
attrs = attributes.dup
relational = {
school_name: school.name,
grade_name: grades.map(&:subject),
position_names: positions.map(&:name)
}
attrs.merge! relational
end
如果我尝试常规 has_many
关联并相应地声明它,也会发生同样的事情。
可能是什么原因导致的,我该如何解决?
所以事实证明 search_data
实际上是正确的,但我没有意识到我必须 运行 rails searchkick:reindex:all
命令,而不是只执行 Profile.reindex
在我的 rails console
中是 运行ning。
一旦我 运行 该命令,我注意到它重新索引了所有内容,包括所有关联,现在它就像一个魅力!
我最近设置了 searchkick,它对我已编制索引的模型的属性非常有效。
然而,当涉及到协会时,它就失败了。
这是我的关联模型和 search_data
方法:
class Profile < ActiveRecord::Base
searchkick
has_and_belongs_to_many :positions
belongs_to :school
def search_data
{
name: name,
bib_color: bib_color,
height: height,
weight: weight,
player_type: player_type,
school_name: school.name,
age: age,
position_name: positions.map(&:name)
}
end
end
我确保 运行 Profile.reindex
,但是当我 运行 查询 Center Back
,这是 position
的名称时,它return当我知道有结果时,这是一个空查询集。
> Profile.search("center back").to_a
Profile Search (27.5ms) curl http://localhost:9200/profiles_development/_search?pretty -d '{"query":{"dis_max":{"queries":[{"match":{"_all":{"query":"center back","boost":10,"operator":"and","analyzer":"searchkick_search"}}},{"match":{"_all":{"query":"center back","boost":10,"operator":"and","analyzer":"searchkick_search2"}}},{"match":{"_all":{"query":"center back","boost":1,"operator":"and","analyzer":"searchkick_search","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}},{"match":{"_all":{"query":"center back","boost":1,"operator":"and","analyzer":"searchkick_search2","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}}]}},"size":1000,"from":0,"timeout":"11s","_source":false}'
=> []
但这里是其他结果:
> p
=> #<Position:0x007fa881566310 id: 1, created_at: Sun, 04 Sep 2016 06:49:45 UTC +00:00, updated_at: Wed, 14 Sep 2016 06:17:02 UTC +00:00, name: "Center Back">
> p.profiles.count
(4.1ms) SELECT COUNT(*) FROM "profiles" INNER JOIN "positions_profiles" ON "profiles"."id" = "positions_profiles"."profile_id" WHERE "positions_profiles"."position_id" = [["position_id", 1]]
=> 5
应该至少有 5 个 profiles
,但结果 return 为空。
我什至试过像这样格式化我的 search_data
:
def search_data
attrs = attributes.dup
relational = {
school_name: school.name,
grade_name: grades.map(&:subject),
position_names: positions.map(&:name)
}
attrs.merge! relational
end
如果我尝试常规 has_many
关联并相应地声明它,也会发生同样的事情。
可能是什么原因导致的,我该如何解决?
所以事实证明 search_data
实际上是正确的,但我没有意识到我必须 运行 rails searchkick:reindex:all
命令,而不是只执行 Profile.reindex
在我的 rails console
中是 运行ning。
一旦我 运行 该命令,我注意到它重新索引了所有内容,包括所有关联,现在它就像一个魅力!