最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far