ElasticSearch - 在嵌套列表中获取分页结果(嵌套分页)

时间:2017-04-13 08:41:43

标签: elasticsearch

我有以下描述乡村城市(1:n)关系的Json

{
    "country": [
        {
            "id": 1,
            "name": "Country1",
            "city": [
                {"id": 1, "name": "City1"},
                {"id": 2,"name": "City2"}
            ]
        }, {
            "id": 2,
            "name": "Country2",
            "city": [
                {"id": 3,"name": "City3"},
                {"id": 4,"name": "City4"}
            ]
        }, {
            "id": 3,
            "name": "Country3",
            "city": [
                {"id": 5,"name": "City5"},
                {"id": 6,"name": "City6"}
            ]
        }
    ]
}

我已将其加载到包含三个国家/地区的3个文档的ES地图中 我在城市索引中添加了嵌套属性

...
 "city": {
             "type": "nested",
...

我想查询所有城市并获得分页结果。

  1. 例如3次点击将返回city1,city2,city3
  2. 我想按国家/地区名称进行过滤
  3. 我试过

    GET /127.0.0.1:9200/country_city/_search
    {
        "from": 0,
        "size": 2,
        "fields": [
           "city.id", "city.name"
        ]
    }
    

    GET /127.0.0.1:9200/country_city/country/_search?_source=false
    {
      "query": {
        "nested": {
          "path": "city",
          "query": {
            "match_all": {}
          },
          "inner_hits": {
              "sort": "city.id",
              "from": 0,
              "size": 3
          }
        }
      },
      "fields": [
        "name",
        "city.id",
        "city.name"
      ]
    }
    

    但是第一个返回了两个4个城市而不是2个 (2个国家各有2个城市)
    第二个返回所有文档(虽然请求中的大小为2),并且在一个内部元素中返回了每个国家的前3个城市。

    如何获取嵌套对象的页面大小? 然后进入下一页?

1 个答案:

答案 0 :(得分:0)

这应该有效

<强>映射

{
    "mappings": {
        "type": {
            "properties": {
                "country": {
                    "type": "nested",
                    "properties": {
                        "id": {
                            "type": "integer"
                        },
                        "name": {
                            "type": "text"
                        },
                        "city": {
                            "type": "nested",
                            "properties": {
                                "id": {
                                    "type": "integer"
                                },
                                "name": {
                                    "type": "keyword"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

<强>查询

{
    "query": {
        "nested": {
            "path": "country",
            "inner_hits": {},
            "query": {
                "nested": {
                    "path": "country.city",
                    "query": {
                        "match_all": {}
                    },
                    "inner_hits": {
                        "from": 0,
                        "size": 1,
                        "_source": {
                            "includes": ["country.city.name", "country.city.id"]
                        }
                    }
                }
            }
        }
    }
}

github bug

source filtering

由于