Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/elasticsearch/indexing/bulk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,23 @@ defmodule Elasticsearch.Index.Bulk do
{"title":null,"doctype":{"name":"post"},"author":null}
\"\"\"

iex> Bulk.encode!(Cluster, %Post{id: "my-id"}, "my-index", "delete")
\"\"\"
{"delete":{"_index":"my-index","_id":"my-id"}}
\"\"\"

iex> Bulk.encode!(Cluster, 123, "my-index")
** (Protocol.UndefinedError) protocol Elasticsearch.Document not implemented for 123 of type Integer
"""
def encode!(cluster, struct, index, action \\ "create") do
def encode!(cluster, struct, index, action \\ "create")

def encode!(cluster, struct, index, "delete") do
config = Cluster.Config.get(cluster)
header = header(config, "delete", index, struct)
"#{header}\n"
end

def encode!(cluster, struct, index, action) do
config = Cluster.Config.get(cluster)
header = header(config, action, index, struct)

Expand Down
7 changes: 7 additions & 0 deletions test/elasticsearch/indexing/bulk_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,11 @@ defmodule Elasticsearch.Index.BulkTest do
"\"_routing\":\"123\""
end
end

describe ".encode!/4" do
test "'delete' omits the struct from the body" do
assert "{\"delete\":{\"_routing\":\"123\",\"_index\":\"my-index\",\"_id\":\"my-id\"}}\n" ==
Bulk.encode!(Cluster, %Comment{id: "my-id", post_id: "123"}, "my-index", "delete")
end
end
end