@@ -11,6 +11,16 @@ defmodule Mix.Tasks.Elasticsearch.Build do
1111 For a functional version of this approach, see
1212 `Elasticsearch.Index.hot_swap/4`.
1313
14+ ## Options
15+
16+ `--cluster`: The `Elasticsearch.Cluster` to build the indexes to.
17+
18+ `--bulk-page-size`: (Optional) The number of documents to post to the cluster in each
19+ bulk page upload. Default: 5000
20+
21+ `--bulk-wait-interval`: (Optional) The number of milliseconds to wait between posting
22+ each bulk page, to avoid overloading your cluster. Default: 0
23+
1424 ## Example
1525
1626 $ mix elasticsearch.build posts [index2] [index3] --cluster MyApp.Cluster
@@ -19,10 +29,16 @@ defmodule Mix.Tasks.Elasticsearch.Build do
1929
2030 $ mix elasticsearch.build posts --existing --cluster MyApp.Cluster
2131 Index posts already exists.
32+
33+ You can also specify `--bulk-page-size` and `--bulk-wait-interval` manually:
34+
35+ $ mix elasticsearch.build posts --cluster MyApp.Cluster --bulk-page-size 1000 --bulk-wait-interval 500
2236 """
2337
2438 require Logger
2539
40+ import Maybe
41+
2642 alias Elasticsearch . {
2743 Cluster.Config ,
2844 Index
@@ -32,31 +48,33 @@ defmodule Mix.Tasks.Elasticsearch.Build do
3248 def run ( args ) do
3349 Mix.Task . run ( "app.start" , [ ] )
3450
35- { cluster , indexes , type } = parse_args! ( args )
51+ { type , cluster , indexes , settings } = parse_args! ( args )
3652 config = Config . get ( cluster )
3753
3854 for alias <- indexes do
39- build ( config , alias , type )
55+ build ( type , config , alias , settings )
4056 end
4157 end
4258
43- defp build ( config , alias , :existing ) do
59+ defp build ( :existing , config , alias , settings ) do
4460 case Index . latest_starting_with ( config , alias ) do
4561 { :ok , name } ->
4662 IO . puts ( "Index already exists: #{ name } " )
4763
4864 { :error , :not_found } ->
49- build ( config , alias , :rebuild )
65+ build ( :rebuild , config , alias , settings )
5066
5167 { :error , exception } ->
52- Mix . raise ( exception )
68+ Mix . raise ( """
69+ Index could not be built.
70+
71+ #{ inspect ( exception ) }
72+ """ )
5373 end
5474 end
5575
56- defp build ( config , alias , :rebuild ) do
57- % { settings: settings , store: store , sources: sources } = config . indexes [ alias ]
58-
59- with :ok <- Index . hot_swap ( config , alias , settings , store , sources ) do
76+ defp build ( :rebuild , config , alias , settings ) do
77+ with :ok <- Index . hot_swap ( config , alias , Map . merge ( config . indexes [ alias ] , settings ) ) do
6078 :ok
6179 else
6280 { :error , errors } when is_list ( errors ) ->
@@ -66,12 +84,12 @@ defmodule Mix.Tasks.Elasticsearch.Build do
6684 Index created, but not aliased: #{ alias }
6785 The following errors occurred:
6886
69- #{ errors }
87+ #{ errors }
7088 """ )
7189
7290 { :error , :enoent } ->
7391 Mix . raise ( """
74- Schema file not found at #{ settings } .
92+ Settings file not found at #{ maybe ( config , [ :indexes , alias , : settings] ) } .
7593 """ )
7694
7795 { :error , exception } ->
@@ -80,14 +98,20 @@ defmodule Mix.Tasks.Elasticsearch.Build do
8098
8199 #{ inspect ( exception ) }
82100 """ )
83-
84- error ->
85- Mix . raise ( error )
86101 end
87102 end
88103
89104 defp parse_args! ( args ) do
90- { options , indexes } = OptionParser . parse! ( args , strict: [ cluster: :string , existing: :boolean ] )
105+ { options , indexes } =
106+ OptionParser . parse! (
107+ args ,
108+ strict: [
109+ cluster: :string ,
110+ existing: :boolean ,
111+ bulk_page_size: :integer ,
112+ bulk_wait_interval: :integer
113+ ]
114+ )
91115
92116 cluster =
93117 if options [ :cluster ] do
@@ -108,7 +132,12 @@ defmodule Mix.Tasks.Elasticsearch.Build do
108132
109133 type = if options [ :existing ] , do: :existing , else: :rebuild
110134
111- { cluster , indexes , type }
135+ settings =
136+ options
137+ |> Keyword . take ( [ :bulk_page_size , :bulk_wait_interval ] )
138+ |> Enum . into ( % { } )
139+
140+ { type , cluster , indexes , settings }
112141 end
113142
114143 defp validate_indexes! ( indexes , cluster ) do
0 commit comments