# Sort nested field with two conditions

**URL:** <https://discuss.elastic.co/t/sort-nested-field-with-two-conditions/234804>\
**Category:** Elasticsearch\
**Created:** [May 28, 2020, 7:28pm UTC](https://discuss.elastic.co/t/sort-nested-field-with-two-conditions/234804 "2020-05-28T19:28:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![net](https://avatars.discourse-cdn.com/v4/letter/n/9de0a6/32.png) [@net](https://discuss.elastic.co/u/net)\
**Post date:** [May 28, 2020, 7:28pm UTC](https://discuss.elastic.co/t/sort-nested-field-with-two-conditions/234804/1 "2020-05-28T19:28:17Z")

</div>

Here is what my mapping looks like:

Elasticsearch version 5.5

```auto
{
  "myindex": {
    "mappings": {
      "mythings": {
        "properties": {
          "title": {
            "type": "text"
          },
          "custom_labels": {
            "type": "nested",
            "properties": {
              "label": {
                "type": "keyword"
              },
              "label_group": {
                "type": "keyword"
              },
              "label_group_id": {
                "type": "integer"
              },
              "label_id": {
                "type": "integer"
              }
            }
          }
        }
      }
    }
  }
}

```

And the data looks like this:

```auto
{
  "_source": {
    "title": "The data of the sun",
    "custom_labels": [
      "label": "Green spike",
      "label_group": "spikes",
      "label_group_id": 52,
      "label_id": 9
    ]
  },
  "_source": {
    "title": "Another day",
    "custom_labels": [
      "label": "Apple spike",
      "label_group": "spikes",
      "label_group_id": 52,
      "label_id": 25
    ]
  },
  "_source": {
    "title": "Day five",
    "custom_labels": [
      "label": "Tread",
      "label_group": "tires",
      "label_group_id": 45,
      "label_id": 1
    ]
  }
}

```

I would like to sort the list by saying:

label\_group === "spikes" AND sort by "label" in ascending order

so the result would be:

```auto
{
    "_source": {
    "title": "Another day",
    "custom_labels": [
      "label": "Apple spike",
      "label_group": "spikes",
      "label_group_id": 52,
      "label_id": 25
    ]
  },
  "_source": {
    "title": "The data of the sun",
    "custom_labels": [
      "label": "Green spike",
      "label_group": "spikes",
      "label_group_id": 52,
      "label_id": 9
    ]
  },
  "_source": {
    "title": "Day five",
    "custom_labels": [
      "label": "Tread",
      "label_group": "tires",
      "label_group_id": 45,
      "label_id": 1
    ]
  }
}

```

---

<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:** [June 25, 2020, 7:28pm UTC](https://discuss.elastic.co/t/sort-nested-field-with-two-conditions/234804/2 "2020-06-25T19:28:23Z")

</div>

Closed on June 25, 2020, 7:28pm UTC

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