Aggregation collecting pairs


(Stefan) #1

Hi all

I want to calculate a facet/aggregation where all CodeText pairs are returned. At the moment I therefore aggregate over the codes and then try to add the texts be aggregation too. However, this does not work. I prepared an example below and appreciate and help or idea.

Lets consider the following example (requests are minified to keep this post's size limited)

PUT index/a/_mapping
{"a":{"properties":{"b":{"properties":{"c":{"properties":{"code":{"type":"string"},"text":{"type":"string"}}}}}}}}

PUT index/a/1
{"a":{"b":{"c":[{"code":"123","text":"abc"},{"code":"456","text":"def"}]}}}

PUT index/a/2
{"a":{"b":{"c":[{"code":"123","text":"abc"},{"code":"789","text":"ghi"}]}}}

What I want to have is all available c-elements, i.e., code as well as related text. My current aggregation looks as follows:

POST index/a/_search
{"size":0,"aggs":{"allCCodes":{"terms":{"field":"a.b.c.code"},"aggs":{"allCTexts":{"terms":{"field":"a.b.c.text"}}}}}}

Unfortunately it returns the following (simplified):

123 ---> abc, def, ghi
456 ---> abc, def
789 ---> abc, ghi

What I want

123 ---> abc
456 ---> def
789 ---> ghi

Any ideas?

Best regards and wishes,
Stefan


(Taras Shkvarchuk) #2

What you wanted to do is to create c nodes as nested object.

Updated mapping: (assumes

PUT index/a/_mapping
{"properties": { "a":{"properties":{"b":{"properties":{"c":{"type":"nested", "properties":{"code":{"type":"string"},"text":{"type":"string"}}}}}}}}}

Updated query:

POST index/a/_search
{"size": 0,"aggs": {
    "nest": {
      "nested": {
        "path": "a.b.c"
      },
      "aggs": {
        "allCCodes": {
          "terms": {"field": "a.b.c.code"
          },"aggs": {
            "allCTexts": {
              "terms": {"field": "a.b.c.text"}
            }
          }
}}}}}

(Stefan) #3

Thanks a lot. I will give this a try.

Cheers, Stefan


(Stefan) #4

Perfect! It works like a charm. You saved the day, thanks a lot.

Stefan


(system) #5