# Dynamically passing to query

**URL:** <https://discuss.elastic.co/t/dynamically-passing-to-query/290471>\
**Category:** Elasticsearch\
**Created:** [November 29, 2021, 4:58pm UTC](https://discuss.elastic.co/t/dynamically-passing-to-query/290471 "2021-11-29T16:58:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cool999](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@cool999](https://discuss.elastic.co/u/cool999)\
**Post date:** [November 29, 2021, 4:58pm UTC](https://discuss.elastic.co/t/dynamically-passing-to-query/290471/1 "2021-11-29T16:58:06Z")

</div>

Hi Team,

I have below similar logs.

I have created dummy index and created mapping like below,

```auto
PUT new
{
  "mappings": {
    "properties": {
      "@timestamp": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss.SSS"
      }
    }
  }
}

```

and indexed data as below,

```auto
PUT /new/_doc/1
{
  "@timestamp": "2021-11-05 08:12:14.534",
  "level": "INFO",
  "id": "1",
  "text": "website is accessed",
  "status": "clicked"
}

PUT /new/_doc/2
{
  "@timestamp": "2021-10-14 09:11:14.534",
  "level": "INFO",
  "id": "3",
  "text": "website is accessed",
  "status": "clicked"
}

PUT /new/_doc/3
{
  "@timestamp": "2021-09-09 02:08:20.534",
  "level": "INFO",
  "id": "4",
  "text": "website is accessed",
  "status": "clicked"
}

```

I am able to fetch the total counts using below `request query`,

```auto
GET new/_search
{
  "aggs": {},
  "size": 0,
  "fields": [],
  "query": {
    "bool": {
      "must": [],
      "filter": [
        {
          "bool": {
            "should": [
              {
                "match_phrase": {
                  "text": "website is accessed"
                }
              }
            ],
            "minimum_should_match": 1
          }
        },
        {
          "range": {
            "@timestamp": {
              "gte": "2021-10-01",
              "lte": "2021-10-30"
            }
          }
        }
      ],
      "should": [],
      "must_not": []
    }
  }
}

```

Getting `response` as below,

```auto
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : []
  }
}

```

As you see, i need to `hardcode` the `date` to fetch the value for a particular `month` i.e to fetch the same information for `sept` month, i need to modify the date time range as below in curl request ,

```auto
"range": {
  "@timestamp": {
    "gte": "2021-09-01",
    "lte": "2021-09-30"
    }
    }

```

How can i pass `year` and `month` dynamically (i.e without actually hardcoding it request itself ) to the curl request which will fetch the information for that particular month, year?

Thanks,

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [November 30, 2021, 9:15am UTC](https://discuss.elastic.co/t/dynamically-passing-to-query/290471/2 "2021-11-30T09:15:38Z")

</div>

Not via Dev Tools, no.

You could probably do it via the shell and `curl` and `date`.

---

<div class="post-metadata">

**Author:** ![cool999](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@cool999](https://discuss.elastic.co/u/cool999)\
**Post date:** [November 30, 2021, 9:41am UTC](https://discuss.elastic.co/t/dynamically-passing-to-query/290471/3 "2021-11-30T09:41:59Z")

</div>

Hi @warkolm,

Thanks for reply.

Yes i am asking how to do achieve this from curl call?

Above `dev-tools` example was just to create things quickly.

Below is the curl request for above call.

```auto
curl -u elastic:xxx -XGET "http://10.10.10.10:9200/new/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "aggs": {},
  "size": 0,
  "fields": [],
  "query": {
    "bool": {
      "must": [],
      "filter": [
        {
          "bool": {
            "should": [
              {
                "match_phrase": {
                  "text": "website is accessed"
                }
              }
            ],
            "minimum_should_match": 1
          }
        },
        {
          "range": {
            "@timestamp": {
              "gte": "2021-10-01",
              "lte": "2021-10-30"
            }
          }
        }
      ],
      "should": [],
      "must_not": []
    }
  }
}'

```

Thanks,

---

<div class="post-metadata">

**Author:** ![cool999](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@cool999](https://discuss.elastic.co/u/cool999)\
**Post date:** [December 1, 2021, 4:29pm UTC](https://discuss.elastic.co/t/dynamically-passing-to-query/290471/4 "2021-12-01T16:29:52Z")

</div>

I am able to get the results for `last month` (Nov) or `last 2 months` (Oct) and so on using below,

last month - `Nov` -

```auto
"gte": "now-M",
"lt": "now/M"

```

2 months - `Oct`

```auto
"gte": "now-2M/M",
"lte": "now-2M/M"

```

But is there way to provide desired `year` and `month` to retrieve results?

---

<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:** [December 29, 2021, 4:30pm UTC](https://discuss.elastic.co/t/dynamically-passing-to-query/290471/5 "2021-12-29T16:30:37Z")

</div>

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