mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 02:40:53 -06:00
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:
parent
840ac6bd0a
commit
c9a46cfdda
@ -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 =
|
||||
(
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user