How to get results from a pg_search multisearch

Viewed 1667

I'm trying to use the pg_search gem in my rails application to search through numerous models from one search bar.

I have added the search bar:

   <div id="search-bar">
        <%= form_tag searches_path, method: :get do %>
          <span><%= text_field_tag :query, params[:query] %><%= submit_tag "Search", name: nil %></span>
        <% end %>
      </div>

The route:

    resources :searches 

The searches controller index method:

    def index
    @results = PgSearch.multisearch(:query)
end

And the search index.html.erb:

      <ul>
        <% @results.each do |result| %>
          <li><%= link_to result.searchable.title, result.searchable %></li>
        <% end %>
      </ul>

Each model that I want to be searchable has something like this in it:

    include PgSearch
multisearchable :against => [:content, :author]

And I have an initializer like this:

PgSearch.multisearch_options = {
  :using => {
    :tsearch => {
      :dictionary => "english"
    }
  }
}

I created a new item to make sure that it was added to the pg_search_documents table and checked the terminal to see that it was.

Yet when I enter a search query, nothing is coming up in the results table?

Here is the terminal output from the search:

Started GET "/searches?utf8=%E2%9C%93&query=Test" for 127.0.0.1 at 2019-01-16 13:16:48 +0000
Processing by SearchesController#index as HTML
  Parameters: {"utf8"=>"✓", "query"=>"Test"}
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/---/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
   Group Exists (0.2ms)  SELECT  1 AS one FROM "groups" LIMIT $1  [["LIMIT", 1]]
  ↳ app/controllers/application_controller.rb:34
  Category Exists (0.2ms)  SELECT  1 AS one FROM "categories" LIMIT $1  [["LIMIT", 1]]
  ↳ app/controllers/application_controller.rb:37
  Rendering searches/index.html.erb within layouts/application
  ggPgSearch::Document Load (20.6ms)  SELECT "pg_search_documents".* FROM "pg_search_documents" INNER JOIN (SELECT "pg_search_documents"."id" AS pg_search_id, (ts_rank((to_tsvector('english', coalesce("pg_search_documents"."content"::text, ''))), (to_tsquery('english', ''' ' || 'query' || ' ''')), 0)) AS rank FROM "pg_search_documents" WHERE (((to_tsvector('english', coalesce("pg_search_documents"."content"::text, ''))) @@ (to_tsquery('english', ''' ' || 'query' || ' '''))))) AS pg_search_ce9b9dd18c5c0023f2116f ON "pg_search_documents"."id" = pg_search_ce9b9dd18c5c0023f2116f.pg_search_id ORDER BY pg_search_ce9b9dd18c5c0023f2116f.rank DESC, "pg_search_documents"."id" ASC
  ↳ app/views/searches/index.html.erb:7
  Rendered searches/index.html.erb within layouts/application (24.2ms)
  Group Load (0.3ms)  SELECT "groups".* FROM "groups"
  ↳ app/views/layouts/application.html.erb:59
  Category Load (0.2ms)  SELECT "categories".* FROM "categories"
  ↳ app/views/layouts/application.html.erb:72
Completed 200 OK in 114ms (Views: 88.4ms | ActiveRecord: 21.9ms)

The ul tags are present on the page, but no li items, and I am sure that the term I am putting in was within the item I created.

How come it isn't picking up any results? Many thanks

1 Answers

Did you do the following?

$ rails g pg_search:migration:multisearch

$ rake db:migrate

It's required for multisearching. Also existing items may not be added to the document as you already know. You would need to "rebuild" as stated in the docs. When you made a new item for testing, did you save it properly?

Here are the docs I found for the pg gem: https://github.com/Casecommons/pg_search

Related