1

I am using the client object of elasticsearch.

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
    host:"https://********",
    log: 'trace',
});


"hits": [
        {
            "_index": "abc",
            "_type": "_doc",
            "_id": "39KueHEBnbKK-Fpwq9wG",
            "_score": 1.0,
            "_source": {
                "videoId": "EV-IOHABXh-qOCdiWVbW",
               "createddate": "2020-04-14 18:04:05"


            }
        },
        {
            "_index": "abc",
            "_type": "_doc",
            "_id": "29KueHEBnbKK-Fpwq9wG",
            "_score": 1.0,
            "_source": {
                "videoId": "zV-IOHABXh-qOCdiWVbW",
                 "createddate": "2020-03-14 18:04:05",


            }
        }
    ]

I am trying to filter based on createddate. This is my query which I am trying to filter out the data but not working. Please help me.

Note: I need to filter data between two dates.

{
   "query": {
    "bool": {
        "filter": [
            {

                "range": {
                    "createddate": {
                        "gte": "2020-04-11 00:00:00",
                        "lte": "2020-04-16 23:59:59"
                    }
                }
            }
        ]
    }
}

}

2
  • Field name in index and query is different .It is createdDate in index and createddate in query. Note "d" in date. Is it typo in question or also in actual query Commented Apr 14, 2020 at 17:49
  • My bad @jaspreetchahal. Thanks for the it. Actually I want to filter between two dates that are requested by the frontend. Commented Apr 14, 2020 at 17:56

1 Answer 1

1

Mapping:

{
  "mappings": {
    "properties": {
      "createddate": {
        "type": "date",
        "format":"yyyy-MM-dd HH:mm:ss"
      }
    }
  }
}

Indexed two documents

{
    "videoId": "EV-IOHABXh-qOCdiWVbW",
    "createddate":"2020-04-14 18:04:05"
 }

 {
 "videoId": "zV-IOHABXh-qOCdiWVbW",
 "createddate": "2020-03-14 18:04:05"
 }

Same Search Query which you provided:

{
   "query": {
    "bool": {
        "filter": [
            {

                "range": {
                    "createddate": {
                        "gte": "2020-04-11 00:00:00",
                        "lte": "2020-04-16 23:59:59"
                    }
                }
            }
        ]
    }
}
}

Result:

"hits": [
         {
            "_index": "test",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.0,
            "_source": {
               "videoId": "EV-IOHABXh-qOCdiWVbW",
               "createddate": "2020-04-14 18:04:05"
            }
         }
      ]

The search query filters the data between the two dates, and gives out the expected result.

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

1 Comment

@Rahul Saini glad could be of help :)

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.