# Painless Scripting

**URL:** <https://discuss.elastic.co/t/painless-scripting/301043>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [March 30, 2022, 7:38am UTC](https://discuss.elastic.co/t/painless-scripting/301043 "2022-03-30T07:38:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![chivas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chivas/32/58800_2.png) [@chivas](https://discuss.elastic.co/u/chivas)\
**Post date:** [March 30, 2022, 7:38am UTC](https://discuss.elastic.co/t/painless-scripting/301043/1 "2022-03-30T07:38:18Z")

</div>

Hi,  
I am trying to get a count of the requests hitting my server by time range, and have come up with below query:

```auto
GET /logs-prod*/_search?pretty=true
{
  "query": {
    "bool": {
      "filter": [
        {
          "match_phrase": {
            "type": "REQ"
          }
        },
        {
          "range": {
            "timestamp": {
              "gte": "2022-03-29T01:00:00.000Z",
              "lte": "2022-03-29T15:00:00.000Z"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "txn-count": {
      "terms": {
        "script": "if (doc[\"timestamp\"].size() == 0) return '';else if (doc[\"timestamp\"].value.getHour() >= 1 && doc[\"timestamp\"].value.getHour() <= 14 ) return \"Peak Transactions\"; else return \"Off-Peak Transactions\""
      }
    }
  }
}

```

However, the result is coming as below:

```auto
  "aggregations" : {
    "txn-count" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Peak Transactions",
          "doc_count" : 164783
        }
      ]
    }
  }
}

```

i.e. everything is just being grouped into Peak Transactions, what am I doing wrong?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 30, 2022, 7:54am UTC](https://discuss.elastic.co/t/painless-scripting/301043/2 "2022-03-30T07:54:27Z")

</div>

Hey, you are filtering from 1am till 3pm in your `bool` query with a `range` filter so, with the exception of documents being indexed exactly at 3pm, all of your docs fall into Peak Transactions anyway?

---

<div class="post-metadata">

**Author:** ![chivas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chivas/32/58800_2.png) [@chivas](https://discuss.elastic.co/u/chivas)\
**Post date:** [March 30, 2022, 8:24am UTC](https://discuss.elastic.co/t/painless-scripting/301043/3 "2022-03-30T08:24:17Z")

</div>

@spinscale does not seem so, please see below:

```auto
GET /logs-prod*/_count
{
  "query": {
    "bool": {
      "filter": [
        {
          "match_phrase": {
            "type": "REQ"
          }
        },
        {
          "range": {
            "timestamp": {
              "format": "strict_date_optional_time",
              "gte": "2022-03-29T01:00:00.000Z",
              "lte": "2022-03-29T14:00:00.000Z"
            }
          }
        }
      ]
    }
  }
}
}

```

```auto
{
  "count" : 161655,
  "_shards" : {
    "total" : 211,
    "successful" : 211,
    "skipped" : 0,
    "failed" : 0
  }
}

```

Thanks.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 30, 2022, 8:48am UTC](https://discuss.elastic.co/t/painless-scripting/301043/4 "2022-03-30T08:48:38Z")

</div>

Sorry, but I fail to understand what this should prove compared to what I said above?

---

<div class="post-metadata">

**Author:** ![chivas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chivas/32/58800_2.png) [@chivas](https://discuss.elastic.co/u/chivas)\
**Post date:** [March 30, 2022, 8:57am UTC](https://discuss.elastic.co/t/painless-scripting/301043/5 "2022-03-30T08:57:30Z")

</div>

> [@chivas](#):
>
> `if (doc[\"timestamp\"].value.getHour() >= 1 && doc[\"timestamp\"].value.getHour() <= 14 ) return \"Peak Transactions\"`

@spinscale since I am using the condition,

`if (doc[\"timestamp\"].value.getHour() >= 1 && doc[\"timestamp\"].value.getHour() <= 14 ) return \"Peak Transactions\"`

in the script, shouldn't 161655 documents fall under Peak Transactions?

Thanks!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 31, 2022, 11:48am UTC](https://discuss.elastic.co/t/painless-scripting/301043/6 "2022-03-31T11:48:31Z")

</div>

can you share the full aggregation response, and also add the `show_term_doc_count_error` parameter. See [Terms aggregation | Elasticsearch Guide [8.1] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.1/search-aggregations-bucket-terms-aggregation.html#terms-agg-doc-count-error)

---

<div class="post-metadata">

**Author:** ![chivas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chivas/32/58800_2.png) [@chivas](https://discuss.elastic.co/u/chivas)\
**Post date:** [March 31, 2022, 12:24pm UTC](https://discuss.elastic.co/t/painless-scripting/301043/7 "2022-03-31T12:24:06Z")

</div>

@spinscale sure, here you go, I executed below query (hope I used the parameter correctly)

```auto
GET /logs-prod*/_search?pretty=true
{
  "query": {
    "bool": {
      "filter": [
        {
          "match_phrase": {
            "type": "REQ"
          }
        },
        {
          "range": {
            "timestamp": {
              "gte": "2022-03-29T01:00:00.000Z",
              "lte": "2022-03-29T15:00:00.000Z"
            }
          }
        }
      ]
    }
  },
   "size": 0,
  "aggs": {
    "txn-count": {
      "terms": {
        "show_term_doc_count_error": true,
        "script": "if (doc[\"timestamp\"].size() == 0) return '';else if (doc[\"timestamp\"].value.getHour() >= 1 && doc[\"timestamp\"].value.getHour() <= 14 ) return \"Peak Transactions\"; else return \"Off-Peak Transactions\""
      }
    }
  }
}

```

Output is below

```auto
{
  "took" : 222,
  "timed_out" : false,
  "_shards" : {
    "total" : 212,
    "successful" : 212,
    "skipped" : 211,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : []
  },
  "aggregations" : {
    "txn-count" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Peak Transactions",
          "doc_count" : 164783,
          "doc_count_error_upper_bound" : 0
        }
      ]
    }
  }
}

```

Thanks.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 31, 2022, 3:46pm UTC](https://discuss.elastic.co/t/painless-scripting/301043/8 "2022-03-31T15:46:35Z")

</div>

Can you enable tracking of total hits [Search your data | Elasticsearch Guide [8.1] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.1/search-your-data.html#track-total-hits) - not yet sure about that discrepancy... numbers are supposed to be exact as only one shard seems to be involved

---

<div class="post-metadata">

**Author:** ![chivas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chivas/32/58800_2.png) [@chivas](https://discuss.elastic.co/u/chivas)\
**Post date:** [March 31, 2022, 3:51pm UTC](https://discuss.elastic.co/t/painless-scripting/301043/9 "2022-03-31T15:51:00Z")

</div>

@spinscale

```auto
GET /logs-prod*/_search?pretty=true
{
  "track_total_hits": true,
  "query": {
    "bool": {
      "filter": [
        {
          "match_phrase": {
            "type": "REQ"
          }
        },
        {
          "range": {
            "timestamp": {
              "gte": "2022-03-29T01:00:00.000Z",
              "lte": "2022-03-29T15:00:00.000Z"
            }
          }
        }
      ]
    }
  },
   "size": 0,
  "aggs": {
    "txn-count": {
      "terms": {
        "show_term_doc_count_error": true,
        "script": "if (doc[\"timestamp\"].size() == 0) return '';else if (doc[\"timestamp\"].value.getHour() >= 1 && doc[\"timestamp\"].value.getHour() <= 14 ) return \"Peak Transactions\"; else return \"Off-Peak Transactions\""
      }
    }
  }
}

```

Output

```auto
{
  "took" : 14,
  "timed_out" : false,
  "_shards" : {
    "total" : 212,
    "successful" : 212,
    "skipped" : 211,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 164783,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : []
  },
  "aggregations" : {
    "txn-count" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Peak Transactions",
          "doc_count" : 164783,
          "doc_count_error_upper_bound" : 0
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 4, 2022, 9:51am UTC](https://discuss.elastic.co/t/painless-scripting/301043/10 "2022-04-04T09:51:58Z")

</div>

So, the query returns `164783` hits as well as the aggregation. That means that part looks good to me.

Above you stated, that `161655` are returned. Why isn't that the case here, when you are using the same query against the same indices?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 2, 2022, 9:52am UTC](https://discuss.elastic.co/t/painless-scripting/301043/11 "2022-05-02T09:52:07Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
