Skip to content

A platform for community discussion. Free, open, simple.

FIX: Advanced search category term should be case insensitive.

Guo Xiang Tanda941840d47d
diff --git a/lib/search.rb b/lib/search.rb
index 3ffbda872fb..07ddba622ec 100644
--- a/lib/search.rb
+++ b/lib/search.rb
@@ -399,8 +399,17 @@ class Search
if slug[1]
# sub category
- parent_category_id = Category.where(slug: slug[0].downcase, parent_category_id: nil).pluck(:id).first
- category_id = Category.where(slug: slug[1].downcase, parent_category_id: parent_category_id).pluck(:id).first
+ parent_category_id = Category
+ .where("lower(slug) = ? AND parent_category_id IS NULL", slug[0].downcase)
+ .pluck(:id)
+ .first
+
+ category_id = Category
+ .where("lower(slug) = ? AND parent_category_id = ?",
+ slug[1].downcase, parent_category_id
+ )
+ .pluck(:id)
+ .first
else
# main category
if slug[0][0] == "="
@@ -409,7 +418,7 @@ class Search
exact = false
end
- category_id = Category.where(slug: slug[0].downcase)
+ category_id = Category.where("lower(slug) = ?", slug[0].downcase)
.order('case when parent_category_id is null then 0 else 1 end')
.pluck(:id)
.first
diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb
index 993a2597196..287fa8e7f65 100644
--- a/spec/components/search_spec.rb
+++ b/spec/components/search_spec.rb
@@ -873,25 +873,28 @@ describe Search do
it 'supports category slug and tags' do
# main category
- category = Fabricate(:category, name: 'category 24', slug: 'category-24')
+ category = Fabricate(:category, name: 'category 24', slug: 'cateGory-24')
topic = Fabricate(:topic, created_at: 3.months.ago, category: category)
post = Fabricate(:post, raw: 'Sams first post', topic: topic)
- expect(Search.execute('sams post #category-24').posts.length).to eq(1)
+ expect(Search.execute('sams post #categoRy-24').posts.length).to eq(1)
expect(Search.execute("sams post category:#{category.id}").posts.length).to eq(1)
- expect(Search.execute('sams post #category-25').posts.length).to eq(0)
+ expect(Search.execute('sams post #categoRy-25').posts.length).to eq(0)
sub_category = Fabricate(:category, name: 'sub category', slug: 'sub-category', parent_category_id: category.id)
second_topic = Fabricate(:topic, created_at: 3.months.ago, category: sub_category)
Fabricate(:post, raw: 'sams second post', topic: second_topic)
- expect(Search.execute("sams post category:category-24").posts.length).to eq(2)
- expect(Search.execute("sams post category:=category-24").posts.length).to eq(1)
+ expect(Search.execute("sams post category:categoRY-24").posts.length).to eq(2)
+ expect(Search.execute("sams post category:=cAtegory-24").posts.length).to eq(1)
expect(Search.execute("sams post #category-24").posts.length).to eq(2)
expect(Search.execute("sams post #=category-24").posts.length).to eq(1)
expect(Search.execute("sams post #sub-category").posts.length).to eq(1)
+ expect(Search.execute("sams post #categoRY-24:SUB-category").posts.length)
+ .to eq(1)
+
# tags
topic.tags = [Fabricate(:tag, name: 'alpha'), Fabricate(:tag, name: 'привет'), Fabricate(:tag, name: 'HeLlO')]
expect(Search.execute('this is a test #alpha').posts.map(&:id)).to eq([post.id])