Rahul Saini
Rahul Saini

Reputation: 937

How to search based on createdDate and other fields in elastic search

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
     host: 'ABC',
     log: 'trace'
});

client.search({
index: 'allevents_production',
"body": {
    "query": {
        "query_string": {
            "default_field": "clientname",
            "query": 'ser'
        },
        "range" : {
            "createddate" : {
                "gte" : "now-1d/d",
                "lt" :  "now/d"
            }
        }
    }
}
})

I want to search on the multiple fields like client name and createddate. I passed these fields in the body part.

It returns an error. Please help me where I am doing wrong. Thanks!

Upvotes: 1

Views: 99

Answers (1)

Val
Val

Reputation: 217514

You need to combine both constraints using bool/must/filter:

client.search({
index: 'allevents_production',
"body": {
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "default_field": "clientname",
            "query": "ser"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "createddate": {
              "gte": "now-1d/d",
              "lt": "now/d"
            }
          }
        }
      ]
    }
  }
}
})

Upvotes: 1

Related Questions