pg-search

pg_search_scope: chaining scopes seems impossible


I have a search form for searching "documents", that have a small dozen of search criterions, including "entire_text", "keywords" and "description".

I'm using pg_search_scope, but I have 2 different scopes.

This is in my document.rb:

pg_search_scope :search_entire_text, 
                :against => :entire_text, 
                :using => {
                    :tsearch => {
                        :prefix => true,
                        :dictionary => "french"
                        }
                    }

pg_search_scope :search_keywords,
                :associated_against => {
                    :keywords => [:keyword]
                },
                :using => {
                    :tsearch => {
                        :any_word => true
                    }
                }

Each separately works fine. But I can't do this:

@resultats = Document.search_keywords(params[:ch_document][:keywords]).search_entire_text(params[:ch_document][:entire_text])

Is there any way to work around this?

Thanks


Solution

  • I've never used pg_search_scope but it looks like you indeed can't combine two pg_search_scope's.

    What you could do is use :search_entire_text with a pg_search_scope and use the resulting id's in a Document.where([1,2,3]) that way you can use standard rails scope's for the remaining keyword searches.

    Example:

    # If pluck doesn't work you can also use map(&:id)
    txt_res_ids = Document.search_entire_text(params[:ch_document][:entire_text]).pluck(:id)
    final_results = Document.where(id: txt_res_ids).some_keyword_scope.all