2

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?

1 Answer 1

4

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"
              }
            }
          ]
      }
    };
Sign up to request clarification or add additional context in comments.

Comments

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.