<commit_before>var elasticsearch = require('elasticsearch');

exports.query = function(config, req, res, query, sort) {
   var client = new elasticsearch.Client({
       host: config.url
   });

   // Execute ES Query
   client.search({
       index: config.index,
       type: config.type,
       body: {
           query: {
               query_string: {
                   query: query
               }
           }
       },
       size: 100,
       sort: sort
   }).then(function(resp) {
       // Parse ES response and send result back
       parseResponse(resp, res);

       // Release client resources
       client.close();
   }, function(err) {
       res.status(400).send(err.message);

       // Release client resources
       client.close();
   });
};

/**
* Parses an ES response, formats it and sends data on the HTTP response.
*/
function parseResponse(resp, res) {
   var hits = resp.hits.hits;
   var results = [];
   for (var x = 0; x < hits.length; x++) {
       results.push(hits[x]._source);
   }

   // Write response direct back to client
   res.send({
       "rows": results,
       "total": results.length
   });
}<commit_msg>Added elasticsearch service<commit_after>var elasticsearch = require('elasticsearch');

exports.query = function(config, req, res, query, handler) {
   var client = new elasticsearch.Client({
       host: config.url
   });

   // Execute ES Query
   client.search({
       index: config.index,
       type: config.type,
       body: {
           query: {
               query_string: {
                   query: query
               }
           }
       }
       // sort: sort 
   }).then(function(resp) {
       // Parse ES response and send result back
       var hits = module.exports.parseResponse(resp, res);

       if (handler) {
          handler(res, hits);
       }
       else {
          module.exports.defaultHandler(res, hits);
       }

       // Release client resources
       client.close();
   }, function(err) {
       res.status(400).send(err.message);

       // Release client resources
       client.close();
   });
};

/**
* Parses an ES response, formats it and sends data on the HTTP response.
*/
exports.parseResponse = function(resp, res) {
    return resp.hits.hits.map(function(hit) { return hit._source; });
}

exports.defaultHandler = function(res, hits) {
    res.json(hits);
}
{'fraction_non_alphanumeric': 0.12721893491124261, 'fraction_numerical': 0.00422654268808115, 'mean_word_length': 1.95875, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '91957', 'n_tokens_mistral': 686, 'n_tokens_neox': 667, 'n_words': 177}