1

I'm trying to write a query that aggregates on document type. The thing is, the document type can be obtained from the document id - it's 4-5 first characters of the id, like BPR::fawL5CcPE72Wf3m93hUg2.
Here's the query I put together:

{
    "query": {
        "match_all": { }
    },
    "aggregations": {
        "by_document_type": {
            "script": {
                "script": "doc['_id'].value.substring(0, 3)",
                "order": { 
                    "_count": "desc" 
                }
            }
        }
    }
}

but it says Parse Failure [Could not find aggregator type [script] in [by_document_type]]];.

What did I do wrong?

Val
  • 207,596
  • 13
  • 358
  • 360
chester89
  • 8,328
  • 17
  • 68
  • 113

1 Answers1

2

You simply need to use a terms aggregation and feed your script in it:

{
  "query": {
    "match_all": {}
  },
  "aggregations": {
    "by_document_type": {
      "terms": {
        "script": "doc['_id'].value.substring(0, 3)",
        "order": {
          "_count": "desc"
        }
      }
    }
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • it gives me 'Parse Failure [Unknown key for a START_OBJECT in [by_document_type]' – chester89 Sep 03 '15 at 09:00
  • Whoops, my bad, you had one too many "script". I've removed it, please check again. So basically, just replace your first `script` by `terms` and you're fine. – Val Sep 03 '15 at 09:02