@@ -86,6 +86,7 @@ private void GenerateBulkPathAndJson(BulkDescriptor bulkDescriptor, out string j
8686 path += queryString . ToQueryString ( ) ;
8787
8888 }
89+
8990 public IBulkResponse Bulk ( BulkDescriptor bulkDescriptor )
9091 {
9192 string json , path ;
@@ -193,72 +194,64 @@ private string GenerateBulkCommand<T>(IEnumerable<BulkParameters<T>> objects, st
193194 }
194195
195196
197+
198+ //used by IndexMany and DeleteMany
196199 private string GenerateBulkCommand < T > ( IEnumerable < T > @objects , string index , string typeName , string command ) where T : class
197200 {
198201 objects . ThrowIfEmpty ( "objects" ) ;
199202
200- var sb = new StringBuilder ( ) ;
201- var action = "{{ \" {0}\" : {{ \" _index\" : \" {1}\" , \" _type\" : \" {2}\" " . F ( command , index , typeName ) ;
202-
203- foreach ( var @object in objects )
203+ var b = new BulkDescriptor ( ) ;
204+ b . FixedPath ( index , typeName ) ;
205+ foreach ( var @object in @objects )
204206 {
205- var objectAction = action ;
206-
207- var id = this . Infer . Id ( @object ) ;
208- if ( ! id . IsNullOrEmpty ( ) )
209- objectAction += ", \" _id\" : \" {0}\" " . F ( id ) ;
210-
211- objectAction += "} }\n " ;
212-
213- sb . Append ( objectAction ) ;
207+ var o = @object ;
214208 if ( command == "index" )
215- {
216- string jsonCommand = this . Serializer . Serialize ( @object , Formatting . None ) ;
217- sb . Append ( jsonCommand + "\n " ) ;
218- }
209+ b . Index < T > ( bb => bb . Object ( o ) ) ;
210+ else if ( command == "delete" )
211+ b . Delete < T > ( bb => bb . Object ( o ) ) ;
219212 }
220- var json = sb . ToString ( ) ;
213+
214+ string json , path ;
215+ this . GenerateBulkPathAndJson ( b , out json , out path ) ;
221216 return json ;
217+ }
222218
223219
224-
225- }
220+ //used by IndexMany and DeleteMany
226221 private string GenerateBulkCommand < T > ( IEnumerable < BulkParameters < T > > @objects , string index , string typeName , string command ) where T : class
227222 {
228223 objects . ThrowIfEmpty ( "objects" ) ;
229224
230- var sb = new StringBuilder ( ) ;
231- var action = "{{ \" {0}\" : {{ \" _index\" : \" {1}\" , \" _type\" : \" {2}\" " . F ( command , index , typeName ) ;
232225
233- foreach ( var @object in objects )
226+ var b = new BulkDescriptor ( ) ;
227+ b . FixedPath ( index , typeName ) ;
228+ foreach ( var @object in @objects )
234229 {
235- if ( @object . Document == null )
236- continue ;
237-
238- var objectAction = action ;
239- if ( ! @object . Id . IsNullOrEmpty ( ) )
240- objectAction += ", \" _id\" : \" {0}\" " . F ( @object . Id ) ;
241- else
242- objectAction += ", \" _id\" : \" {0}\" " . F ( this . Infer . Id ( @object . Document ) ) ;
243-
244- if ( ! @object . Version . IsNullOrEmpty ( ) )
245- objectAction += ", \" version\" : \" {0}\" " . F ( @object . Version ) ;
246- if ( ! @object . Parent . IsNullOrEmpty ( ) )
247- objectAction += ", \" parent\" : \" {0}\" " . F ( @object . Parent ) ;
248- if ( @object . VersionType != VersionType . Internal )
249- objectAction += ", \" version_type\" : \" {0}\" " . F ( @object . VersionType . ToString ( ) . ToLower ( ) ) ;
250- if ( ! @object . Routing . IsNullOrEmpty ( ) )
251- objectAction += ", \" routing\" : \" {0}\" " . F ( @object . Routing ) ;
252- objectAction += "} }\n " ;
253-
254- sb . Append ( objectAction ) ;
230+ var o = @object ;
255231 if ( command == "index" )
256- {
257- string jsonCommand = this . Serializer . Serialize ( @object . Document , Formatting . None ) ;
258- sb . Append ( jsonCommand + "\n " ) ;
259- }
232+ b . Index < T > ( bb => bb
233+ . Object ( o . Document )
234+ . Id ( o . Id )
235+ . Parent ( o . Parent )
236+ . Percolate ( o . Percolate )
237+ . Routing ( o . Routing )
238+ . Timestamp ( o . Timestamp )
239+ . Ttl ( o . Ttl )
240+ . Version ( o . Version )
241+ . VersionType ( o . VersionType ) ) ;
242+ else if ( command == "delete" )
243+ b . Delete < T > ( bb => bb
244+ . Object ( o . Document )
245+ . Parent ( o . Parent )
246+ . Routing ( o . Routing )
247+ . Timestamp ( o . Timestamp )
248+ . Ttl ( o . Ttl )
249+ . Version ( o . Version )
250+ . VersionType ( o . VersionType ) ) ;
260251 }
261- var json = sb . ToString ( ) ;
252+
253+ string json , path ;
254+ this . GenerateBulkPathAndJson ( b , out json , out path ) ;
262255 return json ;
263256 }
264257
0 commit comments