Elasticsearch似乎没有按预期工作

时间:2015-10-25 12:44:31

标签: elasticsearch analyzer globalize

我正在使用elasticsearch和全球化宝石进行全文搜索,我期望的是我可以使用捷克语/英语分析器搜索供应商名称,本地化描述。

实施例: 供应商名称:“Bonami.cz” 供应商描述_CZ:“捷克的测试描述。”

当我搜索“Bonami.cz”时它会起作用,但当我搜索时它不起作用(0结果):

  • “Bonami”(单词的一部分)
  • “test”(描述)

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

在使用模型之前,我还需要以某种方式安装捷克语/英语分析器吗?

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

缺少两个步骤让它按预期工作=&gt; 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

1 个答案:

答案 0 :(得分:0)

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