Search code examples
rubyelasticsearchsearchkick

How to make Searchkick/Elasticsearch where clause case insensitive?


I'm using ElasticSearch with Ruby (Searchkick). At the moment, by default the where filter is a case sensitive.

I'm using ElasticSearch with my EquityContract model, so once I search for Food I get different results to search for food.

    [21] pry(main)> EquityContract.search('*', {:load=>false, :where=>{:industry=>"FOOD"}, limit:1})
effective_url=http://127.0.0.1:9200/equity_contracts_development/_search response_code=200 return_code=ok total_time=0.002279
      EquityContract Search (5.9ms)  curl http://127.0.0.1:9200/equity_contracts_development/_search?pretty -d '{"query":{"filtered":{"query":{"match_all":{}},"filter":{"and":[{"term":{"industry":"FOOD"}}]}}},"size":1,"from":0}'
    => #<Searchkick::Results:0x0000010b65c5c8
     @klass=
      EquityContract(id: integer, ticker: text, name: string, country: string, currency: string, instrument: string),
     @options=
      {:page=>1,
       :per_page=>1,
       :padding=>0,
       :load=>false,
       :includes=>nil,
       :json=>false,
       :match_suffix=>"analyzed",
       :highlighted_fields=>[]},
     @response=
      {"took"=>1,
       "timed_out"=>false,
       "_shards"=>{"total"=>5, "successful"=>5, "failed"=>0},
       "hits"=>{"total"=>0, "max_score"=>nil, "hits"=>[]}}>

While I get some results when I do the same with Food:

[23] pry(main)> EquityContract.search('*', {:load=>false, :where=>{:industry=>"Food"}, limit:1})
ETHON: performed EASY effective_url=http://127.0.0.1:9200/equity_contracts_development/_search response_code=200 return_code=ok total_time=0.002795
  EquityContract Search (7.5ms)  curl http://127.0.0.1:9200/equity_contracts_development/_search?pretty -d '{"query":{"filtered":{"query":{"match_all":{}},"filter":{"and":[{"term":{"industry":"Food"}}]}}},"size":1,"from":0}'
=> #<Searchkick::Results:0x000001112d1880
 @klass=
  EquityContract(id: integer, ticker: text, name: string, country: string, currency: string, instrument: string),
 @options=
  {:page=>1,
   :per_page=>1,
   :padding=>0,
   :load=>false,
   :includes=>nil,
   :json=>false,
   :match_suffix=>"analyzed",
   :highlighted_fields=>[]},
 @response=
  {"took"=>1,
   "timed_out"=>false,
   "_shards"=>{"total"=>5, "successful"=>5, "failed"=>0},
   "hits"=>
    {"total"=>73,
     "max_score"=>1.0,
     "hits"=>
      [{"_index"=>"equity_contracts_development_20160320195353552",
        "_type"=>"equity_contract",
        "_id"=>"1181",
        "_score"=>1.0,
        "_source"=>
         {"name"=>"Some name",
          "ticker"=>"some ticker",
          "country"=>"SA",

How can I change this to make it more case-insensitive so I'll het the same results for both?


Solution

  • I saw Searchkiq generates term query, but what you need is a full-text query. I'm not familiar with Searchkiq so I can't tell you how.

    According to the documentation on ElasticSearch official website,

    Term query

    Queries like the term or fuzz queries are low-level queries that have no analysis phase. They operate on a single term. A term query for the term Foo looks for that exact term in the inverted index and calculates the TF/IDF relevance _score for each document that contains the term.

    Full-text query

    Queries like the match or query_string queries are high-level queries that understand the mapping of a field ... If you query a full-text (analyzed) field, they will first pass the query string through the appropriate analyzer to produce the list of terms to be queried.