【问题标题】:ElasticSearch - Getting paged result in a nested list (nested pagination)ElasticSearch - 在嵌套列表中获取分页结果(嵌套分页)
【发布时间】:2017-04-13 08:41:42
【问题描述】:

我有以下描述国家-城市 (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. 我想按国家/地区名称过滤

我试过了

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 个城市。

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

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    这应该可行

    映射

    {
        "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

    谢谢

    【讨论】:

    • 这实际上返回了空的命中数组。即使我正在清除内部查询。顺便说一句,为什么外部对象也标记为嵌套?
    • 是的,因为另一个对象也是嵌套的。你有没有运行我共享的映射,国家和城市都是嵌套对象
    • 我做了,但是我的数据馈送器在馈送数据时会创建另一个映射(使用动态映射)。我的映射是包含“城市”的对象“国家”。你的对象是名称为 'type' 的对象,其中包含 'country' 和 'city'。
    • 是的,你必须避免动态映射,因为这是 has_many 关联,它总是与嵌套或父子关系相关。结帐elastic.co/guide/en/elasticsearch/reference/5.2/nested.html 和elastic.co/guide/en/elasticsearch/guide/master/…。是的,国家是嵌套的,就像国家内部的城市阵列一样。
    • 谢谢。我总是使用静态映射,但我设置了动态这个简单的例子。只是为了让我理解,您建议通过将 country 包装在另一个名为 type 的对象中并在每个 type 对象中保存一个实例来更改我的数据?
    猜你喜欢
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 2013-06-29
    • 2021-11-08
    • 1970-01-01
    • 2022-11-23
    • 2013-12-07
    • 1970-01-01
    相关资源
    最近更新 更多