2

I'm using the elasticsearch client for doing some search query using constant_score and aggregations but it's throwing the exception:

[parsing_exception] [constant_score] malformed query, expected [END_OBJECT] but found [FIELD_NAME]

This is where I'm creating the query and calling the search function:

fetchData: function fetchFn(req, res, next) {
    var model = new urlModel();
    util.log('Fetching data from ES::');
    var query = buildAggregateUrl(req.reqData);
    // if (req.reqData['domain']) {
    //     var termFilter = { "term": { "domain": req.reqData.domain } };
    //     query.constant_score.filter.bool.must.push(termFilter);
    // }
    model.search({ query }, function (er, re) {
        if (re) {
            req.sendResult = re;
        }
        next(er);
    });

    function buildAggregateUrl(opts) {
        var query = {
            constant_score: {
                filter: {
                    bool: {
                        must: [{
                            "range": {
                                "timestamp": {
                                    "gte": opts.startTime,
                                    "lte": opts.endTime
                                }
                            }
                        }]
                    }
                }
            },
            aggs: {
                success: {
                    scripted_metric: {
                        init_script: "params._agg.succ=0;",
                        map_script: "if(doc.status.value=='success'){params._agg.succ+=1}",
                        combine_script: "return params._agg.succ",
                        reduce_script: "int sum=0;for (a in params._aggs){sum+=a}return sum"
                    }
                },
                failure: {
                    scripted_metric: {
                        init_script: "params._agg.fail=0;",
                        map_script: "if(doc.status.value=='failure'){params._agg.fail+=1}",
                        combine_script: "return params._agg.fail",
                        reduce_script: "int sum=0;for (a in params._aggs){sum+=a}return sum"
                    }
                }
            }
        };
        return query;
    }
}

 And this is what the search function looks like:

    es_model.prototype.search = function (opts, cb) {
    var query = {
        index: this.esConfig.index,
        type: this.esConfig.type,
        body: {query:opts.query} 
    };
    console.log('Query ===> ',query);
    this.client.search(query, cb);
}

I can't figure out where the problem is with the syntax.I tried wrapping the constant_score key withtin query as well but it doesn't seem to work.

1 Answer 1

2

You're simply missing a query element at the top level:

{
    query: {
        constant_score: {
        filter: {
            bool: {
                must: [{
                    "range": {
                        "timestamp": {
                            "gte": opts.startTime,
                            "lte": opts.endTime
                        }
                    }
                }]
            }
        }
        }
    },
    aggs: {
        success: {
            scripted_metric: {
                init_script: "params._agg.succ=0;",
                map_script: "if(doc.status.value=='success'){params._agg.succ+=1}",
                combine_script: "return params._agg.succ",
                reduce_script: "int sum=0;for (a in params._aggs){sum+=a}return sum"
            }
        },
        failure: {
            scripted_metric: {
                init_script: "params._agg.fail=0;",
                map_script: "if(doc.status.value=='failure'){params._agg.fail+=1}",
                combine_script: "return params._agg.fail",
                reduce_script: "int sum=0;for (a in params._aggs){sum+=a}return sum"
            }
        }
    }
}

Also note that your client code should look like this:

es_model.prototype.search = function (opts, cb) { 
    var query = { 
        index: this.esConfig.index, 
        type: this.esConfig.type, 
        body: opts.query
    }; 
    this.client.search(query, cb); 
}
Sign up to request clarification or add additional context in comments.

7 Comments

I tried that already but failed with this exception:[parsing_exception] no [query] registered for [query]
Well, you probably need to show some more code as the query is correct as it is.
Well this is where I'm calling the client's search function: es_model.prototype.search = function (opts, cb) { var query = { index: this.esConfig.index, type: this.esConfig.type, body: { query: opts.query } }; this.client.search(query, cb); } And this is where I'm calling this wrapper function: model.search({ query },cb);
Still I'm getting this exception: [parsing_exception] Unknown key for a START_OBJECT in [constant_score].,
Well, update your question with the exact code you have now, otherwise we're just shooting in the dark.
|

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.