ruby - 如何使Searchkick/Elasticsearch where子句不区分大小写?

标签 ruby elasticsearch searchkick

我在Ruby(ElasticSearch)中使用Searchkick。目前,默认情况下,where过滤器区分大小写。

我在自己的EquityContract模型中使用ElasticSearch,因此一旦搜索Food,我将得到不同的结果来搜索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"=>[]}}>

当我对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",

我该如何更改它以使其对大小写不敏感,以便为两者提供相同的结果?

最佳答案

我看到Searchkiq生成词条查询,但是您需要的是全文查询。我对Searchkiq不熟悉,所以无法告诉您如何操作。

根据the documentation on ElasticSearch official website

字词查询

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.



全文查询

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.

关于ruby - 如何使Searchkick/Elasticsearch where子句不区分大小写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36414571/

相关文章:

Ruby - 使用扫描拆分多个字符串

ruby-on-rails - 未定义的局部变量或方法 `searchkick'

ruby - 如何在 erb 中为 2 个布局创建 1 个 View ?

ruby - Rspec 相等方法

ruby-on-rails - Rails 服务器不工作?

elasticsearch - 如何在嵌套查询中为每个查询获取n个文档?

c# - Elasticsearch NEST 搜索

javascript - 使用来自Elasticsearch的通配符搜索全部和部分文本值

ruby-on-rails - 如何使用searchkick设置word_middle区分大小写的搜索?

elasticsearch - 脚本化聚合计算,结果出乎意料