# Sort by nested values "across" all documents (duplicate parent documents)

**URL:** https://discuss.elastic.co/t/sort-by-nested-values-across-all-documents-duplicate-parent-documents/213257
**Category:** Elasticsearch
**Created:** [December 28, 2019, 3:28pm UTC](https://discuss.elastic.co/t/sort-by-nested-values-across-all-documents-duplicate-parent-documents/213257 "2019-12-28T15:28:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![awagner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/awagner/32/60049_2.png) [@awagner](https://discuss.elastic.co/u/awagner)
#### Post date: [December 28, 2019, 3:28pm UTC](https://discuss.elastic.co/t/sort-by-nested-values-across-all-documents-duplicate-parent-documents/213257/1 "2019-12-28T15:28:57Z")

</div>

Hi all,

First question, please forgive me if I'm ignorant of some essentials.

Here is the issue: Among other options, I want to offer an ordering of query results based on the values of nested fields. That is, if a document has the first and the third value of all possible values, it should be listed in the first place, together with the corresponding nested field value, and then again in the third place, with the other nested field value. An examplary list is at the end of this post.

I have some documents like these:

PUT /test:

```auto
{
  "mappings": {
    "doc": {
      "dynamic": false,
      "properties": {
        "elternid": { "type": "keyword" },
        "datum": { "type" : "keyword" },
        "form": { "type": "text",
                  "fields": {
                    "raw": { "type": "keyword" }
                 }
        },
        "titel": { "type": "text" },
        "betreff": { "type": "nested",
					"dynamic": false,
					"properties": {
						"grp_id": { "type" : "keyword" },
						"grp_string" : { "type" : "keyword" },
						"gruppe": { "type" : "text" }
					}
        }
      }
    }
  }
}

```

And documents:

PUT /test/\_doc/1

```auto
{
	"elternid": "Dokument 1",
	"datum": "1950-12-30",
	"form": "irgendwas langes",
	"titel": "noch ein längerer Text",
	"betreff": [
		{
			"grp_string" : "a.1",
			"grp_id": "blabla",
			"gruppe": "schon wieder langer Text"
		},
		{
			"grp_string" : "b.2",
			"grp_id": "blabla",
			"gruppe": "schon wieder langer Text"
		},
		{
			"grp_string" : "c.1",
			"grp_id": "bloblo",
			"gruppe": "Kurztext"
		}
	]
}

```

PUT /test/\_doc/2:

```auto
{
	"elternid": "Dokument 2",
	"datum": "1970-01-01",
	"form": "irgendwas langes",
	"titel": "noch ein längerer Text",
	"betreff": [
		{
			"grp_string" : "a.1",
			"grp_id": "blabla",
			"gruppe": "schon wieder langer Text"
		},
		{
			"grp_string" : "a.3",
			"grp_id": "foo",
			"gruppe": "schon wieder langer Text"
		}
	]
}

```

PUT /test/\_doc/3:

```auto
{
	"elternid": "Dokument 3",
	"datum": "1940-01-01",
	"form": "irgendwas langes",
	"titel": "noch ein längerer Text",
	"betreff": [
		{
			"grp_string" : "a.2",
			"grp_id": "bar",
			"gruppe": "schon wieder langer Text"
		},
		{
			"grp_string" : "b.1",
			"grp_id": "baz",
			"gruppe": "nul"
		}
	]
}

```

For some reason, I was under the impression that I would have to do it with an aggregation and extract the data/ignore the hits client-side.

This is what I have:

POST /test/\_search:

```auto
{
    "from": 0,
    "size": 10,
    "query": {
        "bool": {
            "filter": [{ "match_all": {} }]
        }
    },
    "aggs": {
		"my_buckets": {
            "composite" : {
                "sources" : [
					{ "betreff": { "terms": { "script": "params._source.betreff.grp_string" } } },
                    { "datum": { "terms": {"field": "datum" } } },
                    { "form": { "terms": {"field": "form.raw" } } },
                    { "ter": { "terms": {"field": "elternid" } } }
                ]
            }
        }
    }
}

```

But it gives an error ("Illegal list shortcut value [grp\_string].").

In the end, I want to have a sorted list like this:

```auto
1. "a.1" "1950-12-30" "Dokument 1"
2. "a.1" "1970-01-01" "Dokument 2"
3. "a.2" "1940-01-01" "Dokument 3"
4. "a.3" "1970-01-01" "Dokument 2"
5. "b.1" "1940-01-01" "Dokument 3"
6. "b.2" "1950-12-30" "Dokument 1"
7. "c.1" "1950-12-30" "Dokument 1"

```

How should I be approaching this?  
Thanks for any insight,

Andreas

PS. I'm on ES 6.8.6.

---

<div class="post-metadata">

### Author: ![awagner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/awagner/32/60049_2.png) [@awagner](https://discuss.elastic.co/u/awagner)
#### Post date: [January 3, 2020, 9:29pm UTC](https://discuss.elastic.co/t/sort-by-nested-values-across-all-documents-duplicate-parent-documents/213257/2 "2020-01-03T21:29:45Z")

</div>

Almost a week and no reply; is this even possible at all?:

> [@](#):
>
> In the end, I want to have a sorted list like this:
> 
> ```auto
> [betreff.grp:string] [datum] [elternid]
> 1. "a.1" "1950-12-30" "Dokument 1"
> 2. "a.1" "1970-01-01" "Dokument 2"
> 3. "a.2" "1940-01-01" "Dokument 3"
> 4. "a.3" "1970-01-01" "Dokument 2"
> 5. "b.1" "1940-01-01" "Dokument 3"
> 6. "b.2" "1950-12-30" "Dokument 1"
> 7. "c.1" "1950-12-30" "Dokument 1"
> 
> ```

Given that "a.1", "a.2", "a.3", "b.1" etc. are values of a nested field (betreff.grp\_string) of the main documents "Dokument 1", "Dokument 2" etc.

---

<div class="post-metadata">

### Author: ![awagner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/awagner/32/60049_2.png) [@awagner](https://discuss.elastic.co/u/awagner)
#### Post date: [January 16, 2020, 4:06pm UTC](https://discuss.elastic.co/t/sort-by-nested-values-across-all-documents-duplicate-parent-documents/213257/3 "2020-01-16T16:06:28Z")

</div>

Okay, I could not get it to work except by changing the data model. Now I am using child documents instead of nested fields. Then I can query for and sort all child documents and retrieve necessary fields from the"main" documents via has\_parent and inner\_hits.

---

<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: [February 13, 2020, 4:06pm UTC](https://discuss.elastic.co/t/sort-by-nested-values-across-all-documents-duplicate-parent-documents/213257/4 "2020-02-13T16:06:30Z")

</div>

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