# Sorting on nested elements

**URL:** <https://discuss.elastic.co/t/sorting-on-nested-elements/318>\
**Category:** Elasticsearch\
**Created:** [May 6, 2015, 10:07pm UTC](https://discuss.elastic.co/t/sorting-on-nested-elements/318 "2015-05-06T22:07:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![blake1024](https://avatars.discourse-cdn.com/v4/letter/b/4491bb/32.png) [@blake1024](https://discuss.elastic.co/u/blake1024)\
**Post date:** [May 6, 2015, 10:07pm UTC](https://discuss.elastic.co/t/sorting-on-nested-elements/318/1 "2015-05-06T22:07:05Z")

</div>

Greetings,

I have a query that I want to be sorted by specific nested elements. I get back the correct result with no errors, but it is not sorted. I feel confident that it is something to do with the "sort" element of my query. Really kind of lost. Sure appreciate some help.

Mapping:

```
{
  "components": {
    mappings: {
      component: {
        properties: {
          classifications: { type: "string", index: "not_analyzed" },
          componentProperties: {
            type: "nested",
            properties: {
              name: { type: "string" },
              numericValue: { type: "double" },
              stringValue: { type: "string" },
              units: { type: "string" }
            }
          },
          name: { type: "string" },
          position: { type: "long" }
        }
      }
    }
  }
}

```

Query:

```
curl -s -XGET 'http://localhost:9200/components/_search' -d '{
  from: 0,
  size: 20,
  query: {
    bool: {
      must: [
        {
          match_phrase_prefix: { classifications: "passive_components/resistors/single_components/" }
        }
      ]
    }
  },
  sort: [
    {
      "componentProperties.numericValue": {
        order: "asc",
        nested_filter: { term: { "componentProperties.name": "Resistance" } }
      }
    }
  ]
}
'

```

So, I want it sorted by the numericValue element when that element is associated with name=Resistance.

Example single document:

```
  {
    _index: "components",
    _type: "component",
    _id: "391a635c-ee56-46d1-9d9e-343d89b323c5",
    _score: null,
    _source: {
      name: "R 1206 1.1 0.333W 5% Thick Film",
      classifications: "passive_components/resistors/single_components/",
      componentProperties: [
        {
          name: "Resistance",
          stringValue: "1.1",
          numericValue: 1.1,
          units: "ohm"
        },
        {
          name: "Cp",
          stringValue: "0.07e-12",
          numericValue: 7e-14,
          units: "F"
        },
        { name: "Octopart mpn", stringValue: "ESR18EZPJ1R1" },
        {
          name: "Ls",
          stringValue: "0.95e-9",
          numericValue: 9.5e-10,
          units: "H"
        }
      ],
      position: 12
    },
    sort: ["Infinity"]
  }

```

Sure appreciate some help!

Thanks.

Blake McBride

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [May 6, 2015, 11:00pm UTC](https://discuss.elastic.co/t/sorting-on-nested-elements/318/2 "2015-05-06T23:00:36Z")

</div>

I suspect the issue is with your nested filter: you are using a term query on an analyzed field, so you are trying to find "Resistance" while the index probably contains "resistance" (lowercase). You should either make your componentProperties.name field not\_analyzed (typically a good idea if it is an enum) or use a match query instead of a term query (typically a better idea if this is a full text field).

---

<div class="post-metadata">

**Author:** ![blake1024](https://avatars.discourse-cdn.com/v4/letter/b/4491bb/32.png) [@blake1024](https://discuss.elastic.co/u/blake1024)\
**Post date:** [May 7, 2015, 2:25pm UTC](https://discuss.elastic.co/t/sorting-on-nested-elements/318/3 "2015-05-07T14:25:25Z")

</div>

Downcasing 'Resistance' fixed the problem. It would be great if auto-downcasing indexes was highlighted in the docs when discussing sorting and elsewhere where it is applicable.

I really appreciate your help. I don't know how I would have figured it out without your help.

Thanks.

Blake

---

<div class="post-metadata">

**Author:** ![blake1024](https://avatars.discourse-cdn.com/v4/letter/b/4491bb/32.png) [@blake1024](https://discuss.elastic.co/u/blake1024)\
**Post date:** [May 7, 2015, 4:11pm UTC](https://discuss.elastic.co/t/sorting-on-nested-elements/318/4 "2015-05-07T16:11:47Z")

</div>

Interestingly, when you do a nested search, the data elements are _not_ downcased, but when you do a nested sort, they are. Seems inconsistent.

---

<div class="post-metadata">

**Author:** ![blake1024](https://avatars.discourse-cdn.com/v4/letter/b/4491bb/32.png) [@blake1024](https://discuss.elastic.co/u/blake1024)\
**Post date:** [May 7, 2015, 7:56pm UTC](https://discuss.elastic.co/t/sorting-on-nested-elements/318/5 "2015-05-07T19:56:48Z")

</div>

It is now working for the "resistance" field, but it doesn't work with the "octopart mpn" field. I can't figure out why not. Could it be because of the space in the name?

Thanks!

Blake

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [May 10, 2015, 9:53pm UTC](https://discuss.elastic.co/t/sorting-on-nested-elements/318/6 "2015-05-10T21:53:36Z")

</div>

> [@blake1024](#):
>
> Interestingly, when you do a nested search, the data elements are not downcased, but when you do a nested sort, they are.

Sorry I'm not following what you mean here, could you elaborate a bit (an example would be great)?

---

<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:** [July 6, 2017, 12:14am UTC](https://discuss.elastic.co/t/sorting-on-nested-elements/318/7 "2017-07-06T00:14:44Z")

</div>


