【问题标题】:Why multi-field mapping is not working with tire gem for elasticsearch?为什么多场映射不适用于弹性搜索的轮胎宝石?
【发布时间】:2013-02-04 16:25:53
【问题描述】:

我正在使用弹性搜索来增强我的应用中的搜索功能。搜索工作正常,但排序不适用于包含多个单词的字段。

当我尝试按日志“消息”对搜索进行排序时,我收到了错误:

“无法对每个文档具有多个值或每个字段多个标记的字符串类型进行排序”

我搜索了错误并发现我可以在 :message 字段上使用多字段映射(一个已分析,另一个未分析)来对它们进行排序。所以我这样做了:

class Log < ActiveRecord::Base
  include Tire::Model::Search
  include Tire::Model::Callbacks

  tire.mapping do
    indexes :id, index: :not_analyzed
    indexes :source, type: 'string'
    indexes :level, type: 'string'
    indexes :created_at, :type => 'date', :include_in_all => false
    indexes :updated_at, :type => 'date', :include_in_all => false
    indexes :message, type: 'multi_field', fields: { 
      analyzed: {type: 'string', index: 'analyzed'},
      message: {type: 'string', index: :not_analyzed} 
    }
    indexes :domain, type: 'keyword'
  end
end

但是,由于某种原因没有将此映射传递给 ES。

rails console
Log.index.delete #=> true
Log.index.create #=> 200 : {"ok":true,"acknowledged":true}
Log.index.import Log.all #=> 200 : {"took":243,"items":[{"index":{"_index":"logs","_type":"log","_id":"5 ... ...

# Index mapping for :message is not the multi-field 
# as I created in the Log model... why?

Log.index.mapping
=> {"log"=>
  {"properties"=>
    {"created_at"=>{"type"=>"date", "format"=>"dateOptionalTime"},
     "id"=>{"type"=>"long"},
     "level"=>{"type"=>"string"},
     "message"=>{"type"=>"string"},
     "source"=>{"type"=>"string"},
     "updated_at"=>{"type"=>"date", "format"=>"dateOptionalTime"}}}}

# However if I do a Log.mapping I can see the multi-field
# how I can fix that and pass the mapping correctly to ES? 

Log.mapping
=> {:id=>{:index=>:not_analyzed, :type=>"string"},
 :source=>{:type=>"string"},
 :level=>{:type=>"string"},
 :created_at=>{:type=>"date", :include_in_all=>false},
 :updated_at=>{:type=>"date", :include_in_all=>false},
 :message=>
  {:type=>"multi_field",
   :fields=>
    {:message=>{:type=>"string", :index=>"analyzed"},
     :untouched=>{:type=>"string", :index=>:not_analyzed}}},
 :domain=>{:type=>"keyword"}}

所以,Log.index.mapping 是 ES 中的当前映射,它不包含我创建的多字段。我错过了什么吗?以及为什么Log.mapping 中显示多字段而不在Log.index.mapping 中显示?

【问题讨论】:

    标签: ruby-on-rails ruby mapping elasticsearch tire


    【解决方案1】:

    我已将工作流程更改为:

    Log.index.delete; Log.index.create; Log.import
    

    Log.index.delete; Log.create_elasticsearch_index; Log.import
    

    MyModel.create_elasticsearch_index 使用模型定义的正确映射创建索引。请参阅轮胎的问题#613

    【讨论】:

    • 在 Rails 控制台中运行这些也使我可以轻松诊断导致索引无法更新的错误,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多