From c9a46cfdda21624c1262813d14fc869a11cd44d1 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Wed, 17 Apr 2024 17:20:25 +0300 Subject: [PATCH] FIX: Use ILIKE for searching categories (#26619) Full text search does not return ideal results for category dropdown. Usually, in category dropdowns we want to search for categories as we type. For example, while typing "theme", the dropdown should show intermediary results for "t", "th", "the", "them" and finally "theme". For some of these substrings (like "the"), full text search does not return any results, which leads to an unpleasant user experience. --- app/controllers/categories_controller.rb | 10 +++------- spec/requests/categories_controller_spec.rb | 5 +++-- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 8cb422cabde..e8c7098099c 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -366,13 +366,9 @@ class CategoriesController < ApplicationController categories = Category.secured(guardian) - categories = - categories - .includes(:category_search_data) - .references(:category_search_data) - .where( - "category_search_data.search_data @@ #{Search.ts_query(term: term)}", - ) if term.present? + if term.present? && words = term.split + words.each { |word| categories = categories.where("name ILIKE ?", "%#{word}%") } + end categories = ( diff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rb index d1b2b815e52..3ec01bf0381 100644 --- a/spec/requests/categories_controller_spec.rb +++ b/spec/requests/categories_controller_spec.rb @@ -1211,7 +1211,7 @@ RSpec.describe CategoriesController do queries = track_sql_queries { get "/categories/search.json", params: { term: "Notfoo" } } - expect(queries.length).to eq(5) + expect(queries.length).to eq(8) expect(response.parsed_body["categories"].length).to eq(1) expect(response.parsed_body["categories"][0]["custom_fields"]).to eq("bob" => "marley") @@ -1247,10 +1247,11 @@ RSpec.describe CategoriesController do it "returns categories" do get "/categories/search.json", params: { term: "Foo" } - expect(response.parsed_body["categories"].size).to eq(2) + expect(response.parsed_body["categories"].size).to eq(3) expect(response.parsed_body["categories"].map { |c| c["name"] }).to contain_exactly( "Foo", "Foobar", + "Notfoo", ) end end