简体   繁体   English

ElasticSearch按数组第一个索引处的NestedObject中的字段排序

[英]ElasticSearch sort by field in NestedObject At First Index Of Array

I am trying to sort a field inside the first object of an array in the following docs each docs has an array i want to retrieve the docs sorted by they first objects by there city name lets name that in the following result I want to have first the third documents because the name of the city its start by "L" ('london') then the second "M" ('Moscow') then the third "N" ('NYC') 我正在尝试在以下文档中对数组的第一个对象内的字段进行排序,每个文档都有一个数组,我想检索按它们的第一个对象排序的文档,并按城市名称让其在以下我首先要具有的结果中命名第三个文件是因为城市名称以“ L”(伦敦)开头,然后是第二个“ M”(莫斯科),然后是第三个“ N”(纽约市)

the structure is a record that: 该结构是以下记录:

  1. has an array 有一个数组
  2. the array contains an object (called 'address') 数组包含一个对象(称为“地址”)
  3. the object has a field (called 'city') 该对象具有一个字段(称为“城市”)

i want to sort the docs by the first 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
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

Thos are the document that i indexed 这是我索引的文档

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

Try this (of course, add some test inside the script if field could be empty. Note it could be pretty slow, because elastic wont have this value indexed. Add a main address field would be faster (and really faster) for sure and would be the good way to do it. 尝试一下(当然,如果字段可以为空,请在脚本内添加一些测试。请注意,这可能会很慢,因为弹性不会为该值建立索引。添加主地址字段肯定会更快(并且实际上会更快),并且会成为做到这一点的好方法。

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

You have to use _source instead of doc[yourfield] because you dont know in witch order elastic store your array. 您必须使用_source而不是doc [yourfield],因为您不知道按顺序弹性存储数组。

EDIT: test if field exist 编辑:测试字段是否存在

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM