1

Here is my aggregation:

"aggregations": {
"nested-econtext": {
  "nested": {
    "path": "econtext"
  },
  "aggregations": {
    "nested-name": {
      "terms": {
        "field": "econtext.name.raw",
        "size": 2147483647,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_key": "asc"
          }
        ]
      },
      "aggregations": {
        "nested-type": {
          "terms": {
            "field": "econtext.type",
            "size": 2147483647,
            "min_doc_count": 1,
            "shard_min_doc_count": 0,
            "show_term_doc_count_error": false,
            "order": [
              {
                "_count": "desc"
              },
              {
                "_key": "asc"
              }
            ]
          },
          "aggregations": {
            "other-fields": {
              "terms": {
                "script": {
                  "source": "if(doc['econtext.facets.key.raw'].size()!=0){doc['econtext.facets.key.raw'].value + '|' + doc['econtext.facets.value.raw'].value}",
                  "lang": "painless"
                }
              }
            }
          }
        }
      }
    }
  }
}

}

The script here is

"if(doc['econtext.facets.key.raw'].size()!=0){doc['econtext.facets.key.raw'].value + '|' + doc['econtext.facets.value.raw'].value}"

However I do have results

Here is an example of what we have in our DB

"econtext" : [
        {
          "name" : "Answering Machines",
          "type" : "T",
          "facets" : [
            {
              "key" : "domain",
              "value" : "product"
            }
          ]
        }

and this is what is returned...

{
        "key" : "Answering Machines",
        "doc_count" : 1,
        "nested-type" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : "T",
              "doc_count" : 1,
              "other-fields" : {
                "doc_count_error_upper_bound" : 0,
                "sum_other_doc_count" : 0,
                "buckets" : [ ]
              }
            }
          ]
        }

Why doesn't this script return "domain|product", as shown above, it stores nothing in the bucket but I see it in the DB. Actually, for all data returned, all of them return empty buckets when not all are empty.

However, not all documents have data in the facets array, which is why I do the size check in the script.

Chieve
  • 59
  • 6
  • 1
    At first glance: It seems like econtext.facets is a list, so you can't access this as a property in painless. You need to loop through – Muhammad Ali Feb 07 '20 at 09:28

0 Answers0