我正在尝试使用 Tire 对持久模型执行嵌套查询。模型(事物)具有标签,我正在寻找所有带有特定标签的事物
class Thing
  include Tire::Model::Callbacks
  include Tire::Model::Persistence
  index_name { "#{Rails.env}-thing" }
  property :title, :type => :string
  property :tags, :default => [], :analyzer => 'keyword', :class => [Tag], :type => :nested
end
嵌套查询看起来像
class Thing 
   def self.find_all_by_tag(tag_name, args)
      self.search(args) do
         query do
            nested path: 'tags' do
               query do
                  boolean do
                     must { match 'tags.name', tag_name }
                 end
               end
            end
         end
      end 
   end 
 end
当我执行查询时,出现“非嵌套类型”错误
Parse Failure [Failed to parse source [{\"query\":{\"nested\":{\"query\":{\"bool\":{\"must\":[{\"match\":{\"tags.name\":{\"query\":\"TestTag\"}}}]}},\"path\":\"tags\"}},\"size\":10,\"from\":0,\"version\":true}]]]; nested: QueryParsingException[[test-thing] [nested] nested object under path [tags] is not of nested type]; }]","status":500}
查看轮胎的来源,似乎映射是从传递给“属性”方法的选项创建的,所以我认为我不需要在类中单独的“映射”块。谁能看到我做错了什么?
更新
按照下面 Karmi 的回答,我重新创建了索引并验证了映射是否正确:
thing: {
  properties: {
    tags: {
      properties: {
        name: {
          type: string
        }
        type: nested
      }
    }
    title: {
      type: string
    }
   }
但是,当我向 Thing 添加新标签时
thing = Thing.new
thing.title = "Title"
thing.tags << {:name => 'Tag'}
thing.save
映射恢复为“动态”类型并且“嵌套”丢失。
thing: {
  properties: {
    tags: {
      properties: {
        name: {
          type: string
        }
        type: "dynamic"
      }
    }
    title: {
      type: string
    }
   }
查询失败并出现与以前相同的错误。添加新标签时如何保留嵌套类型?