amit shukla
amit shukla

Reputation: 31

How to build dynamic query on Elasticsearch, which will execute on Node.js

When I tried to create query like

var my_query = new Object("[{ \"match\": { \"techskills.programming\":"+ programming+" } }]");

var deferred = Q.defer();
client.search({
    "index": _index,
    "type": _type,
    "body": {
        "query": {
            "bool": {
                "must": my_query
            }
        }
    }
},function (error,resp) {
    console.log("Found error is ::::: "+error + " resp::: "+JSON.stringify(resp));
    if(error) deferred.reject(error);
    deferred.resolve(resp);
});
return deferred.promise;

I am getting the following error:

[query_parsing_exception] [bool] query does not support [must]

Upvotes: 0

Views: 1340

Answers (1)

amit shukla
amit shukla

Reputation: 31

I have solved this by creating complete body tag as dynamic query string and passed this variable to the elasticsearch query. ex: var my_query = "{\"query\": {\"bool\":{\"must\": [{ \"match\": { \"techskills.programming\":\"" + programming + "\"} }]}}}"

client.search({ "index": _index, "type": _type, body:my_query },function (error,resp) { console.log("Found error is ::::: "+error + " resp::: "+JSON.stringify(resp)); if(error) deferred.reject(error); deferred.resolve(resp); });

Upvotes: 3

Related Questions