弹性搜索查找单个查询中两个字段的总和 [英] Elastic search find sum of two fields in single query

查看:136
本文介绍了弹性搜索查找单个查询中两个字段的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在单个查询中查找两个字段的总和。我设法找到一个字段的总和,但面临的困难是在一个查询中添加两个侵略。

I have a requirement of find sum of two fields in a single query. I have managed to find the sum of one field, but facing difficulty to add two aggression in a single query.

我的json看起来像下面的方式

My json look like the following way

{
    "_index": "outboxprov1",
    "_type": "message",
    "_id": "JXpDpNefSkKO-Hij3T9m4w",
    "_score": 1,
    "_source": {
       "team_id": "1fa86701af05a863f59dd0f4b6546b32",
       "created_user": "1a9d05586a8dc3f29b4c8147997391f9",
       "created_ip": "192.168.2.245",
       "folder": 1,
       "post_count": 5,
       "sent": 3,
       "failed": 2,
       "status": 6,
       "message_date": "2014-08-20T14:30Z",
       "created_date": "2014-06-27T04:34:30.885Z"
    }
 } 

我的搜索查询

{
   "query": {
      "filtered": {
         "query": {
            "match": {
               "team_id": {
                  "query": "1fa86701af05a863f59dd0f4b6546b32"
               }
            }
         },
         "filter": {
            "and": [
               {
                  "term": {
                     "status": "6"
                  }
               }
            ]
         }
      }
   },
   "aggs": {
      "intraday_return": {
         "sum": {
            "field": "sent"
         }
      }
   },
    "aggs": {
      "intraday_return": {
         "sum": {
            "field": "failed"
         }
      }
   }
}

如何将两个侵略放在一个查询中?请帮我解决这个问题。谢谢

How to put two aggression in one query? Please help me to solve this issue. Thank you

推荐答案

可以有多个子聚合

{
   "query": {
      "filtered": {
         "query": {
            "match": {
               "team_id": {
                  "query": "1fa86701af05a863f59dd0f4b6546b32"
               }
            }
         },
         "filter": {
            "and": [
               {
                  "term": {
                     "status": "6"
                  }
               }
            ]
         }
      }
   },
   "aggs": {
      "intraday_return_sent": {
         "sum": {
            "field": "sent"
         }
      },
      "intraday_return_failed": {
         "sum": {
            "field": "failed"
         }
      }
   }
}

这篇关于弹性搜索查找单个查询中两个字段的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆