All files / src SearchResult.js

94.55% Statements 52/55
77.78% Branches 28/36
100% Functions 11/11
94.55% Lines 52/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193                                    19x                                                                     19x 5x     50x   50x         14x           1x     9x 9x   9x     9x 3x 1x 1x       2x 2x     2x 2x     2x   2x       6x 2x         2x       2x   2x 2x     2x   2x       4x 3x     3x   3x 1x   1x     2x   2x     1x           1x 6x           1x 9x           1x 1x           1x 1x           1x 1x           1x 1x           1x 1x     1x  
/**
 * @param {Collection} collection
 * @param {int} total
 * @param {Document[]} documents
 * @param {object} aggregations
 * @param {object} options
 * @param {object} filters
 * @param {SearchResult} previous
 * @property {Collection} collection
 * @property {number} total
 * @property {Document[]} documents
 * @property {object} aggregations
 * @property {object} options
 * @property {object} filters
 * @property {number} fetchedDocument
 * @constructor
 */
function SearchResult (collection, total, documents, aggregations, options, filters, previous) {
  Object.defineProperties(this, {
    // read-only properties
    collection: {
      value: collection,
      enumerable: true
    },
    total: {
      value: total,
      enumerable: true
    },
    documents: {
      value: documents,
      enumerable: true
    },
    aggregations: {
      value: aggregations || {},
      enumerable: true
    },
    options: {
      value: options || {},
      enumerable: true
    },
    filters: {
      value: filters || {},
      enumerable: true
    },
    // writable properties
    fetchedDocument: {
      value: previous instanceof SearchResult ? documents.length + previous.fetchedDocument : documents.length,
      enumerable: true,
      writable: true
    }
  });
 
  // promisifying
  if (this.collection.kuzzle.bluebird) {
    return this.collection.kuzzle.bluebird.promisifyAll(this, {
      suffix: 'Promise',
      filter: function (name, func, target, passes) {
        var whitelist = ['fetchNext'];
 
        return passes && whitelist.indexOf(name) !== -1;
      }
    });
  }
 
  return this;
}
 
/**
 * @param {function} cb
 */
SearchResult.prototype.fetchNext = function (cb) {
  var
    filters,
    options = Object.assign({}, this.options),
    self = this;
  
  options.previous = this;
 
  // retrieve next results with scroll if original search use it
  if (options.scrollId) {
    if (this.fetchedDocument >= this.getTotal()) {
      cb(null, null);
      return;
    }
 
    // from and size parameters are not valid for a scroll action
    Eif (typeof options.from !== 'undefined') {
      delete options.from;
    }
 
    Eif (options.size) {
      delete options.size;
    }
 
    this.collection.scroll(options.scrollId, options, this.filters || {}, cb);
 
    return;
  }
 
  // retrieve next results using ES's search_after
  if (options.size && this.filters.sort) {
    Iif (this.fetchedDocument >= this.getTotal()) {
      cb(null, null);
      return;
    }
 
    Iif (options.from) {
      delete options.from;
    }
 
    filters = Object.assign(this.filters, {search_after: []});
 
    filters.sort.forEach(function (sortRule) {
      filters.search_after.push(self.documents[self.documents.length - 1].content[Object.keys(sortRule)[0]]);
    });
 
    this.collection.search(filters, options, cb);
 
    return;
  }
 
  // retrieve next results with from/size if original search use it
  if (options.from !== undefined && options.size !== undefined) {
    filters = Object.assign({}, this.filters);
 
    // check if we need to do next request to fetch all matching documents
    options.from += options.size;
 
    if (options.from >= this.getTotal()) {
      cb(null, null);
 
      return;
    }
 
    this.collection.search(filters, options, cb);
 
    return;
  }
 
  cb(new Error('Unable to retrieve next results from search: missing scrollId or from/size params'));
};
 
/**
 * @returns {Document[]}
 */
SearchResult.prototype.getDocuments = function () {
  return this.documents;
};
 
/**
 * @returns {number}
 */
SearchResult.prototype.getTotal = function () {
  return this.total;
};
 
/**
 * @returns {object}
 */
SearchResult.prototype.getAggregations = function () {
  return this.aggregations;
};
 
/**
 * @returns {Object}
 */
SearchResult.prototype.getOptions = function() {
  return this.options;
};
 
/**
 * @returns {object}
 */
SearchResult.prototype.getFilters = function() {
  return this.filters;
};
 
/**
 * @returns {object}
 */
SearchResult.prototype.getCollection = function () {
  return this.collection;
};
 
/**
 * @returns {number}
 */
SearchResult.prototype.getFetchedDocument = function () {
  return this.fetchedDocument;
};
 
module.exports = SearchResult;