我有一份文件,请说
{
"id" : "someID",
"name" : "someName",
"states" : ["A", "B", "C"] }
现在我需要找到每个文档中状态数的总和, 我使用了以下查询,
{
"aggs" : {
"value" : {
"sum" : {
"_script" : {
"script" : "doc[\"states\"].values.length"
}
}
}
}
}'
但我收到以下错误,
{
"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
}
有人可以提出正确的查询吗?
答案 0 :(得分:1)
由于您需要“状态”字段中的元素数量,请尝试以下操作:
{
"query": {
"match": {
"id": "someID"
}
},
"aggs":{
"value" : {
"value_count" : { "field" : "states"}
}
}
}
您可以在此处参阅以获取更多信息:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html
答案 1 :(得分:0)
这应该是正确的查询:
{
"aggs": {
"value": {
"sum": {"script": "doc['states'].size()"}
}
}
}