1

I can count the hits per day that match my queried string with this code, but if the span of a whole week has no hits, then the query will return nothing - as opposed to returning 0 for each day. Is there a way I can 'default' to 0?

GET index/_search
    {
  "size": 0,
  "query": {
    "bool": {
      "must": [
          {"match_phrase": {
            "message": "Cannot login"
            }
          },
          {"range": {
            "@timestamp":{
              "gte":"2021-07-01",
              "lte":"2021-07-07"
            }
          }
        }
      ]
    }
  }, 
  "aggs": {
    "hit_count_per_day": {
      "date_histogram": {
        "field": "@timestamp",
        "calendar_interval": "day"
      }
    }
  } 
}

2 Answers 2

2

For this propose you need to add extended_bounds to your aggregation like below:

GET index/_search
    {
  "size": 0,
  "query": {
    "bool": {
      "must": [
          {"match_phrase": {
            "message": "Cannot login"
            }
          },
          {"range": {
            "@timestamp":{
              "gte":"2021-07-01",
              "lte":"2021-07-07"
            }
          }
        }
      ]
    }
  }, 
  "aggs": {
    "hit_count_per_day": {
      "date_histogram": {
        "field": "@timestamp",
        "calendar_interval": "day",
        "extended_bounds": {
          "min": "2021-07-01",
          "max": "2021-07-07"
        }
      }
    }
  } 
}

Please let me know if it did not solve your problem.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes friend! It does work, thanks for helping again
You're welcome, buddy. Glad to hear that.
0

When elasticsearch in aggregation response returns 0s means there are values not showed because of filters.

For example, I had a term filter on "region":"mexico" and it returned me:

  • "colombia": 0
  • "argentina": 0
  • "mexico": 7

Firsts two because of they are in dataset (index) but filtered.

Hopin' it could help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.