简体   繁体   English

自定义pg_search以仅显示当前用户的内容

[英]Customize pg_search to only show current user's content

I have a multiuser site, and I want users to be able to full-text multi-model search only their own content. 我有一个多用户站点,我希望用户能够仅对自己的内容进行全文本多模式搜索。

My Work model belongs_to: :users. 我的工作模型属于::用户。 Here's a snippet of work.rb : 这是work.rb的片段:

class Work < ApplicationRecord
  include PgSearch
  belongs_to :user
  …

The ideal scenario would be something like this in my controller: 理想的情况是在我的控制器中是这样的:

@results = PgSearch.multisearch('spring').where(:_user_id => current_user.id)

I was able to easily do this using the Searchkick gem with Elasticsearch by using this line of code: 通过使用以下代码行,我可以轻松地将Searchkick gem和Elasticsearch一起使用:

@results = Searchkick.search query, where: {user_id: current_user.id}

I can't use Elasticsearch anymore unfortunately. 不幸的是,我不能再使用Elasticsearch。

How can I implement similar functionality in pg_search? 如何在pg_search中实现类似的功能?

Thanks a lot 非常感谢

You can use pg_search_scope as described in pg_search_scope 您可以按照pg_search_scope中所述使用pg_search_scope

in your case is pg_search_scope multi-search 您的情况是pg_search_scope多次搜索

I am just starting to work with PgSearch, but based on what I see, you can only apply filters on what data gets sent into their database pg_search_documents. 我刚刚开始使用PgSearch,但是根据我所看到的,您只能对将哪些数据发送到其数据库pg_search_documents中应用过滤器。 So you cannot isolate on a particular user for the search that you do later. 因此,您无法隔离特定用户以进行稍后的搜索。 They simply concat the various fields into their content field and reference the model and record they came from. 他们只是将各个字段合并到其内容字段中,并引用模型并记录它们来自何处。 This allows easy searches to find the records which match the criteria, but then you have to post process the data. 这样可以轻松搜索以找到符合条件的记录,但是您必须对数据进行后处理。

To do what you're suggesting would require creating a search table for each user. 要执行您建议的操作,需要为每个用户创建一个搜索表。

if you include the user_id in your against criteria (should be in the content), then you can post process the data quickly since the ID will be in the content. 如果您在违反条件中将user_id包含在内容中(应该包含在内容中),则由于ID将包含在内容中,因此可以快速对数据进行后期处理。 If it is at the end of the :against list, it will be at the end of the content, so you can strip it off quickly and process against it 如果它在:against列表的末尾,它将在内容的末尾,因此您可以快速剥离它并对其进行处理

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

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