Aditya Irri
Aditya Irri

Reputation: 55

Getting error "request [/_search] contains unrecognized parameter: [query]'" using ElasticSearch in NodeJS API

I've been using NodeJS and ElasticSearch in my application and writing APIs to post and get data.

This is my reportsApi.js

var elasticsearch = require('elasticsearch');
var elasticClient = new elasticsearch.Client({
    host: 'http://xx.xx.xx.xx:xxxx'
})


module.exports = function() {

    global.app.post('/api/getIssuesReport', async(req, res) => {
        var fromDate = req.body.fromDate;
        var toDate = req.body.toDate;
        var issueReason = req.body.issueReason;
        try {
          var query={
            "query": {
                "bool": {
                    "must": [{
                        "range": {
                            "offlineEpoch": {
                                "gte": fromDate,
                                "lte": toDate,
                                "boost": 2
                            }
                        }
                    }, {
                        "match": {
                            "reason": issueReason
                        }
                    }]
                }
            },
            "sort": [
                {
                  "@timestamp": {
                    "order": "asc"
                  }
                }
              ]
        };
        var response = await elasticClient.search(query);
        if(response){
            console.log(response);
          }else{
            console.log("No records found");
          }
        } catch (error) {
          res.send(error);
        }
     });



}; 

In this I'm getting data from angularJs and I need to fetch records which epoch time is between the range and also the Reason should match. The same query is working fine in Kibana DevTools.

But I'm getting error at res.send(error) "request [/_search] contains unrecognized parameter: [query]'". Where am I doing wrong?

Upvotes: 2

Views: 1785

Answers (1)

Val
Val

Reputation: 217304

You need to send the query inside the body parameter:

   var query={
      "body": {                            <---- add this
        "query": {
            "bool": {
                "must": [{
                    "range": {
                        "offlineEpoch": {
                            "gte": fromDate,
                            "lte": toDate,
                            "boost": 2
                        }
                    }
                }, {
                    "match": {
                        "reason": issueReason
                    }
                }]
            }
        },
        "sort": [
            {
              "@timestamp": {
                "order": "asc"
              }
            }
          ]
      }
    };

Upvotes: 4

Related Questions