我想使用輪胎在持久模型上執行嵌套查詢。該模型(事)有標籤,我期待找到標有一定的標籤Elasticsearch /輪胎與持久對象的嵌套查詢
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}
縱觀源輪胎似乎映射從傳遞給該選項創建的‘屬性’的方法,所以我不認爲我需要一個單獨的‘在映射’塊班上。任何人都可以看到我做錯了什麼?
UPDATE
按照下面果報工作者的回答,我重新創建索引並驗證了映射是正確的:
thing: {
properties: {
tags: {
properties: {
name: {
type: string
}
type: nested
}
}
title: {
type: string
}
}
然而,當我加入新的標籤,以事
thing = Thing.new
thing.title = "Title"
thing.tags << {:name => 'Tag'}
thing.save
映射恢復爲「動態」類型,「嵌套」丟失。
thing: {
properties: {
tags: {
properties: {
name: {
type: string
}
type: "dynamic"
}
}
title: {
type: string
}
}
該查詢失敗,出現與以前相同的錯誤。添加新標籤時如何保留嵌套類型?
非常感謝您的幫助,以及一個優秀的圖書館。添加新標籤時,我仍然遇到保留嵌套文檔類型的問題。我已經用細節更新了這個問題,你能解釋一下嗎? –
我得到了這個工作,謝謝。我必須在每個測試的設置中調用Thing.create_elasticsearch_index以確保映射已正確創建。 –