Ruby on rails 使用持久对象的Elasticsearch/tire嵌套查询
我正在尝试使用Tire对持久化模型执行嵌套查询。模型(东西)有标签,我希望找到所有带有特定标签的东西Ruby on rails 使用持久对象的Elasticsearch/tire嵌套查询,ruby-on-rails,ruby,elasticsearch,tire,Ruby On Rails,Ruby,elasticsearch,Tire,我正在尝试使用Tire对持久化模型执行嵌套查询。模型(东西)有标签,我希望找到所有带有特定标签的东西 class Thing include Tire::Model::Callbacks include Tire::Model::Persistence index_name { "#{Rails.env}-thing" } property :title, :type => :string property :tags, :default => [], :an
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
当我执行查询时,我得到一个“notofnestedtype”错误
查看Tire的源代码,似乎映射是从传递给“property”方法的选项创建的,因此我认为我不需要在类中使用单独的“mapping”块。有人能看出我做错了什么吗
更新
按照Karmi下面的回答,我重新创建了索引,并验证了映射是否正确:
thing: {
properties: {
tags: {
properties: {
name: {
type: string
}
type: nested
}
}
title: {
type: string
}
}
然而,当我给这个东西添加新标签时
thing = Thing.new
thing.title = "Title"
thing.tags << {:name => 'Tag'}
thing.save
查询失败,错误与以前相同。添加新标记时如何保留嵌套类型 是的,实际上,
属性
声明中的映射配置是在持久性集成中传递的
在这样的情况下,始终存在且唯一的第一个问题:映射对于real是什么样子的
因此,使用Thing.index.mapping
方法或Elasticsearch的RESTAPI:curl localhost:9200/things/\u mapping
来查看一下
很有可能,您的索引是基于您使用的JSON使用动态映射创建的,并且您后来更改了映射。在这种情况下,将跳过索引创建逻辑,并且映射不是您所期望的
当索引映射与模型中定义的映射不同时,将显示警告。非常感谢您的帮助,这是一个非常好的库。添加新标记时,保留嵌套文档类型仍然存在问题。我已经更新了这个问题的细节,你能解释一下吗?我已经完成了,谢谢。我必须在每个测试的设置中调用Thing.create_elasticsearch_index,以确保正确创建映射。
thing = Thing.new
thing.title = "Title"
thing.tags << {:name => 'Tag'}
thing.save
thing: {
properties: {
tags: {
properties: {
name: {
type: string
}
type: "dynamic"
}
}
title: {
type: string
}
}