danpaz / bodybuilder

An elasticsearch query body builder :muscle:

Home Page:http://bodybuilder.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for aggregations without field missing

yatishbalaji opened this issue · comments

For aggregations not done on any field like TopHits, there is no way to get it done.
Similarly for aggregation which has script without field doesn't work

"aggs" : {
    "doctype" : {
      "terms" : { "field" : "field1",  "order": [{ "maxScore": "desc" }] },
      "aggs": {
        "topHits": {
          "top_hits": {
            "size": 2,
            "_source": ["field1"],
            "track_scores" : true,
          }
        },
        "maxScore": {
          "max": {
            "script": "_score"
          }
        }
      }
    }
  }
bodybuilder()
    .aggregation('terms', 'field1', 'doctype', {
        order: [{
            "maxScore": "desc"
        }]
    }, (a) => {
        return a.aggregation('top_hits', null, 'topHits', {
            "size": 2,
            "_source": ["field1"],
            "track_scores": true,
        }).aggregation("max", null, "maxScore", {
            script: "_score"
        })
    })
    .build()

==>

{
  "aggs": {
    "doctype": {
      "terms": {
        "field": "field1",
        "order": [
          {
            "maxScore": "desc"
          }
        ]
      },
      "aggs": {
        "topHits": {
          "top_hits": {
            "size": 2,
            "_source": [
              "field1"
            ],
            "track_scores": true
          }
        },
        "maxScore": {
          "max": {
            "script": "_score"
          }
        }
      }
    }
  }
}