最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to take the current week's data (from the beginning of the week) from elasticsearch query irrespective of present we

programmeradmin1浏览0评论

The following is query I am using to fetch data for the last three months (starting of the month). How to change this to start of the current week?

{
    "size": 10,
    "timeout": "1000s",
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "filter_grouper": "ABC"
                    }
                },
                {
                    "match": {
                        "state": "High"
                    }
                },
                {
                    "range": {
                        "end_date": {
                            "gte": "now-3M/M",
                            "lte": "now"
                        }
                    }
                }
            ]
        }
    },
    "_source": [
        "number",
        "barrel"
    ]
}

I tried using

{
    "range": {
         "end_date": {
              "gte": "now/yyyy-W/W",
              "lte": "now"
          }
     }
}

But it throws the error -

{
                "type": "parse_exception",
                "reason": "operator not supported for date math [/yyyy-W/W]"
            }

same goes for now-1W/W

The following is query I am using to fetch data for the last three months (starting of the month). How to change this to start of the current week?

{
    "size": 10,
    "timeout": "1000s",
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "filter_grouper": "ABC"
                    }
                },
                {
                    "match": {
                        "state": "High"
                    }
                },
                {
                    "range": {
                        "end_date": {
                            "gte": "now-3M/M",
                            "lte": "now"
                        }
                    }
                }
            ]
        }
    },
    "_source": [
        "number",
        "barrel"
    ]
}

I tried using

{
    "range": {
         "end_date": {
              "gte": "now/yyyy-W/W",
              "lte": "now"
          }
     }
}

But it throws the error -

{
                "type": "parse_exception",
                "reason": "operator not supported for date math [/yyyy-W/W]"
            }

same goes for now-1W/W

Share Improve this question asked Nov 18, 2024 at 18:31 Puneeth CPuneeth C 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can use below query to get current week data. It will give the data from Monday (00:00:00.000 hour) to Sunday (23:59:59.999 hour).

{
  "size": 10,
  "timeout": "1000s",
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "filter_grouper": "ABC"
          }
        },
        {
          "match": {
            "state": "High"
          }
        },
        {
          "range": {
            "end_date": {
              "gte": "now/w",
              "lte": "now/w"
            }
          }
        }
      ]
    }
  },
  "_source": [
    "number",
    "barrel"
  ]
}

If you want to start week from Sunday to Saturday then you can change condition as below:

{
  "range": {
    "end_date": {
      "gte": "now/w-1d",
      "lte": "now/w-1d"
    }
  }
}

You can add ?explain=true in your search request in Kibana to find out from what date to what date it is querying and this will return millisecond with response so you can convert millisecond to date online and validate it. Below is sample example.

POST test/_search?explain=true
{
  "sort": [
    {
      "timestamp": {
        "order": "asc"
      }
    }
  ], 
  "query": {
    "range": {
      "timestamp": {
        "gte": "now/w",
        "lte":"now/w"
      }
    }
  }
}

Response:

{
  "_shard": "[test][0]",
  "_node": "Uy1khygkQweGl25lvrzmvg",
  "_index": "test",
  "_id": "12",
  "_score": null,
  "_source": {
    "user_id": "101",
    "timestamp": "2024-11-18T12:10:30Z",
    "priority": "1"
  },
  "sort": [
    1731931830000
  ],
  "_explanation": {
    "value": 1,
    "description": "timestamp:[1731888000000 TO 1732492799999]",
    "details": []
  }
}

Here, 1731888000000 millisecond means Mon Nov 18 2024 00:00:00.000 date and 1732492799999 means Sun Nov 24 2024 23:59:59.999. hope this will help you.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论