Elasticsearch 似乎没有按预期工作

Elasticsearch does not seem to be working as expected

我正在使用 elasticsearch 和 globalize gems 进行全文搜索,我希望我可以使用 czech/english 分析器搜索供应商名称和本地化描述。

示例: 供应商名称:"Bonami.cz" 供应商 Description_CZ:"Test description in czech."

当我搜索 "Bonami.cz" 时有效,但当我搜索时无效(0 个结果):

根据文档,以下方法应该有效,但显然我遗漏了一些东西。我验证了索引和数据在 ElasticSearch 中。

在模型中使用之前,我是否还需要以某种方式安装 czech/english 分析器?

require 'elasticsearch/model'
require 'activerecord-import'

class Supplier < ActiveRecord::Base
    after_commit lambda { __elasticsearch__.index_document  },  on: :create
    after_commit lambda { __elasticsearch__.update_document },  on: :update

    translates :description, :fallbacks_for_empty_translations => true
    accepts_nested_attributes_for :translations

    include Elasticsearch::Model
    include Elasticsearch::Model::Callbacks
    include Elasticsearch::Model::Globalize::MultipleFields

        mapping do
          indexes :id,             type: 'integer'
          indexes :name,           analyzer: 'czech'
          indexes :description_ma, analyzer: 'czech'
          indexes :description_cs, analyzer: 'czech'
          indexes :description_en, analyzer: 'english'
       end

       def as_indexed_json(options={})
          { id: id,
            name: name,
            description_ma: description_ma,
            description_cs: description_cs,
            description_en: description_en    
         }
       end

       def self.search(query)
         __elasticsearch__.search(
            {
             query: {
               multi_match: {
                 query: query,
                 fields: ['name^10', 'description_ma', 'description_cs', 'description_en']
               }
            }
           })
       end
end

知道吗,有什么问题吗? 谢谢,米罗斯拉夫

更新 1 我的灵感来自 Rails 4, elasticsearch-rails 的解决方案,但是当我现在尝试搜索时,对于任何词我总是得到零结果。

settings index: {
    number_of_shards: 1,
    analysis: {
      filter: {
        trigrams_filter: {
          type: 'ngram',
          min_gram: 2,
          max_gram: 10
        },
        content_filter: {
          type: 'ngram',
          min_gram: 4,
          max_gram: 20
        }
      },
      analyzer: {
        index_trigrams_analyzer: {
          type: 'custom',
          tokenizer: 'standard',
          filter: ['lowercase', 'trigrams_filter']
        },
        search_trigrams_analyzer: {
          type: 'custom',
          tokenizer: 'whitespace',
          filter: ['lowercase']
        },
        english: {
          tokenizer: 'standard',
          filter: ['standard', 'lowercase', 'content_filter']
        },
        czech: {
          tokenizer: 'standard',
          filter: ['standard','lowercase','content_filter']
        }
      }
    }
    } do
    mappings dynamic: 'false' do
      indexes :name, index_analyzer: 'index_trigrams_analyzer', search_analyzer: 'search_trigrams_analyzer'
      indexes :description_en, index_analyzer: 'english', search_analyzer: 'english'
      indexes :description_ma, index_analyzer: 'czech', search_analyzer: 'czech'
      indexes :description_cs, index_analyzer: 'czech', search_analyzer: 'czech'
    end
end

  def as_indexed_json(options={})
    { id: id,
      name: name,
      description_ma: description_ma,
      description_cs: description_cs,
      description_en: description_en    
    }
  end

   def self.search(query)
    __elasticsearch__.search(
      {
        query: {
          multi_match: {
            query: query,
            fields: ['name^10', 'description_ma', 'description_cs', 'description_en']
          }
        },
        highlight: {
          pre_tags: ['<em>'],
          post_tags: ['</em>'],
            fields: {
              name: {},
              description_ma: {},
              description_cs: {},
              description_en: {}
            }
        }
      }
    )
  end

这是我为给定模型打开弹性搜索 URL 时看到的内容:

{"suppliers":{"aliases":{},"mappings":{"supplier":    
{"dynamic":"false","properties":{"description_cs":      
{"type":"string","analyzer":"czech"},"description_en": 
{"type":"string","analyzer":"english"},"description_ma":
{"type":"string","analyzer":"czech"},"name":
{"type":"string","index_analyzer":"index_trigrams_analyzer","search_analyzer":"search_trigrams_analyzer"}}}},"settings":{"index":
{"creation_date":"1445797508427","analysis":{"filter":   
{"trigrams_filter":
{"type":"ngram","min_gram":"2","max_gram":"10"},"content_filter":
{"type":"ngram","min_gram":"4","max_gram":"20"}},"analyzer":{"english":
{"filter":["standard","lowercase","content_filter"],"tokenizer":"standard"},"index_trigrams_analyzer":{"type":"custom","filter":["lowercase","trigrams_filter"],"tokenizer":"standard"},"search_trigrams_analyzer":{"type":"custom","filter":["lowercase"],"tokenizer":"whitespace"},"czech":{"filter":["standard","lowercase","content_filter"],"tokenizer":"standard"}}},"number_of_shards":"1","number_of_replicas":"1","version": 
{"created":"1060099"},"uuid":"wX9kf3OQSva24Iwk7sZ8AQ"}},"warmers":{}}}

更新 2

缺少两个步骤使其按预期工作=> 1.重新导入模型数据 2. 描述字段名称中的拼写错误(而不是 description_ma/en/cs,我不得不使用 ma/cs/en_description。

     settings index: {
    number_of_shards: 1,
     analysis: {
      filter: {
        trigrams_filter: {
          type: 'ngram',
          min_gram: 2,
          max_gram: 10
        },
        content_filter: {
          type: 'ngram',
          min_gram: 4,
          max_gram: 20
        }
      },
      analyzer: {
        index_trigrams_analyzer: {
          type: 'custom',
          tokenizer: 'standard',
          filter: ['lowercase', 'trigrams_filter']
        },
        search_trigrams_analyzer: {
          type: 'custom',
          tokenizer: 'whitespace',
          filter: ['lowercase']
        },
        english: {
          tokenizer: 'standard',
          filter: ['standard', 'lowercase', 'content_filter']
        },
        czech: {
          tokenizer: 'standard',
          filter: ['standard','lowercase','content_filter' ]
        }
      }
    }
    } do
    mappings dynamic: 'false' do
         indexes :name, index_analyzer: 'index_trigrams_analyzer', search_analyzer: 'search_trigrams_analyzer'
      indexes :en_description, index_analyzer: 'english', search_analyzer: 'english'
      indexes :ma_description, index_analyzer: 'czech', search_analyzer: 'czech'
      indexes :cs_description, index_analyzer: 'czech', search_analyzer: 'czech'
    end
   end

  def as_indexed_json(options={})
    { id: id,
      name: name,
      ma_description: ma_description,
      cs_description: cs_description,
      en_description: en_description    
    }
  end

   def self.search(query)
    __elasticsearch__.search(
      {
        query: {
          multi_match: {
            query: query,
            fields: ['name^10', 'ma_description', 'cs_description', 'en_description']
          }
        },
        highlight: {
          pre_tags: ['<em>'],
          post_tags: ['</em>'],
            fields: {
              name: {},
              ma_description: {},
              cs_description: {},
              en_description: {}
            }
        }
      }
    )
  end

为了能够执行您尝试执行的搜索,您需要使用 Ngram 分析器。 (如评论中所述)