【问题标题】:How to make Searchkick/Elasticsearch where clause case insensitive?如何使 Searchkick/Elasticsearch where 子句不区分大小写?
【发布时间】:2016-04-09 16:06:16
【问题描述】:

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

我将 ElasticSearch 与我的 EquityContract 模型一起使用,所以一旦我搜索 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",

如何更改它以使其更不区分大小写,以便两者的结果相同?

【问题讨论】:

  • 这取决于您对该字段industry 的映射。 term完全匹配 ES 索引中的一个术语,因此索引的内容和方式很重要。

标签: ruby elasticsearch searchkick


【解决方案1】:

我看到 Searchkiq 生成词条查询,但您需要的是全文查询。我不熟悉 Searchkiq,所以我不能告诉你怎么做。

根据the documentation on ElasticSearch official website

词条查询

termfuzz 等查询是没有分析阶段的低级查询。他们在一个任期内运作。术语 Foo 的术语查询在倒排索引中查找该确切术语,并计算包含该术语的每个文档的 TF/IDF 相关性_score

全文查询

matchquery_string 这样的查询是了解字段映射的高级查询...如果您查询全文(已分析)字段,它们将首先将查询字符串通过适当的分析器来生成要查询的术语列表。

【讨论】:

  • 感谢您愿意帮助@Aetherus。有什么方法可以过滤结果来处理术语查询?
  • 我认为您可以在将数据存储到 ES 之前将其小写,并在搜索之前将查询小写。
  • 我认为这可能是一些很好的解决方法。让我试试看
  • 您能否在答案中添加您建议的解决方法,以便我接受。
  • @EkiEqbal 你在哪里可以使它工作?我面临同样的问题。我尝试使用 where: {name: /.*partial_name.*/i} where /i is to make regex case insensitive 但这在我的情况下不起作用
猜你喜欢
  • 2016-09-03
  • 2016-11-26
  • 2021-02-27
  • 2020-12-06
  • 2017-01-23
  • 2011-10-16
  • 2021-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多