11/** 
2-  * @param  {Collection } dataCollection  
2+  * @param  {Collection } collection  
33 * @param  {int } total 
44 * @param  {Document[] } documents 
5-  * @param  {object } [aggregations] 
6-  * @param  {object } [searchArgs] 
7-  * @param  previous 
8-  * @property  {Collection } dataCollection 
5+  * @param  {object } aggregations 
6+  * @param  {object } options 
7+  * @param  {object } filters 
8+  * @param  {SearchResult } previous 
9+  * @property  {Collection } collection 
10+  * @property  {number } total 
11+  * @property  {Document[] } documents 
12+  * @property  {object } aggregations 
13+  * @property  {object } options 
14+  * @property  {object } filters 
915 * @property  {number } fetchedDocument 
1016 * @constructor  
1117 */ 
12- function  KuzzleSearchResult   ( dataCollection ,  total ,  documents ,  aggregations ,  searchArgs ,  previous )  { 
18+ function  SearchResult   ( collection ,  total ,  documents ,  aggregations ,  options ,   filters ,  previous )  { 
1319  Object . defineProperties ( this ,  { 
1420    // read-only properties 
15-     dataCollection : { 
16-       value : dataCollection , 
21+     collection : { 
22+       value : collection , 
23+       enumerable : true 
1724    } , 
1825    total : { 
1926      value : total , 
@@ -27,24 +34,28 @@ function KuzzleSearchResult (dataCollection, total, documents, aggregations, sea
2734      value : aggregations  ||  { } , 
2835      enumerable : true 
2936    } , 
30-     searchArgs : { 
31-       value : searchArgs  ||  { } , 
37+     options : { 
38+       value : options  ||  { } , 
39+       enumerable : true 
40+     } , 
41+     filters : { 
42+       value : filters  ||  { } , 
3243      enumerable : true 
3344    } , 
3445    // writable properties 
3546    fetchedDocument : { 
36-       value : previous  instanceof  KuzzleSearchResult  ? documents . length  +  previous . fetchedDocument  : documents . length , 
47+       value : previous  instanceof  SearchResult  ? documents . length  +  previous . fetchedDocument  : documents . length , 
3748      enumerable : true , 
3849      writable : true 
3950    } 
4051  } ) ; 
4152
4253  // promisifying 
43-   if  ( this . dataCollection . kuzzle . bluebird )  { 
44-     return  this . dataCollection . kuzzle . bluebird . promisifyAll ( this ,  { 
54+   if  ( this . collection . kuzzle . bluebird )  { 
55+     return  this . collection . kuzzle . bluebird . promisifyAll ( this ,  { 
4556      suffix : 'Promise' , 
4657      filter : function  ( name ,  func ,  target ,  passes )  { 
47-         var  whitelist  =  [ 'next ' ] ; 
58+         var  whitelist  =  [ 'fetchNext ' ] ; 
4859
4960        return  passes  &&  whitelist . indexOf ( name )  !==  - 1 ; 
5061      } 
@@ -57,16 +68,16 @@ function KuzzleSearchResult (dataCollection, total, documents, aggregations, sea
5768/** 
5869 * @param  {function } cb 
5970 */ 
60- KuzzleSearchResult . prototype . next  =  function  ( cb )  { 
71+ SearchResult . prototype . fetchNext  =  function  ( cb )  { 
6172  var 
6273    filters , 
63-     options  =  Object . assign ( { } ,  this . searchArgs . options ) ; 
74+     options  =  Object . assign ( { } ,  this . options ) ; 
6475
6576  options . previous  =  this ; 
6677
6778  // retrieve next results with scroll if original search use it 
6879  if  ( options . scrollId )  { 
69-     if  ( this . fetchedDocument  >=  this . total )  { 
80+     if  ( this . fetchedDocument  >=  this . getTotal ( ) )  { 
7081      cb ( null ,  null ) ; 
7182      return ; 
7283    } 
@@ -80,30 +91,79 @@ KuzzleSearchResult.prototype.next = function (cb) {
8091      delete  options . size ; 
8192    } 
8293
83-     this . dataCollection . scroll ( options . scrollId ,  options ,  this . searchArgs . filters  ||  { } ,  cb ) ; 
94+     this . collection . scroll ( options . scrollId ,  options ,  this . filters  ||  { } ,  cb ) ; 
8495
8596    return ; 
8697  } 
8798
8899  // retrieve next results with from/size if original search use it 
89100  if  ( options . from  !==  undefined  &&  options . size  !==  undefined )  { 
90-     filters  =  Object . assign ( { } ,  this . searchArgs . filters ) ; 
101+     filters  =  Object . assign ( { } ,  this . filters ) ; 
91102
92103    // check if we need to do next request to fetch all matching documents 
93104    options . from  +=  options . size ; 
94105
95-     if  ( options . from  >=  this . total )  { 
106+     if  ( options . from  >=  this . getTotal ( ) )  { 
96107      cb ( null ,  null ) ; 
97108
98109      return ; 
99110    } 
100111
101-     this . dataCollection . search ( filters ,  options ,  cb ) ; 
112+     this . collection . search ( filters ,  options ,  cb ) ; 
102113
103114    return ; 
104115  } 
105116
106117  cb ( new  Error ( 'Unable to retrieve next results from search: missing scrollId or from/size params' ) ) ; 
107118} ; 
108119
109- module . exports  =  KuzzleSearchResult ; 
120+ /** 
121+  * @returns  {Document[] } 
122+  */ 
123+ SearchResult . prototype . getDocuments  =  function  ( )  { 
124+   return  this . documents ; 
125+ } ; 
126+ 
127+ /** 
128+  * @returns  {number } 
129+  */ 
130+ SearchResult . prototype . getTotal  =  function  ( )  { 
131+   return  this . total ; 
132+ } ; 
133+ 
134+ /** 
135+  * @returns  {object } 
136+  */ 
137+ SearchResult . prototype . getAggregations  =  function  ( )  { 
138+   return  this . aggregations ; 
139+ } ; 
140+ 
141+ /** 
142+  * @returns  {Object } 
143+  */ 
144+ SearchResult . prototype . getOptions  =  function ( )  { 
145+   return  this . options ; 
146+ } ; 
147+ 
148+ /** 
149+  * @returns  {object } 
150+  */ 
151+ SearchResult . prototype . getFilters  =  function ( )  { 
152+   return  this . filters ; 
153+ } ; 
154+ 
155+ /** 
156+  * @returns  {object } 
157+  */ 
158+ SearchResult . prototype . getCollection  =  function  ( )  { 
159+   return  this . collection ; 
160+ } ; 
161+ 
162+ /** 
163+  * @returns  {number } 
164+  */ 
165+ SearchResult . prototype . getFetchedDocument  =  function  ( )  { 
166+   return  this . fetchedDocument ; 
167+ } ; 
168+ 
169+ module . exports  =  SearchResult ; 
0 commit comments