How do I set the default analyzer for elastic search with tire?

这一生的挚爱 提交于 2019-12-19 03:39:14

问题


I have been experimenting with elasticsearch lately with ruby on rails. I am having trouble getting my data indexed so I can search for items with both plural, and non-plural keywords.

Tire will allow me to assign an analyzer per mapping attribute:

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

Now, say I have a keyword in the title of 'tests'

if I do a search with the attribute in the query: http://localhost:9200/myindex/mymapping/_search?q=title:test It will work.

However, if I do a general search without specifying the attribute like so:
http://localhost:9200/myindex/mymapping/_search?q=test

It will not find the document.

How do I specify that I want the default analyzer to be 'snowball' so I don't have to specify the attribute I want to search on?

p.s. I am using the Tire Gem. So please answer as best as you can taking that into account.


回答1:


It's possible to change default analyzer using index settings:

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:


It's easiest to set this in the elasticsearch.yml settings file:

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


来源:https://stackoverflow.com/questions/9670016/how-do-i-set-the-default-analyzer-for-elastic-search-with-tire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!