thinking sphinx ordering by mixing capitals and lo

2019-06-03 16:14发布

I have a rails application that is using thinking_sphinx for the searching. My problem is that the result returned is sorted with capitals first and lower case after it at the bottom. I'd like to mix them so that both 'A' and 'a' comes before 'B'. This is the method I'm using:

Company.search(query, :star => true, :page => params[:page], :per_page => 20, :order => :name, :sort_mode => :asc)

3条回答
孤傲高冷的网名
2楼-- · 2019-06-03 16:26

You have to configure sphinx to transform the input, making uppercase letters become lowercase letters. You can do this with the "charset_table" config.

And here's a complementary tutorial on how to configure it with thinking_sphinx.

查看更多
淡お忘
3楼-- · 2019-06-03 16:37

Maurício's answer is almost on the right track - but that's for transforming the text that Sphinx indexes for fields. When you're sorting, you're doing that by attributes (which don't use the charset table transformations).

What you'll need to do is separate the sorting attribute of name out from the field, and force the attribute version to use lowercase:

indexes :name
has "LOWER(companies.name)", :as => :name_sort

And then searching becomes:

Company.search query,
  :star      => true,
  :page      => params[:page],
  :per_page  => 20,
  :order     => :name_sort,
  :sort_mode => :asc
查看更多
迷人小祖宗
4楼-- · 2019-06-03 16:40

After researching many hours I got simple and elegant solution for case insensitive search. Just define your index in following way.

indexes name, as: :name_sort, sortable: :insensitive
查看更多
登录 后发表回答