0

I have a document let say

{ 
    "id" : "someID", 
    "name" : "someName", 
    "states" : ["A", "B", "C"]  }

Now i need to find the sum of the number of states in each document, I used the following query,

 {
          "aggs" : {
                  "value" : {
                                  "sum" : {
                                          "_script" : {
                                                  "script" : "doc[\"states\"].values.length"
                                          }
                                  }
                  }
          }
  }' 

But i am getting the following error,

{
  "error" : "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[NeZRO8O8Rn2n7hH66anZTw][w3-commsvc][0]: SearchParseException[[w3-commsvc][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n\t\"aggs\" : {\n\t\t\"value\" : {\n\t\t\t\t\"sum\" : { \n\t\t\t\t\t\"_script\" : {\n\t\t\t\t\t\t\"script\" : \"doc[\\\"states\\\"].values.length\"\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t}\n\t}\n}]]]; nested: SearchParseException[[w3-commsvc][0]: from[-1],size[-1]: Parse Failure [Unexpected token START_OBJECT in [value].]]; }{[NeZRO8O8Rn2n7hH66anZTw][w3-commsvc][1]: SearchParseException[[w3-commsvc][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n\t\"aggs\" : {\n\t\t\"value\" : {\n\t\t\t\t\"sum\" : { \n\t\t\t\t\t\"_script\" : {\n\t\t\t\t\t\t\"script\" : \"doc[\\\"states\\\"].values.length\"\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t}\n\t}\n}]]]; nested: SearchParseException[[w3-commsvc][1]: from[-1],size[-1]: Parse Failure [Unexpected token START_OBJECT in [value].]]; }{[NeZRO8O8Rn2n7hH66anZTw][w3-commsvc][2]: SearchParseException[[w3-commsvc][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n\t\"aggs\" : {\n\t\t\"value\" : {\n\t\t\t\t\"sum\" : { \n\t\t\t\t\t\"_script\" : {\n\t\t\t\t\t\t\"script\" : \"doc[\\\"states\\\"].values.length\"\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t}\n\t}\n}]]]; nested: SearchParseException[[w3-commsvc][2]: from[-1],size[-1]: Parse Failure [Unexpected token START_OBJECT in [value].]]; }{[NeZRO8O8Rn2n7hH66anZTw][w3-commsvc][3]: SearchParseException[[w3-commsvc][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n\t\"aggs\" : {\n\t\t\"value\" : {\n\t\t\t\t\"sum\" : { \n\t\t\t\t\t\"_script\" : {\n\t\t\t\t\t\t\"script\" : \"doc[\\\"states\\\"].values.length\"\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t}\n\t}\n}]]]; nested: SearchParseException[[w3-commsvc][3]: from[-1],size[-1]: Parse Failure [Unexpected token START_OBJECT in [value].]]; }{[NeZRO8O8Rn2n7hH66anZTw][w3-commsvc][4]: SearchParseException[[w3-commsvc][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n\t\"aggs\" : {\n\t\t\"value\" : {\n\t\t\t\t\"sum\" : { \n\t\t\t\t\t\"_script\" : {\n\t\t\t\t\t\t\"script\" : \"doc[\\\"states\\\"].values.length\"\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t}\n\t}\n}]]]; nested: SearchParseException[[w3-commsvc][4]: from[-1],size[-1]: Parse Failure [Unexpected token START_OBJECT in [value].]]; }]",
  "status" : 400
}

Can someone suggest the correct query??

2 Answers 2

1

Since you need the number of elements in the "states" field then try this:

{
     "query": {
        "match": {
           "id": "someID"
        }
    },
    "aggs":{
        "value" :  {  
            "value_count" : { "field" : "states"} 
        } 
    }
}

you can refer here for more information: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html

Sign up to request clarification or add additional context in comments.

2 Comments

Will this work if states is another JSON object like, states : [ { "A" : "something" }, { "B" : "something" } ]
It will, just be careful of the mapping that you use on this field. If the analyzer broke down your json into tokens, then the results could be unexpected.
0

This should be the correct query:

{
  "aggs": {
    "value": {
      "sum": {"script": "doc['states'].size()"}
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.