@@ -72,7 +72,7 @@ KuzzleDataCollection.prototype.advancedSearch = function (filters, cb) {
7272 }
7373
7474 result . hits . hits . forEach ( function ( doc ) {
75- documents . push ( new KuzzleDocument ( self , doc . _id , doc . _source ) ) ;
75+ documents . push ( this . documentFactory ( doc . _id , doc ) ) ;
7676 } ) ;
7777
7878 cb ( null , { total : result . hits . total , documents : documents } ) ;
@@ -155,7 +155,7 @@ KuzzleDataCollection.prototype.createDocument = function (document, options, cb)
155155 return cb ( err ) ;
156156 }
157157
158- cb ( null , new KuzzleDocument ( self , res . _id , res . _source ) ) ;
158+ cb ( null , self . documentFactory ( res . _id , res ) ) ;
159159 } ) ;
160160 } else {
161161 this . kuzzle . query ( this . collection , 'write' , action , data , options ) ;
@@ -239,7 +239,7 @@ KuzzleDataCollection.prototype.fetchDocument = function (documentId, cb) {
239239 return cb ( err ) ;
240240 }
241241
242- cb ( null , new KuzzleDocument ( self , res . _id , res . _source ) ) ;
242+ cb ( null , self . documentFactory ( res . _id , res ) ) ;
243243 } ) ;
244244
245245 return this ;
@@ -338,7 +338,7 @@ KuzzleDataCollection.prototype.replaceDocument = function (documentId, content,
338338 return cb ( err ) ;
339339 }
340340
341- cb ( null , new KuzzleDocument ( self , res . _id , res . _source ) ) ;
341+ cb ( null , self . documentFactory ( res . _id , res ) ) ;
342342 } ) ;
343343 } else {
344344 self . kuzzle . query ( this . collection , 'write' , 'createOrUpdate' , data , options ) ;
@@ -417,7 +417,13 @@ KuzzleDataCollection.prototype.updateDocument = function (documentId, content, o
417417 * @constructor
418418 */
419419KuzzleDataCollection . prototype . documentFactory = function ( id , content ) {
420- return new KuzzleDocument ( this , id , content ) ;
420+ var document = content . _source ? new KuzzleDocument ( this , id , content . _source ) : new KuzzleDocument ( this , id , content ) ;
421+
422+ if ( content . _version ) {
423+ document . version = content . _version ;
424+ }
425+
426+ return document ;
421427} ;
422428
423429/**
0 commit comments