简体   繁体   中英

How setup word_middle case-sensitive search with searchkick?

I am trying to setup Searchkick in my Rails application.

I have a model with fields:

  • number:string eg NK4321nn1234

What I need:

  • searching for 1nn1 should find NK4321nn1234
  • searching for 1NN1 should NOT find NK4321nn1234

Here is my model:

class Product < ApplicationRecord
    searchkick word_middle: [:number]

  def search_data
    {
      number: number
    }
  end

  def self.sensitive_search query, page
    search( query,
      fields: [{number: :exact}],
      match: :word_middle,
      misspellings: false,
      page: 1,
      per_page: 15
    )
  end

  def self.insensitive_search query, page
    search( query.downcase,
      fields: [:number],
      match: :word_middle,
      misspellings: false,
      page: 1,
      per_page: 15
    )
  end
end

Case-sensitive search does not work. As far as I understand, searchkick downcase all nGrams during indexing... But why?..

Questions:

  • did I write search methods correct?
  • why sensitive search not working?

My environment: rails 5.2.0, searchkick 3.1.0

As of this commit , you can do:

class Product < ApplicationRecord
  searchkick word_middle: [:number], case_sensitive: true
end

Be sure to reindex after updating Searchkick options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM