2

我有这个要求:

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"]}}
                ]
              }
            }
          }
        }
      }
    }
  }
}'

我没有成功将它转换成轮胎(rails gem)语言......我在轮胎测试中没有找到任何嵌套过滤器的例子......

有任何想法吗?

4

1 回答 1

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

于 2013-03-05T14:59:13.583 回答