2013-01-11 90 views
0

我想使用輪胎在持久模型上執行嵌套查詢。該模型(事)有標籤,我期待找到標有一定的標籤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 
    } 
    } 

該查詢失敗,出現與以前相同的錯誤。添加新標籤時如何保留嵌套類型?

回答

1

是的,的確,property聲明中的映射配置在Persistence集成中傳遞。

在這樣的情況下,總是有且只有第一個問題:映射對於實際怎麼樣?

因此,使用例如。請看Thing.index.mapping方法或Elasticsearch的REST API:curl localhost:9200/things/_mapping

很可能您的索引是使用動態映射創建的,它基於您使用的JSON,並且之後您已經更改了映射。在這種情況下,索引創建邏輯被跳過,並且映射不是您所期望的。


有一個輪胎issue打開約當指數映射是從模型中定義的映射不同顯示警告。

+0

非常感謝您的幫助,以及一個優秀的圖書館。添加新標籤時,我仍然遇到保留嵌套文檔類型的問題。我已經用細節更新了這個問題,你能解釋一下嗎? –

+0

我得到了這個工作,謝謝。我必須在每個測試的設置中調用Thing.create_elasticsearch_index以確保映射已正確創建。 –