简体   繁体   English

如何停止pg_search返回Rails中的所有列

[英]How do I stop pg_search from returning all columns in Rails

I have a pg_search_scope in a class called Name as follows: 我在名为Name的类中有一个pg_search_scope,如下所示:

pg_search_scope :fuzzy_search, 
                :against => [:surname, :forename, :other_names],
                :using => {
                   dmetaphone: {
                      tsvector_column: 'dmetaphone_search_vector'
                   },
                tsearch: {
                   dictionary: 'english',
                   tsvector_column: 'dictionary_search_vector'
                   }
                },
                :ranked_by => ":trigram"

I need to run a SELECT against the scope and just get back selective columns. 我需要针对范围运行SELECT,然后只返回选择列。 I excepted the following todo that: 我除了以下操作之外:

Name.select(:surname).fuzzy_search('smith')

but that generates: 但这会产生:

SELECT surname, \"names\".*, ((similarity((coalesce(\"names\".\"surname\"::text, '') || ' ' || coalesce(\"names\".\"forename\"::text, '') || ' ' || coalesce(\"names\".\"other_names\"::text, '')), 'smith'))) AS pg_search_rank FROM \"names\"  WHERE (((\"names\".\"dmetaphone_search_vector\") @@ (to_tsquery('simple', ''' ' || pg_search_dmetaphone('smith') || ' '''))) OR ((\"names\".\"dictionary_search_vector\") @@ (to_tsquery('english', ''' ' || 'smith' || ' ''')))) ORDER BY pg_search_rank DESC, \"names\".\"id\" ASC

So as we can see that leaves \\"names\\".* in the query, which ofcourse returns all the columns. 因此,我们可以看到在查询中留下\\"names\\".* ,这当然会返回所有列。

Anyone else come across this? 还有其他人遇到吗? Any suggestions? 有什么建议么?

:) :)

Can the select statement come after the fuzzy_search call: select语句可以在fuzzy_search调用之后fuzzy_search

Name.fuzzy_search('smith').select(:surname)

If that doesn't work, try pluck : 如果还是不行,请尝试pluck

Name.fuzzy_search('smith').pluck(:surname)

I was just running into this same issue over and over before finally figuring out a fix: 我只是一遍又一遍地遇到这个问题,最后才找出解决办法:

Name.fuzzy_search('smith').map(&:surname)

It's not ideal, because (as I understand it) it's technically loading all the fields before stripping away everything except :surname . 这不是理想的,因为(据我所知)从技术上来说,它是在剥离所有字段之前除去:surname之外的所有内容。 But better than nothing, I suppose. 但我想总比没有好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM