Find age range according to Birthdate

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

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

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.

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

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

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

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 :slight_smile:

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

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.

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

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

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

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

Yes, basically is the output of the mapping API:

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:

GET /datewise/_mapping

Here

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

1 Like

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

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