Home > Software engineering >  Date range using nodejs as backend with elastic search
Date range using nodejs as backend with elastic search

Time:12-25

I have the following query:

router.get('/date', (req, res)=>{
let query = {
    index: 'decisions',
    size: 5000,   
    from: 0,
    body:{
        query:{
            filtered:{
                filter:{
                    bool:{
                        must:[{
                            "range": {
                                "Date_Lecture": {
                                    "gt": "2019-04-04",
                                    "lt": "2019-04-06"
                                }
                            }
                        }]
                    }
                }
            }
        }
    },
}
if(req.query.decisions) query.q = `*${req.query.decisions}`
client.search(query)
.then(resp=>{
    return res.status(200).json({
        decisions: resp.hits
    })
    .catch(err=>{
        console.log(500).json({
            msg:'Error',
            err
        })
    })
})})

I want to get those records from elastic which are in this range of date, but it give me this error and say it did not know the filtered and if i remove filtered and then it says it did not know the filter and if i just use range then also it says it did not knows teh range keyword.

root_cause: [
      {
        type: 'parsing_exception',
        reason: 'unknown query [filtered]',
        line: 1,
        col: 22
      }
    ],

CodePudding user response:

The filtered query is long gone (since ES 5.0), you should just change your query to this:

body:{
    query:{
        bool:{
            filter:[{
                "range": {
                    "Date_Lecture": {
                        "gt": "2019-04-04",
                        "lt": "2019-04-06"
                    }
                }
            }]
        }
    }
},
  • Related