title: Elasticsearch malformed query, expected [END_OBJECT] but found [FIELD_NAME]
body: <p>I'm using the elasticsearch client for doing some search query using constant_score and aggregations but it's throwing the exception:</p>
  
  <p><code>[parsing_exception] [constant_score] malformed query, expected [END_OBJECT] but found [FIELD_NAME]</code></p>
  
  <p>This is where I'm creating the query and calling the search function:</p>
  
  <pre><code>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 ===&gt; ',query);
      this.client.search(query, cb);
  }
  </code></pre>
  
  <p>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.</p>