这是我的pg_search
方法:
pg_search_scope :node_search, against: [:name, :user_id, :circa, :cached_tagged_user_names, :cached_user_tag_list],
using: { tsearch: { any_word: true, dictionary: :english, prefix: true} },
:associated_against => {
comments: [:message],
user: [:first_name, :last_name, :email],
memberships: [:relation],
user_tags: [:name]
}
不是prefix: true
的{{1}},而是docs recommend。
但是,至少在一个特定情况下,它不像我期望的那样工作。
我有一个tsearch
对象,如下所示:
Node
请注意属性:=> [#<Node id: 85, name: "House Fire 2", family_tree_id: 57, user_id: 57, media_id: 228, media_type: "Video", created_at: "2015-05-15 00:20:26", updated_at: "2015-06-08 22:25:49", circa: nil, is_comment: nil, cached_votes_total: 0, cached_votes_score: 0, cached_votes_up: 0, cached_votes_down: 0, cached_weighted_score: 0, cached_weighted_total: 0, cached_weighted_average: 0.0, cached_user_tag_list: "danny@test.com, abc@test.com", cached_num_user_tags: 2, cached_tagged_user_names: ["Daniel Marty", "Marcamus Prime"]>]
,其中包含电子邮件地址:cached_user_tag_list
。
然而,当我进行以下搜索时,这些是我得到的结果:
danny@test.com
请注意,它会在查询[185] pry(main)> Node.node_search("dann")
Node Load (11.3ms) SELECT "nodes".*, ((ts_rank((to_tsvector('english', coalesce("nodes"."name"::text, '')) || to_tsvector('english', coalesce("nodes"."user_id"::text, '')) || to_tsvector('english', coalesce("nodes"."circa"::text, '')) || to_tsvector('english', coalesce("nodes"."cached_tagged_user_names"::text, '')) || to_tsvector('english', coalesce("nodes"."cached_user_tag_list"::text, '')) || to_tsvector('english', coalesce(pg_search_121ea89914a721445aee70.pg_search_344e3c62d13849726da22e::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_acfcdbc0b3d3a65f40eab7::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_eeb842708b24d4f7fcf549::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_cb8eb1b84bf18ee1412ffd::text, '')) || to_tsvector('english', coalesce(pg_search_b1bc4c0f44e7f4799d8caf.pg_search_9474fb5b090e75ade5136f::text, '')) || to_tsvector('english', coalesce(pg_search_93dd21271636bd02b16bc3.pg_search_484f77386a5aecc6e01094::text, ''))), (to_tsquery('english', ''' ' || 'dann' || ' ''' || ':*')), 0))) AS pg_search_rank FROM "nodes" LEFT OUTER JOIN (SELECT "nodes"."id" AS id, string_agg("comments"."message"::text, ' ') AS pg_search_344e3c62d13849726da22e FROM "nodes" INNER JOIN "comments" ON "comments"."node_id" = "nodes"."id" GROUP BY "nodes"."id") pg_search_121ea89914a721445aee70 ON pg_search_121ea89914a721445aee70.id = "nodes"."id" LEFT OUTER JOIN (SELECT "nodes"."id" AS id, string_agg("users"."first_name"::text, ' ') AS pg_search_acfcdbc0b3d3a65f40eab7, string_agg("users"."last_name"::text, ' ') AS pg_search_eeb842708b24d4f7fcf549, string_agg("users"."email"::text, ' ') AS pg_search_cb8eb1b84bf18ee1412ffd FROM "nodes" INNER JOIN "users" ON "users"."id" = "nodes"."user_id" GROUP BY "nodes"."id") pg_search_4aab10cdca183dac25f479 ON pg_search_4aab10cdca183dac25f479.id = "nodes"."id" LEFT OUTER JOIN (SELECT "nodes"."id" AS id, string_agg("memberships"."relation"::text, ' ') AS pg_search_9474fb5b090e75ade5136f FROM "nodes" INNER JOIN "family_trees" ON "family_trees"."id" = "nodes"."family_tree_id" INNER JOIN "memberships" ON "memberships"."family_tree_id" = "family_trees"."id" GROUP BY "nodes"."id") pg_search_b1bc4c0f44e7f4799d8caf ON pg_search_b1bc4c0f44e7f4799d8caf.id = "nodes"."id" LEFT OUTER JOIN (SELECT "nodes"."id" AS id, string_agg("tags"."name"::text, ' ') AS pg_search_484f77386a5aecc6e01094 FROM "nodes" INNER JOIN "taggings" ON "taggings"."taggable_id" = "nodes"."id" AND "taggings"."taggable_type" = 'Node' AND "taggings"."context" = 'user_tags' INNER JOIN "tags" ON "tags"."id" = "taggings"."tag_id" GROUP BY "nodes"."id") pg_search_93dd21271636bd02b16bc3 ON pg_search_93dd21271636bd02b16bc3.id = "nodes"."id" WHERE (((to_tsvector('english', coalesce("nodes"."name"::text, '')) || to_tsvector('english', coalesce("nodes"."user_id"::text, '')) || to_tsvector('english', coalesce("nodes"."circa"::text, '')) || to_tsvector('english', coalesce("nodes"."cached_tagged_user_names"::text, '')) || to_tsvector('english', coalesce("nodes"."cached_user_tag_list"::text, '')) || to_tsvector('english', coalesce(pg_search_121ea89914a721445aee70.pg_search_344e3c62d13849726da22e::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_acfcdbc0b3d3a65f40eab7::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_eeb842708b24d4f7fcf549::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_cb8eb1b84bf18ee1412ffd::text, '')) || to_tsvector('english', coalesce(pg_search_b1bc4c0f44e7f4799d8caf.pg_search_9474fb5b090e75ade5136f::text, '')) || to_tsvector('english', coalesce(pg_search_93dd21271636bd02b16bc3.pg_search_484f77386a5aecc6e01094::text, ''))) @@ (to_tsquery('english', ''' ' || 'dann' || ' ''' || ':*')))) ORDER BY pg_search_rank DESC, "nodes"."id" ASC
=> [#<Node id: 85, name: "House Fire 2", family_tree_id: 57, user_id: 57, media_id: 228, media_type: "Video", created_at: "2015-05-15 00:20:26", updated_at: "2015-06-08 22:25:49", circa: nil, is_comment: nil, cached_votes_total: 0, cached_votes_score: 0, cached_votes_up: 0, cached_votes_down: 0, cached_weighted_score: 0, cached_weighted_total: 0, cached_weighted_average: 0.0, cached_user_tag_list: "danny@test.com, abc@test.com", cached_num_user_tags: 2, cached_tagged_user_names: ["Daniel Marty", "Marcamus Prime"]>]
[186] pry(main)> Node.node_search("dan")
Node Load (4.2ms) SELECT "nodes".*, ((ts_rank((to_tsvector('english', coalesce("nodes"."name"::text, '')) || to_tsvector('english', coalesce("nodes"."user_id"::text, '')) || to_tsvector('english', coalesce("nodes"."circa"::text, '')) || to_tsvector('english', coalesce("nodes"."cached_tagged_user_names"::text, '')) || to_tsvector('english', coalesce("nodes"."cached_user_tag_list"::text, '')) || to_tsvector('english', coalesce(pg_search_121ea89914a721445aee70.pg_search_344e3c62d13849726da22e::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_acfcdbc0b3d3a65f40eab7::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_eeb842708b24d4f7fcf549::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_cb8eb1b84bf18ee1412ffd::text, '')) || to_tsvector('english', coalesce(pg_search_b1bc4c0f44e7f4799d8caf.pg_search_9474fb5b090e75ade5136f::text, '')) || to_tsvector('english', coalesce(pg_search_93dd21271636bd02b16bc3.pg_search_484f77386a5aecc6e01094::text, ''))), (to_tsquery('english', ''' ' || 'dan' || ' ''' || ':*')), 0))) AS pg_search_rank FROM "nodes" LEFT OUTER JOIN (SELECT "nodes"."id" AS id, string_agg("comments"."message"::text, ' ') AS pg_search_344e3c62d13849726da22e FROM "nodes" INNER JOIN "comments" ON "comments"."node_id" = "nodes"."id" GROUP BY "nodes"."id") pg_search_121ea89914a721445aee70 ON pg_search_121ea89914a721445aee70.id = "nodes"."id" LEFT OUTER JOIN (SELECT "nodes"."id" AS id, string_agg("users"."first_name"::text, ' ') AS pg_search_acfcdbc0b3d3a65f40eab7, string_agg("users"."last_name"::text, ' ') AS pg_search_eeb842708b24d4f7fcf549, string_agg("users"."email"::text, ' ') AS pg_search_cb8eb1b84bf18ee1412ffd FROM "nodes" INNER JOIN "users" ON "users"."id" = "nodes"."user_id" GROUP BY "nodes"."id") pg_search_4aab10cdca183dac25f479 ON pg_search_4aab10cdca183dac25f479.id = "nodes"."id" LEFT OUTER JOIN (SELECT "nodes"."id" AS id, string_agg("memberships"."relation"::text, ' ') AS pg_search_9474fb5b090e75ade5136f FROM "nodes" INNER JOIN "family_trees" ON "family_trees"."id" = "nodes"."family_tree_id" INNER JOIN "memberships" ON "memberships"."family_tree_id" = "family_trees"."id" GROUP BY "nodes"."id") pg_search_b1bc4c0f44e7f4799d8caf ON pg_search_b1bc4c0f44e7f4799d8caf.id = "nodes"."id" LEFT OUTER JOIN (SELECT "nodes"."id" AS id, string_agg("tags"."name"::text, ' ') AS pg_search_484f77386a5aecc6e01094 FROM "nodes" INNER JOIN "taggings" ON "taggings"."taggable_id" = "nodes"."id" AND "taggings"."taggable_type" = 'Node' AND "taggings"."context" = 'user_tags' INNER JOIN "tags" ON "tags"."id" = "taggings"."tag_id" GROUP BY "nodes"."id") pg_search_93dd21271636bd02b16bc3 ON pg_search_93dd21271636bd02b16bc3.id = "nodes"."id" WHERE (((to_tsvector('english', coalesce("nodes"."name"::text, '')) || to_tsvector('english', coalesce("nodes"."user_id"::text, '')) || to_tsvector('english', coalesce("nodes"."circa"::text, '')) || to_tsvector('english', coalesce("nodes"."cached_tagged_user_names"::text, '')) || to_tsvector('english', coalesce("nodes"."cached_user_tag_list"::text, '')) || to_tsvector('english', coalesce(pg_search_121ea89914a721445aee70.pg_search_344e3c62d13849726da22e::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_acfcdbc0b3d3a65f40eab7::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_eeb842708b24d4f7fcf549::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_cb8eb1b84bf18ee1412ffd::text, '')) || to_tsvector('english', coalesce(pg_search_b1bc4c0f44e7f4799d8caf.pg_search_9474fb5b090e75ade5136f::text, '')) || to_tsvector('english', coalesce(pg_search_93dd21271636bd02b16bc3.pg_search_484f77386a5aecc6e01094::text, ''))) @@ (to_tsquery('english', ''' ' || 'dan' || ' ''' || ':*')))) ORDER BY pg_search_rank DESC, "nodes"."id" ASC
=> [#<Node id: 85, name: "House Fire 2", family_tree_id: 57, user_id: 57, media_id: 228, media_type: "Video", created_at: "2015-05-15 00:20:26", updated_at: "2015-06-08 22:25:49", circa: nil, is_comment: nil, cached_votes_total: 0, cached_votes_score: 0, cached_votes_up: 0, cached_votes_down: 0, cached_weighted_score: 0, cached_weighted_total: 0, cached_weighted_average: 0.0, cached_user_tag_list: "danny@test.com, abc@test.com", cached_num_user_tags: 2, cached_tagged_user_names: ["Daniel Marty", "Marcamus Prime"]>]
[187] pry(main)> Node.node_search("danny")
Node Load (2.8ms) SELECT "nodes".*, ((ts_rank((to_tsvector('english', coalesce("nodes"."name"::text, '')) || to_tsvector('english', coalesce("nodes"."user_id"::text, '')) || to_tsvector('english', coalesce("nodes"."circa"::text, '')) || to_tsvector('english', coalesce("nodes"."cached_tagged_user_names"::text, '')) || to_tsvector('english', coalesce("nodes"."cached_user_tag_list"::text, '')) || to_tsvector('english', coalesce(pg_search_121ea89914a721445aee70.pg_search_344e3c62d13849726da22e::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_acfcdbc0b3d3a65f40eab7::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_eeb842708b24d4f7fcf549::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_cb8eb1b84bf18ee1412ffd::text, '')) || to_tsvector('english', coalesce(pg_search_b1bc4c0f44e7f4799d8caf.pg_search_9474fb5b090e75ade5136f::text, '')) || to_tsvector('english', coalesce(pg_search_93dd21271636bd02b16bc3.pg_search_484f77386a5aecc6e01094::text, ''))), (to_tsquery('english', ''' ' || 'danny' || ' ''' || ':*')), 0))) AS pg_search_rank FROM "nodes" LEFT OUTER JOIN (SELECT "nodes"."id" AS id, string_agg("comments"."message"::text, ' ') AS pg_search_344e3c62d13849726da22e FROM "nodes" INNER JOIN "comments" ON "comments"."node_id" = "nodes"."id" GROUP BY "nodes"."id") pg_search_121ea89914a721445aee70 ON pg_search_121ea89914a721445aee70.id = "nodes"."id" LEFT OUTER JOIN (SELECT "nodes"."id" AS id, string_agg("users"."first_name"::text, ' ') AS pg_search_acfcdbc0b3d3a65f40eab7, string_agg("users"."last_name"::text, ' ') AS pg_search_eeb842708b24d4f7fcf549, string_agg("users"."email"::text, ' ') AS pg_search_cb8eb1b84bf18ee1412ffd FROM "nodes" INNER JOIN "users" ON "users"."id" = "nodes"."user_id" GROUP BY "nodes"."id") pg_search_4aab10cdca183dac25f479 ON pg_search_4aab10cdca183dac25f479.id = "nodes"."id" LEFT OUTER JOIN (SELECT "nodes"."id" AS id, string_agg("memberships"."relation"::text, ' ') AS pg_search_9474fb5b090e75ade5136f FROM "nodes" INNER JOIN "family_trees" ON "family_trees"."id" = "nodes"."family_tree_id" INNER JOIN "memberships" ON "memberships"."family_tree_id" = "family_trees"."id" GROUP BY "nodes"."id") pg_search_b1bc4c0f44e7f4799d8caf ON pg_search_b1bc4c0f44e7f4799d8caf.id = "nodes"."id" LEFT OUTER JOIN (SELECT "nodes"."id" AS id, string_agg("tags"."name"::text, ' ') AS pg_search_484f77386a5aecc6e01094 FROM "nodes" INNER JOIN "taggings" ON "taggings"."taggable_id" = "nodes"."id" AND "taggings"."taggable_type" = 'Node' AND "taggings"."context" = 'user_tags' INNER JOIN "tags" ON "tags"."id" = "taggings"."tag_id" GROUP BY "nodes"."id") pg_search_93dd21271636bd02b16bc3 ON pg_search_93dd21271636bd02b16bc3.id = "nodes"."id" WHERE (((to_tsvector('english', coalesce("nodes"."name"::text, '')) || to_tsvector('english', coalesce("nodes"."user_id"::text, '')) || to_tsvector('english', coalesce("nodes"."circa"::text, '')) || to_tsvector('english', coalesce("nodes"."cached_tagged_user_names"::text, '')) || to_tsvector('english', coalesce("nodes"."cached_user_tag_list"::text, '')) || to_tsvector('english', coalesce(pg_search_121ea89914a721445aee70.pg_search_344e3c62d13849726da22e::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_acfcdbc0b3d3a65f40eab7::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_eeb842708b24d4f7fcf549::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_cb8eb1b84bf18ee1412ffd::text, '')) || to_tsvector('english', coalesce(pg_search_b1bc4c0f44e7f4799d8caf.pg_search_9474fb5b090e75ade5136f::text, '')) || to_tsvector('english', coalesce(pg_search_93dd21271636bd02b16bc3.pg_search_484f77386a5aecc6e01094::text, ''))) @@ (to_tsquery('english', ''' ' || 'danny' || ' ''' || ':*')))) ORDER BY pg_search_rank DESC, "nodes"."id" ASC
=> []
上返回空结果,但会在查询danny
和dan
上返回正确的记录。
鉴于该字符串是dann
,该字符串的前缀不是danny@test.com
吗?
请注意,对于电子邮件地址danny
,搜索abc@test.com
会返回正确的结果,如下所示:
abc
这可能是什么原因?
修改1
为了让事情变得更奇怪,我认为[188] pry(main)> Node.node_search("abc")
Node Load (6.9ms) SELECT "nodes".*, ((ts_rank((to_tsvector('english', coalesce("nodes"."name"::text, '')) || to_tsvector('english', coalesce("nodes"."user_id"::text, '')) || to_tsvector('english', coalesce("nodes"."circa"::text, '')) || to_tsvector('english', coalesce("nodes"."cached_tagged_user_names"::text, '')) || to_tsvector('english', coalesce("nodes"."cached_user_tag_list"::text, '')) || to_tsvector('english', coalesce(pg_search_121ea89914a721445aee70.pg_search_344e3c62d13849726da22e::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_acfcdbc0b3d3a65f40eab7::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_eeb842708b24d4f7fcf549::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_cb8eb1b84bf18ee1412ffd::text, '')) || to_tsvector('english', coalesce(pg_search_b1bc4c0f44e7f4799d8caf.pg_search_9474fb5b090e75ade5136f::text, '')) || to_tsvector('english', coalesce(pg_search_93dd21271636bd02b16bc3.pg_search_484f77386a5aecc6e01094::text, ''))), (to_tsquery('english', ''' ' || 'abc' || ' ''' || ':*')), 0))) AS pg_search_rank FROM "nodes" LEFT OUTER JOIN (SELECT "nodes"."id" AS id, string_agg("comments"."message"::text, ' ') AS pg_search_344e3c62d13849726da22e FROM "nodes" INNER JOIN "comments" ON "comments"."node_id" = "nodes"."id" GROUP BY "nodes"."id") pg_search_121ea89914a721445aee70 ON pg_search_121ea89914a721445aee70.id = "nodes"."id" LEFT OUTER JOIN (SELECT "nodes"."id" AS id, string_agg("users"."first_name"::text, ' ') AS pg_search_acfcdbc0b3d3a65f40eab7, string_agg("users"."last_name"::text, ' ') AS pg_search_eeb842708b24d4f7fcf549, string_agg("users"."email"::text, ' ') AS pg_search_cb8eb1b84bf18ee1412ffd FROM "nodes" INNER JOIN "users" ON "users"."id" = "nodes"."user_id" GROUP BY "nodes"."id") pg_search_4aab10cdca183dac25f479 ON pg_search_4aab10cdca183dac25f479.id = "nodes"."id" LEFT OUTER JOIN (SELECT "nodes"."id" AS id, string_agg("memberships"."relation"::text, ' ') AS pg_search_9474fb5b090e75ade5136f FROM "nodes" INNER JOIN "family_trees" ON "family_trees"."id" = "nodes"."family_tree_id" INNER JOIN "memberships" ON "memberships"."family_tree_id" = "family_trees"."id" GROUP BY "nodes"."id") pg_search_b1bc4c0f44e7f4799d8caf ON pg_search_b1bc4c0f44e7f4799d8caf.id = "nodes"."id" LEFT OUTER JOIN (SELECT "nodes"."id" AS id, string_agg("tags"."name"::text, ' ') AS pg_search_484f77386a5aecc6e01094 FROM "nodes" INNER JOIN "taggings" ON "taggings"."taggable_id" = "nodes"."id" AND "taggings"."taggable_type" = 'Node' AND "taggings"."context" = 'user_tags' INNER JOIN "tags" ON "tags"."id" = "taggings"."tag_id" GROUP BY "nodes"."id") pg_search_93dd21271636bd02b16bc3 ON pg_search_93dd21271636bd02b16bc3.id = "nodes"."id" WHERE (((to_tsvector('english', coalesce("nodes"."name"::text, '')) || to_tsvector('english', coalesce("nodes"."user_id"::text, '')) || to_tsvector('english', coalesce("nodes"."circa"::text, '')) || to_tsvector('english', coalesce("nodes"."cached_tagged_user_names"::text, '')) || to_tsvector('english', coalesce("nodes"."cached_user_tag_list"::text, '')) || to_tsvector('english', coalesce(pg_search_121ea89914a721445aee70.pg_search_344e3c62d13849726da22e::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_acfcdbc0b3d3a65f40eab7::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_eeb842708b24d4f7fcf549::text, '')) || to_tsvector('english', coalesce(pg_search_4aab10cdca183dac25f479.pg_search_cb8eb1b84bf18ee1412ffd::text, '')) || to_tsvector('english', coalesce(pg_search_b1bc4c0f44e7f4799d8caf.pg_search_9474fb5b090e75ade5136f::text, '')) || to_tsvector('english', coalesce(pg_search_93dd21271636bd02b16bc3.pg_search_484f77386a5aecc6e01094::text, ''))) @@ (to_tsquery('english', ''' ' || 'abc' || ' ''' || ':*')))) ORDER BY pg_search_rank DESC, "nodes"."id" ASC
=> [#<Node id: 85, name: "House Fire 2", family_tree_id: 57, user_id: 57, media_id: 228, media_type: "Video", created_at: "2015-05-15 00:20:26", updated_at: "2015-06-08 22:25:49", circa: nil, is_comment: nil, cached_votes_total: 0, cached_votes_score: 0, cached_votes_up: 0, cached_votes_down: 0, cached_weighted_score: 0, cached_weighted_total: 0, cached_weighted_average: 0.0, cached_user_tag_list: "danny@test.com, abc@test.com", cached_num_user_tags: 2, cached_tagged_user_names: ["Daniel Marty", "Marcamus Prime"]>,
#<Node id: 86, name: "10PP Form Video", family_tree_id: 57, user_id: 57, media_id: 229, media_type: "Video", created_at: "2015-05-15 01:26:28", updated_at: "2015-06-05 21:10:09", circa: nil, is_comment: nil, cached_votes_total: 1, cached_votes_score: 1, cached_votes_up: 1, cached_votes_down: 0, cached_weighted_score: 1, cached_weighted_total: 1, cached_weighted_average: 0.0, cached_user_tag_list: "gerry@test.com", cached_num_user_tags: 1, cached_tagged_user_names: ["Gerry Atrick"]>]
可能会抛弃前缀搜索,因此我只需将@
的属性从cached_user_tag_list
更改为danny@test.com
即可对其进行测试dannyk@test.com
。然后我搜索了dannyk
并返回了正确的结果,但是当我搜索danny
时,它仍会返回[]
。我不明白为什么。
答案 0 :(得分:4)
您可以启用:trigram search
并指定阈值。它匹配出现在可搜索文本中任意位置的任意子字符串。
你的最终代码看起来像这样。
pg_search_scope :node_search, against: [:name, :user_id, :circa, :cached_tagged_user_names, :cached_user_tag_list],
using: { tsearch: { any_word: true, dictionary: :english, prefix: true}, :trigram => { :threshold => 0.1 } },
:associated_against => {
comments: [:message],
user: [:first_name, :last_name, :email],
memberships: [:relation],
user_tags: [:name]
}