mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 08:57:10 -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 = Category.secured(guardian)
|
||||||
|
|
||||||
categories =
|
if term.present? && words = term.split
|
||||||
categories
|
words.each { |word| categories = categories.where("name ILIKE ?", "%#{word}%") }
|
||||||
.includes(:category_search_data)
|
end
|
||||||
.references(:category_search_data)
|
|
||||||
.where(
|
|
||||||
"category_search_data.search_data @@ #{Search.ts_query(term: term)}",
|
|
||||||
) if term.present?
|
|
||||||
|
|
||||||
categories =
|
categories =
|
||||||
(
|
(
|
||||||
|
@ -1211,7 +1211,7 @@ RSpec.describe CategoriesController do
|
|||||||
|
|
||||||
queries = track_sql_queries { get "/categories/search.json", params: { term: "Notfoo" } }
|
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"].length).to eq(1)
|
||||||
expect(response.parsed_body["categories"][0]["custom_fields"]).to eq("bob" => "marley")
|
expect(response.parsed_body["categories"][0]["custom_fields"]).to eq("bob" => "marley")
|
||||||
@ -1247,10 +1247,11 @@ RSpec.describe CategoriesController do
|
|||||||
it "returns categories" do
|
it "returns categories" do
|
||||||
get "/categories/search.json", params: { term: "Foo" }
|
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(
|
expect(response.parsed_body["categories"].map { |c| c["name"] }).to contain_exactly(
|
||||||
"Foo",
|
"Foo",
|
||||||
"Foobar",
|
"Foobar",
|
||||||
|
"Notfoo",
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user