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

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:

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

{
	"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:

{
	"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:

{
	"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:

{
    "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:

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.

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

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.

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.

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