elasticsearch中两个日期之间的字段之和

jqjz2hbq  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(325)

我对elasticsearch很陌生
我想计算2020年8月27日和2020年8月31日这两个日期之间的工资总额,但我无法实现。
我在这里发布简单的查询和查询结果。
我读了有关日期直方图,但找不到一个具体的答案,如果结果非常简单,那么为什么日期直方图要求把间隔以及它返回的数据桶。
员工索引中的文档是

{
    "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"
                }
            }
        ]
    }
}

我真的无法实现

g6ll5ycj

g6ll5ycj1#

我想计算2020年8月27日和2020年8月31日这两个日期之间的工资总额,但我无法实现。
尝试以下查询:

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

我读了有关日期直方图,但找不到一个具体的答案,如果结果非常简单,那么为什么日期直方图要求把间隔以及它返回的数据桶。
在数据直方图中,数据是基于某个时间段的。
e、 如果你想得到每月支付给员工的工资总额,那么日期柱状图会有帮助。

相关问题