【问题标题】:How do I set the default analyzer for elastic search with tire?如何为使用轮胎的弹性搜索设置默认分析器?
【发布时间】:2026-01-18 20:50:02
【问题描述】:

我最近一直在用 ruby​​ on rails 试验 elasticsearch。我无法将我的数据编入索引,因此我可以搜索包含复数和非复数关键字的项目。

轮胎允许我为每个映射属性分配一个分析器:

mapping do
  indexes title, analyzer: 'snowball'
  indexes body, analyzer: 'snowball'
end

现在,假设我在“测试”的标题中有一个关键字

如果我使用查询中的属性进行搜索: http://localhost:9200/myindex/mymapping/_search?q=title:test 它会起作用的。

但是,如果我在不指定属性的情况下进行一般搜索:
http://localhost:9200/myindex/mymapping/_search?q=test

它不会找到该文档。

如何指定我希望默认分析器为“雪球”,这样我就不必指定要搜索的属性?

附言我正在使用轮胎宝石。因此,请考虑到这一点,尽可能地回答。

【问题讨论】:

    标签: ruby-on-rails elasticsearch tire


    【解决方案1】:

    可以使用索引设置更改默认分析器:

    require 'rubygems'
    require 'tire'
    
    Tire.index 'articles' do
      delete
      create :settings => {
          :index => {
            :analysis => {
              :analyzer => {
                :default => {
                  :type => 'snowball'
                }
              }
            }
          }
        },
        :mappings => {
          :article => {
            :properties => {
              :title    => { :type => 'string', :analyzer => 'snowball'},
              :body     => { :type => 'string', :analyzer => 'snowball'}
            }
          }
        }
    
      store :title => 'Tests', :body => "Plural"
      store :title => 'Test', :body => "Singular"
    
      refresh
    end
    

    【讨论】:

      【解决方案2】:

      在 elasticsearch.yml 设置文件中进行设置是最简单的:

      index.number_of_shards: 2
      index.number_of_replicas: 0
      index.analysis.analyzer.default.type: snowball
      

      【讨论】: