amanshri93
amanshri93

Reputation: 73

How do i find the the total items stored in an array variable in Elasticsearch

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??

Upvotes: 0

Views: 716

Answers (2)

Andrei Stefan
Andrei Stefan

Reputation: 52368

This should be the correct query:

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

Upvotes: 0

Manolis
Manolis

Reputation: 738

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

Upvotes: 1

Related Questions