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?

1 Answer 1

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"
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

it gives me 'Parse Failure [Unknown key for a START_OBJECT in [by_document_type]'
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.