Skip to content

Instantly share code, notes, and snippets.

@frankie-loves-jesus
Last active August 29, 2015 13:58
Show Gist options
  • Save frankie-loves-jesus/9990180 to your computer and use it in GitHub Desktop.
Save frankie-loves-jesus/9990180 to your computer and use it in GitHub Desktop.

Basic search in Forem with forem-postgres-search

See https://github.com/frankie-loves-jesus/demo-search if you want to try it out yourself.

Status:

NOT WORKING

When going to http://localhost:3000/forums/default/search:

ActiveRecord::StatementInvalid in Forem::Forums#search
Showing /home/dev/.gem/ruby/2.1.1/bundler/gems/forem-postgres-search-5c1fed5ad6dd/app/views/forem/forums/search.html.erb where line #24 raised:

PG::SyntaxError: ERROR:  syntax error at or near "AS"
LINE 1: ...a741b6::text, ''))), (''), 0))) AS pg_search_rank AS count_c...
                                                             ^
: SELECT COUNT(count_column) FROM (SELECT  "forem_topics".*, ((ts_rank((to_tsvector('english', coalesce("forem_topics"."subject"::text, '')) || to_tsvector('english', coalesce(pg_search_f657216e8d26f11e18f48f.pg_search_3f4f20b49c337c6ba741b6::text, ''))), (''), 0))) AS pg_search_rank, "forem_topics".*, ((ts_rank((to_tsvector('english', coalesce("forem_topics"."subject"::text, '')) || to_tsvector('english', coalesce(pg_search_f657216e8d26f11e18f48f.pg_search_3f4f20b49c337c6ba741b6::text, ''))), (''), 0))) AS pg_search_rank AS count_column FROM "forem_topics" LEFT OUTER JOIN (SELECT "forem_topics"."id" AS id, string_agg("forem_posts"."text"::text, ' ') AS pg_search_3f4f20b49c337c6ba741b6 FROM "forem_topics" INNER JOIN "forem_posts" ON "forem_posts"."topic_id" = "forem_topics"."id" GROUP BY "forem_topics"."id") pg_search_f657216e8d26f11e18f48f ON pg_search_f657216e8d26f11e18f48f.id = "forem_topics"."id" WHERE (((to_tsvector('english', coalesce("forem_topics"."subject"::text, '')) || to_tsvector('english', coalesce(pg_search_f657216e8d26f11e18f48f.pg_search_3f4f20b49c337c6ba741b6::text, ''))) @@ (''))) AND "forem_topics"."forum_id" = 1 AND "forem_topics"."hidden" = 'f' AND "forem_topics"."state" = 'approved' LIMIT 20 OFFSET 0) subquery_for_count
Extracted source (around line #24):

       <% if @topics.empty? %>
         <tr><td colspan='5' align='center'>No Results Found</td></tr>
       <% end %>
       <%= render @topics %>

<!-- Doesn't work -->
<%# form_tag search_forum_path(@forum), :method=> :get do %>
<%#= text_field_tag :q, @search %>
<%#= submit_tag "Search" %>
<%# end %>
gem 'pg'
# gem 'pg_search'
gem 'forem-postgres-search', :github => 'jgadbois/forem-postgres-search'
Forem::Category.create!(:name => 'General')
user = User.create(
:email => "[email protected]",
:password => "admin1234"
)
user.forem_admin = true
user.save!
user.update_attribute(:forem_state, 'approved')
unless user.nil?
forum = Forem::Forum.create(:category_id => Forem::Category.first.id, :name => "Default", :description => "Default forem created by install")
topic1 = forum.topics.build({ :subject => "Apples", :posts_attributes => [:text => "Eat apples"] })
topic1.user = user
topic1.save!
topic2 = forum.topics.build({ :subject => "Bananas", :posts_attributes => [:text => "Eat bananas"] })
topic2.user = user
topic2.save!
topic3 = forum.topics.build({ :subject => "Oranges", :posts_attributes => [:text => "Eat oranges"] })
topic3.user = user
topic3.save!
topic4 = forum.topics.build({ :subject => "Mangos", :posts_attributes => [:text => "Eat mangos"] })
topic4.user = user
topic4.save!
end
# Create the database:
#
# sudo su _postgresql
# psql template1
#
# CREATE ROLE demo SUPERUSER LOGIN PASSWORD 'demo1234';
# \q
development:
adapter: postgresql
user: demo
password: demo1234
database: demo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment