@@ -27,11 +27,11 @@ defmodule Elasticsearch.Index.Bulk do
2727 %Protocol.UndefinedError{description: "",
2828 protocol: Elasticsearch.Document, value: 123}}
2929 """
30- @ spec encode ( Cluster . t ( ) , struct , String . t ( ) ) ::
30+ @ spec encode ( Cluster . t ( ) , struct , String . t ( ) , String . t ( ) ) ::
3131 { :ok , String . t ( ) }
3232 | { :error , Error . t ( ) }
33- def encode ( cluster , struct , index ) do
34- { :ok , encode! ( cluster , struct , index ) }
33+ def encode ( cluster , struct , index , action \\ "create" ) do
34+ { :ok , encode! ( cluster , struct , index , action ) }
3535 rescue
3636 exception ->
3737 { :error , exception }
@@ -51,9 +51,9 @@ defmodule Elasticsearch.Index.Bulk do
5151 iex> Bulk.encode!(Cluster, 123, "my-index")
5252 ** (Protocol.UndefinedError) protocol Elasticsearch.Document not implemented for 123 of type Integer
5353 """
54- def encode! ( cluster , struct , index ) do
54+ def encode! ( cluster , struct , index , action \\ "create" ) do
5555 config = Cluster.Config . get ( cluster )
56- header = header ( config , "create" , index , struct )
56+ header = header ( config , action , index , struct )
5757
5858 document =
5959 struct
@@ -99,16 +99,17 @@ defmodule Elasticsearch.Index.Bulk do
9999 config = Cluster.Config . get ( cluster )
100100 bulk_page_size = index_config [ :bulk_page_size ] || 5000
101101 bulk_wait_interval = index_config [ :bulk_wait_interval ] || 0
102+ action = index_config [ :bulk_action ] || "create"
102103
103104 errors =
104105 store . transaction ( fn ->
105106 source
106107 |> store . stream ( )
107- |> Stream . map ( & encode! ( config , & 1 , index_name ) )
108+ |> Stream . map ( & encode! ( config , & 1 , index_name , action ) )
108109 |> Stream . chunk_every ( bulk_page_size )
109110 |> Stream . intersperse ( bulk_wait_interval )
110111 |> Stream . map ( & put_bulk_page ( config , index_name , & 1 ) )
111- |> Enum . reduce ( errors , & collect_errors / 2 )
112+ |> Enum . reduce ( errors , & collect_errors ( & 1 , & 2 , action ) )
112113 end )
113114
114115 upload ( config , index_name , % { index_config | sources: tail } , errors )
@@ -123,21 +124,21 @@ defmodule Elasticsearch.Index.Bulk do
123124 Elasticsearch . put ( config , "/#{ index_name } /_doc/_bulk" , Enum . join ( items ) )
124125 end
125126
126- defp collect_errors ( { :ok , % { "errors" => true } = response } , errors ) do
127+ defp collect_errors ( { :ok , % { "errors" => true } = response } , errors , action ) do
127128 new_errors =
128129 response [ "items" ]
129- |> Enum . filter ( & ( & 1 [ "create" ] [ "error" ] != nil ) )
130- |> Enum . map ( & & 1 [ "create" ] )
130+ |> Enum . filter ( & ( & 1 [ action ] [ "error" ] != nil ) )
131+ |> Enum . map ( & & 1 [ action ] )
131132 |> Enum . map ( & Elasticsearch.Exception . exception ( response: & 1 ) )
132133
133134 new_errors ++ errors
134135 end
135136
136- defp collect_errors ( { :error , error } , errors ) do
137+ defp collect_errors ( { :error , error } , errors , _action ) do
137138 [ error | errors ]
138139 end
139140
140- defp collect_errors ( _response , errors ) do
141+ defp collect_errors ( _response , errors , _action ) do
141142 errors
142143 end
143144end
0 commit comments