我有这个要求:

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)语言...
我在Tyre测试中找不到嵌套过滤器的任何示例...

有任何想法吗?

最佳答案

好吧,我找到了答案:

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

关于elasticsearch - 用Tire进行Elasticsearch查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14964052/

10-11 08:36