[英]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.