From 3eebcccbf286f3a682865e35dff23fa800a6029a Mon Sep 17 00:00:00 2001 From: Jakub Macina Date: Wed, 31 May 2017 16:36:15 +0200 Subject: [PATCH] Add searching by all tags using postgres full-text search. --- lib/search.rb | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/search.rb b/lib/search.rb index 323b0942dee..e9c0984f726 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -445,15 +445,28 @@ class Search end end - advanced_filter(/tags?:([a-zA-Z0-9,\-_]+)/) do |posts, match| - tags = match.split(",") + advanced_filter(/tags?:([a-zA-Z0-9,\-_|]+)/) do |posts, match| + if match.include?(',') + tags = match.split(",") - posts.where("topics.id IN ( + # TODO use ts_query function + posts.where("topics.id IN ( + SELECT DISTINCT(tt.topic_id) + FROM topic_tags tt, tags + WHERE tt.tag_id = tags.id + GROUP BY tt.topic_id + HAVING to_tsvector('simple',array_to_string(array_agg(tags.name), ' ')) @@ to_tsquery('simple', ?) + )", tags.join('&')) + else + tags = match.split("|") + + posts.where("topics.id IN ( SELECT DISTINCT(tt.topic_id) FROM topic_tags tt, tags WHERE tt.tag_id = tags.id AND tags.name in (?) )", tags) + end end private