Elasticsearch嵌套查询返回所有嵌套列表

时间:2018-07-10 17:35:52

标签: ruby ruby-on-rails-4 elasticsearch elasticsearch-5

我有一个带有嵌套字段events和字段team_id的模型。这些嵌套的事件可能非常庞大,我想对此做些事情。

  1. 仅返回具有特定team_id的事件,该事件与下面的查询无关,该查询返回所有事件

  2. 使用inner_hits进行排序,限制大小和偏移量,也可以通过发送sizefrom参数而没有运气来尝试

我在Ruby中使用elasticsearch 5

https://www.elastic.co/guide/en/elasticsearch/reference/5.0/search-request-inner-hits.html#nested-inner-hits

Model.search(
        query: {
          bool:{
            must: [
              { match: { "_id":  "zzzzzzz" }},
              nested: {
                path: "events",
                query: {
                          match: { "events.team_id":  "sdlfjslasdfj" }
                },
                inner_hits: {}
              }
            ]
          }
        }
      )

1 个答案:

答案 0 :(得分:0)

使用elasticsearch-api宝石使其正常工作 https://github.com/elastic/elasticsearch-ruby/tree/master/elasticsearch-api

response = client.search index: 'index_name',
                           body: {
                             "_source": false,
                              "query": {
                                "nested": {
                                  "path": "events",
                                  "query": {
                                    "bool": {
                                      "must":[
                                        {"match": {"events.team_id": "xyz"}},
                                        {"match": {"_id": "sdlfjslasdfj"}}
                                      ]
                                    }
                                   },
                                   "inner_hits": {
                                     "size": 10,
                                     "from": 0,
                                     "sort": [{ "events.starts_at": "asc" }]
                                   }
                                  }
                                }
                              }