[英]Index field on embedded documents with tire gem for ElasticSearch?
[英]Why multi-field mapping is not working with tire gem for elasticsearch?
我正在使用弹性搜索来增强应用程序中的搜索功能。 搜索工作正常,但是不对具有多个单词的字段进行排序。
当我尝试按日志“消息”对搜索进行排序时,出现错误:
“不能对每个文档具有多个值或每个字段具有多个标记的字符串类型进行排序”
我在错误中进行了搜索,发现可以在: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
?
我将工作流程从:
Log.index.delete; Log.index.create; Log.import
至
Log.index.delete; Log.create_elasticsearch_index; Log.import
MyModel.create_elasticsearch_index
使用来自模型定义的正确映射创建索引。 请参阅《轮胎问题》 第613期 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.