You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search.rb 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. require 'sequel'
  2. def search(params)
  3. query = params[:q].strip
  4. offset = (params[:page] - 1) * 10
  5. username = params[:username].strip
  6. case params[:type]
  7. when 'threads'
  8. sort =
  9. case params[:sort]
  10. when 'thread'
  11. 'created_at DESC'
  12. when 'post'
  13. 'last_post_created_at DESC'
  14. else
  15. 'created_at DESC'
  16. end
  17. search_threads(query, username, sort, offset)
  18. when 'posts'
  19. search_posts(query, username, offset)
  20. else
  21. Array.new
  22. end
  23. end
  24. def search_threads(query, username, sort, offset)
  25. DB[<<-SQL, query, username, username, offset]
  26. SELECT
  27. threads.*
  28. FROM threads
  29. WHERE
  30. to_tsvector(title) @@ plainto_tsquery(?)
  31. AND (LOWER(threads.creator) = LOWER(?) OR ? = '')
  32. ORDER BY #{sort}
  33. LIMIT 50
  34. OFFSET ?;
  35. SQL
  36. end
  37. def search_posts(query, username, offset)
  38. DB[<<-SQL, query, username, username, offset]
  39. SELECT
  40. posts.*,
  41. threads.title as thread_title,
  42. threads.remote_id as remote_thread_id
  43. FROM posts
  44. INNER JOIN threads on posts.thread_id = threads.id
  45. WHERE
  46. tsv @@ plainto_tsquery(?)
  47. AND (
  48. (LOWER(posts.creator) = LOWER(?)) OR (? = '')
  49. )
  50. ORDER BY created_at DESC
  51. LIMIT 50
  52. OFFSET ?;
  53. SQL
  54. end