# Find age range according to Birthdate

**URL:** https://discuss.elastic.co/t/find-age-range-according-to-birthdate/177691
**Category:** Elasticsearch
**Created:** [April 20, 2019, 8:05am UTC](https://discuss.elastic.co/t/find-age-range-according-to-birthdate/177691 "2019-04-20T08:05:05Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![chan\_1di](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chan_1di/32/31001_2.png) [@chan\_1di](https://discuss.elastic.co/u/chan_1di)
#### Post date: [April 20, 2019, 8:05am UTC](https://discuss.elastic.co/t/find-age-range-according-to-birthdate/177691/1 "2019-04-20T08:05:05Z")

</div>

My date of birth is storing as this "1995-08-10 00:00:00.314"

ANd now I want to write a search query so that it can return the data according to the age of the person. My condition is I want to return data within 21-31 age, I tried below query but it didn't work

POST /datewise/testEnvo/\_search  
{  
"query": {  
"range": {  
"user.dateOfBirth": {  
"gte": "2016-01-01 00:00:00",  
"lte": "2026-01-01 00:00:00"  
}  
}  
}  
}

I'm a little confused in here please help me out.

Thanks

---

<div class="post-metadata">

### Author: ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)
#### Post date: [April 22, 2019, 3:25pm UTC](https://discuss.elastic.co/t/find-age-range-according-to-birthdate/177691/2 "2019-04-22T15:25:46Z")

</div>

In your case I think what it would work is something like:

```auto
GET discuss/_search
{
  "query": {
    "range": {
      "user.dateOfBirth": {
        "gte": "now-31y/d",
        "lte": "now-21y/d"
      }
    }
  }
}

```

Note that the user birthdate needs to be lower than 21 years from now and bigger 31 years from now.

---

<div class="post-metadata">

### Author: ![chan\_1di](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chan_1di/32/31001_2.png) [@chan\_1di](https://discuss.elastic.co/u/chan_1di)
#### Post date: [April 22, 2019, 5:13pm UTC](https://discuss.elastic.co/t/find-age-range-according-to-birthdate/177691/3 "2019-04-22T17:13:15Z")

</div>

Hi @Ignacio_Vera thanks for your reply , but that didn't work

 ![Screenshot%20from%202019-04-22%2022-35-24](https://us1.discourse-cdn.com/elastic/original/3X/a/2/a2c7f66c71908c83defade72bea0c43e386243cc.png)

I have attached an image can you look at the "user.dateOfBirth" i want to bring the result if my age is in between 21-30

---

<div class="post-metadata">

### Author: ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)
#### Post date: [April 22, 2019, 5:29pm UTC](https://discuss.elastic.co/t/find-age-range-according-to-birthdate/177691/4 "2019-04-22T17:29:19Z")

</div>

I need more information about what you are doing. Can you give an example of what is failing for you?

---

<div class="post-metadata">

### Author: ![chan\_1di](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chan_1di/32/31001_2.png) [@chan\_1di](https://discuss.elastic.co/u/chan_1di)
#### Post date: [April 22, 2019, 5:40pm UTC](https://discuss.elastic.co/t/find-age-range-according-to-birthdate/177691/5 "2019-04-22T17:40:42Z")

</div>

I will have multiple data just like in the image I attached. Those data will have "user.dateOfBirth" field in each data. Now I want to find the age according to that "user.dateOfBirth" field and return all the result which are in between age 21-30. I know it would be a lot easier if I had a number instead of that date in "user.dateOfBirth" field.

I tried your approach but it didn't work, and I also tried times range query but that also didn't work maybe there's a condition that I need to apply to query, but I don't know what is it.

POST /datewise/testEnvo/\_search  
{  
"query": {  
"range": {  
"user.dateOfBirth": {  
"gt": "now-21y/d",  
"lt": "now-30y/d"  
}  
}  
}  
}

If you can guide me through this that would be a big help 🙂

---

<div class="post-metadata">

### Author: ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)
#### Post date: [April 22, 2019, 5:47pm UTC](https://discuss.elastic.co/t/find-age-range-according-to-birthdate/177691/6 "2019-04-22T17:47:50Z")

</div>

Having a number would not be very useful as it changes over time 🙂

The query I sent to you works for me so I need more information why it does not work for you. If you can reproduce what it is not working for you, it would be very helpful. In the example you showed before the query only hits the record with date of birth in 1995 but of course it won't match the record in 2019.

---

<div class="post-metadata">

### Author: ![chan\_1di](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chan_1di/32/31001_2.png) [@chan\_1di](https://discuss.elastic.co/u/chan_1di)
#### Post date: [April 22, 2019, 5:52pm UTC](https://discuss.elastic.co/t/find-age-range-according-to-birthdate/177691/7 "2019-04-22T17:52:58Z")

</div>

![Screenshot%20from%202019-04-22%2023-22-11](https://us1.discourse-cdn.com/elastic/original/3X/a/1/a13f2a19957b1d94b3b1cf8fa00645f6b2c8480b.png)

Here I used your query it didn't return any data though ☹

---

<div class="post-metadata">

### Author: ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)
#### Post date: [April 22, 2019, 5:54pm UTC](https://discuss.elastic.co/t/find-age-range-according-to-birthdate/177691/8 "2019-04-22T17:54:40Z")

</div>

Can you sent the mapping you are using? Probably that give us the answer

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [April 22, 2019, 5:59pm UTC](https://discuss.elastic.co/t/find-age-range-according-to-birthdate/177691/9 "2019-04-22T17:59:12Z")

</div>

It looks like you have swapped the range limits. Copy and paste the example Ignacio provided above and show us what the result is.

---

<div class="post-metadata">

### Author: ![chan\_1di](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chan_1di/32/31001_2.png) [@chan\_1di](https://discuss.elastic.co/u/chan_1di)
#### Post date: [April 22, 2019, 6:04pm UTC](https://discuss.elastic.co/t/find-age-range-according-to-birthdate/177691/10 "2019-04-22T18:04:27Z")

</div>

![Screenshot%20from%202019-04-22%2023-33-22](https://us1.discourse-cdn.com/elastic/original/3X/a/9/a93bee2ec3ee620ffd38f9a79d5910e7bed3128c.png)

---

<div class="post-metadata">

### Author: ![chan\_1di](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chan_1di/32/31001_2.png) [@chan\_1di](https://discuss.elastic.co/u/chan_1di)
#### Post date: [April 22, 2019, 6:13pm UTC](https://discuss.elastic.co/t/find-age-range-according-to-birthdate/177691/11 "2019-04-22T18:13:42Z")

</div>

which mapping , You mean how I am storing my data ?

---

<div class="post-metadata">

### Author: ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)
#### Post date: [April 22, 2019, 6:16pm UTC](https://discuss.elastic.co/t/find-age-range-according-to-birthdate/177691/12 "2019-04-22T18:16:23Z")

</div>

Yes, basically is the output of the mapping API:

[https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html)

This is the schema that Elasticsearch is using to index the data.

so in your case it should execute the following command in kibana:

```auto
GET /datewise/_mapping

```

---

<div class="post-metadata">

### Author: ![chan\_1di](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chan_1di/32/31001_2.png) [@chan\_1di](https://discuss.elastic.co/u/chan_1di)
#### Post date: [April 22, 2019, 6:20pm UTC](https://discuss.elastic.co/t/find-age-range-according-to-birthdate/177691/13 "2019-04-22T18:20:30Z")

</div>

![Screenshot%20from%202019-04-22%2023-50-05](https://us1.discourse-cdn.com/elastic/original/3X/2/9/29992619c9bd5b8291217ca531c5dc389b512b3e.png)

Here

---

<div class="post-metadata">

### Author: ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)
#### Post date: [April 22, 2019, 6:39pm UTC](https://discuss.elastic.co/t/find-age-range-according-to-birthdate/177691/14 "2019-04-22T18:39:50Z")

</div>

You are using dynamic mapping and the data is being indexed as `text` instead of `date`. That is the reason you get nothing back from your query.

You need to declare explicitly the mapping of your data as shown here:

[https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html#\_example\_mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html#_example_mapping)

---

<div class="post-metadata">

### Author: ![chan\_1di](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chan_1di/32/31001_2.png) [@chan\_1di](https://discuss.elastic.co/u/chan_1di)
#### Post date: [April 22, 2019, 10:35pm UTC](https://discuss.elastic.co/t/find-age-range-according-to-birthdate/177691/15 "2019-04-22T22:35:38Z")

</div>

Hi @Ignacio_Vera thanks for your quick response , everything works perfectly now 😃

---

<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 20, 2019, 10:35pm UTC](https://discuss.elastic.co/t/find-age-range-according-to-birthdate/177691/16 "2019-05-20T22:35:46Z")

</div>

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