使用Tire进行Elasticsearch查询

时间:2013-02-19 17:47:19

标签: elasticsearch tire

我有这个要求:

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

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

有什么想法吗?

1 个答案:

答案 0 :(得分: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