首页 > 解决方案 > ElasticSearch 按数组第一个索引处的 NestedObject 中的字段排序

问题描述

我正在尝试在以下文档中对数组的第一个对象内的字段进行排序每个文档都有一个数组我想检索按它们排序的文档的第一个对象按那里的城市名称让我们在以下结果中命名我想要的第一个第三个文件,因为城市的名称以“L”('london')开头,然后是第二个“M”('Moscow'),然后是第三个“N”('NYC')

该结构是一个记录:

  1. 有一个数组
  2. 该数组包含一个对象(称为“地址”)
  3. 该对象有一个字段(称为“城市”)

我想按第一个 address.cities 对文档进行排序

    get hello/_mapping 
    {
      "hello": {
        "mappings": {
          "jack": {
            "properties": {
              "houses": {
                "type": "nested",
                "properties": {
                  "address": {
                    "properties": {
                      "city": {
                        "type": "text",
                        "fields": {
                          "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

这些是我索引的文档

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "hello",
        "_type": "jack",
        "_id": "2",
        "_score": 1,
        "_source": {
          "houses": [
            {
              "address": {
                "city": "moscow"
              }
            },
            {
              "address": {
                "city": "belgrade"
              }
            },
            {
              "address": {
                "city": "Sacramento"
              }
            }
          ]
        }
      },
      {
        "_index": "hello",
        "_type": "jack",
        "_id": "1",
        "_score": 1,
        "_source": {
          "houses": [
            {
              "address": {
                "city": "NYC"
              }
            },
            {
              "address": {
                "city": "PARIS"
              }
            },
            {
              "address": {
                "city": "TLV"
              }
            }
          ]
        }
      },
      {
        "_index": "hello",
        "_type": "jack",
        "_id": "3",
        "_score": 1,
        "_source": {
          "houses": [
            {
              "address": {
                "city": "London"
              }
            }
          ]
        }
      }
    ]
  }
}

标签: arrayssortingelasticsearchnested

解决方案


试试这个(当然,如果字段可能为空,请在脚本中添加一些测试。注意它可能会很慢,因为弹性不会索引这个值。添加一个主地址字段肯定会更快(并且真的更快)并且会做它的好方法。

{
      "sort" : {
        "_script" : {
            "script" : "params._source.houses[0].address.city",
            "type" : "string",
            "order" : "asc"
        }
    }
}

您必须使用 _source 而不是 doc[yourfield] 因为您不知道以女巫顺序弹性存储您的数组。

编辑:测试字段是否存在

{
  "query": {
    "nested": {
      "path": "houses",
      "query": {
        "bool": {
          "must": [
            {
              "exists": {
                "field": "houses.address"
              }
            }
          ]
        }
      }
    }
  },
  "sort": {
    "_script": {
      "script" : "params._source.houses[0].address.city",
      "type": "string",
      "order": "asc"
    }
  }
}

推荐阅读