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.
This commit is contained in:
Bianca Nenciu 2024-04-17 17:20:25 +03:00 committed by GitHub
parent 840ac6bd0a
commit c9a46cfdda
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 9 deletions

View File

@ -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 =
(

View File

@ -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