【问题标题】:Elasticsearch nested query is returned all the nested listElasticsearch 嵌套查询返回所有嵌套列表
【发布时间】:2018-07-11 21:47:57
【问题描述】:

我有一个模型,它有一个嵌套字段 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: {}
              }
            ]
          }
        }
      )

【问题讨论】:

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


    【解决方案1】:

    使用elasticsearch-api gem 让它工作 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" }]
                                       }
                                      }
                                    }
                                  }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多