2013-02-19 146 views
2

我有這樣的要求:Elasticsearch查詢與輪胎

curl -XGET localhost:9200/users/_search -d ' 
{ 
    "query": { 
    "filtered": { 
     "query": {"match_all": {}}, 
     "filter": { 
     "nested": { 
      "path": "apps_events", 
      "query":{ 
      "filtered": { 
       "query": { "match_all": {}}, 
       "filter": { 
       "and": [ 
        {"term": {"apps_events.status": "active"}}, 
        {"terms": {"apps_events.type": ["sale"]}} 
       ] 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
}' 

我沒有成功將其轉化爲輪胎(軌道GEM)的語言...... 我沒有在輪胎測試發現嵌套的任何實例帶過濾器...

任何想法?

回答

3

好吧,我找到了答案:

nested_filter = Tire::Search::Query.new do 
    filtered do 
    query { all } 
    filter :term, { 'apps_events.status' => 'active' } 
    filter :terms, { 'apps_events.type' => ['sale'] } 
    end 
end 

tire.search(page: params[:page], per_page: params[:per_page], load: params[:load]) do 
    query do 
    filtered do 
     query { all } 
     # Merge the defined filter as a hash into the `nested` filter 
     filter :nested, { path: 'apps_events'}.merge({ query: nested_filter.to_hash }) 
    end 
    end 
end 

由於@karmiq https://github.com/karmi/tire/issues/660