ElasticSearch 中两个日期之间的字段总和

Sum of fields between two dates in ElasticSearch

我是 ElasticSearch 的新手

我想计算 2020 年 8 月 27 日和 2020 年 8 月 31 日这两个日期之间的薪水总和,但我无法实现。

我在这里发布简单查询和查询结果。

我阅读了有关日期直方图的信息,但找不到具体答案,如果结果非常简单,那么为什么日期直方图要求将区间和 returns 数据放入存储桶中。

员工索引中的文件是

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "employees",
                "_type": "_doc",
                "_id": "wO8AMXQBDHla7ClA8iDV",
                "_score": 1.0,
                "_source": {
                    "FirstName": "JOYE",
                    "LastName": "WIATR",
                    "Designation": "CEO",
                    "Salary": 144000,
                    "DateOfJoining": "25/05/2009",
                    "Address": "9068 SW. Grove St. Waynesboro, PA 17268",
                    "Gender": "Female",
                    "Age": 58,
                    "MaritalStatus": "Unmarried",
                    "Interests": "Renting movies,Scuba Diving,Snowboarding,Butterfly Watching,Dumpster Diving,Badminton,Church/church activities"
                }
            },
            {
                "_index": "employees",
                "_type": "_doc",
                "_id": "wu8CMXQBDHla7ClAwCDT",
                "_score": 1.0,
                "_source": {
                    "FirstName": "Ajay",
                    "LastName": "Jaiswal",
                    "Designation": "CEO",
                    "Salary": 44000,
                    "DateOfJoining": "28/08/2020",
                    "Address": "Hyderabad",
                    "Gender": "Male",
                    "Age": 29,
                    "MaritalStatus": "Unmarried",
                    "Interests": "Watching movies , learing from scratch"
                }
            },
            {
                "_index": "employees",
                "_type": "_doc",
                "_id": "w-8KMXQBDHla7ClAICC9",
                "_score": 1.0,
                "_source": {
                    "FirstName": "MR.X",
                    "LastName": "Jaiswal",
                    "Designation": "CEO",
                    "Salary": 56000,
                    "DateOfJoining": "30/08/2020",
                    "Address": "Hyderabad",
                    "Gender": "Male",
                    "Age": 39,
                    "MaritalStatus": "Married",
                    "Interests": "Watching movies,Watching war movies"
                }
            }
        ]
    }
}

这是我的查询

 {
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "DateOfJoining": {
              "gte": "27/08/2020",
              "lte": "31/08/2020"
            }
          }
        }
      ]
    }
  }
}

这是查询的结果

{
    "took": 11,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "employees",
                "_type": "_doc",
                "_id": "wu8CMXQBDHla7ClAwCDT",
                "_score": 1.0,
                "_source": {
                    "FirstName": "Ajay",
                    "LastName": "Jaiswal",
                    "Designation": "CEO",
                    "Salary": 44000,
                    "DateOfJoining": "28/08/2020",
                    "Address": "Hyderabad",
                    "Gender": "Male",
                    "Age": 29,
                    "MaritalStatus": "Unmarried",
                    "Interests": "Watching movies , learing from scratch"
                }
            },
            {
                "_index": "employees",
                "_type": "_doc",
                "_id": "w-8KMXQBDHla7ClAICC9",
                "_score": 1.0,
                "_source": {
                    "FirstName": "MR.X",
                    "LastName": "Jaiswal",
                    "Designation": "CEO",
                    "Salary": 56000,
                    "DateOfJoining": "30/08/2020",
                    "Address": "Hyderabad",
                    "Gender": "Male",
                    "Age": 39,
                    "MaritalStatus": "Married",
                    "Interests": "Watching movies,Watching war movies"
                }
            }
        ]
    }
}

我真的做不到

I want to sum of salary between two dates 27/08/2020 and 31/08/2020 which I'm not able to achieve.

尝试以下查询:

{
  "size": 0,
  "query": {
    "range": {
      "DateOfJoining": {
        "gte": "27/08/2020",
        "lte": "31/08/2020"
      }
    }
  },
  "aggs": {
    "sum_of_salary": {
      "sum": {
        "field": "Salary"
      }
    }
  }
}

I read about date-histogram but could not find a specific answer and If the outcome is very simple then why date-histogram is asking to put in interval as well as it returns data in the bucket.

在 Date-histograms 中,数据根据某个时间段进行分桶。

例如,如果您想获得每月支付给员工的薪水总和,那么 date-histogram 会有所帮助。