# Сортировка по вложенным полям. Не получается. Ошибка

**URL:** <https://discuss.elastic.co/t/topic/106848>\
**Category:** Вопросы на русском языке\
**Created:** [November 8, 2017, 10:34am UTC](https://discuss.elastic.co/t/topic/106848 "2017-11-08T10:34:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![karelianboy1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karelianboy1/32/26477_2.png) [@karelianboy1](https://discuss.elastic.co/u/karelianboy1)\
**Post date:** [November 8, 2017, 10:34am UTC](https://discuss.elastic.co/t/topic/106848/1 "2017-11-08T10:34:20Z")

</div>

```auto
"customer": {
   "id": 2,
   "user_profile": {
     "rating": 0.16,
      "name": "Сергей",
      "surname": "Петров",
      "reviewsCount": 1
   }
 },

```

Вот такая вложенная структура.

Поисковый запрос такой.

```auto
GET mytasks/mytask/_search
{ "query": { "match_all": {} },
  "sort" : [
    "_score",
       {
          "customer.user_profile.rating" : {
             "order" : "desc",
             "nested_path" : "customer.user_profile"
          }
       },
       {"createdAt": {"order": "desc"}}
    ]
}

```

Получаю ошибку.

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "[nested] nested object under path [customer.user_profile] is not of nested type",
        "index_uuid": "pLlOA9jMTpa10DwJMWacrg",
        "index": "mytasks"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "mytasks",
        "node": "O0XJacQYR_SX-EKIeFk-MA",
        "reason": {
          "type": "query_shard_exception",
          "reason": "[nested] nested object under path [customer.user_profile] is not of nested type",
          "index_uuid": "pLlOA9jMTpa10DwJMWacrg",
          "index": "mytasks"
        }
      }
    ]
  },
  "status": 400
}

```

Может можно сортировать только, когда один уровень вложенности?  
А у меня получается двойной уровень вложенности.

Мэппинго определен сейчас так:

```auto
{
   "mappings": {
      "mytask": {
         "properties": {
            "customer": {
               "type": "nested"
            }
         }
      }
   }
}

```

---

<div class="post-metadata">

**Author:** ![karelianboy1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karelianboy1/32/26477_2.png) [@karelianboy1](https://discuss.elastic.co/u/karelianboy1)\
**Post date:** [November 8, 2017, 11:03am UTC](https://discuss.elastic.co/t/topic/106848/2 "2017-11-08T11:03:03Z")

</div>

Сам разобрался. Нужно более четко определить мэппинг.

```auto
PUT mytasks
{
   "mappings": {
      "mytask": {
         "properties": {
            "customer": {
               "type": "nested",
                "properties": {
                  "id": {
                    "type": "integer"
                  },
                  "user_profile": {
                    "type": "nested",
                    "properties": {
                      "rating": {
                        "type": "float"
                      }
                    }
                  }
                }
            },
            "createdAt": {
              "type": "date",
              "format": "date_optional_time||epoch_millis"
            }
         }
      }
   }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 6, 2017, 11:03am UTC](https://discuss.elastic.co/t/topic/106848/3 "2017-12-06T11:03:39Z")

</div>

Closed on December 6, 2017, 11:03am UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
