Skip to content

Commit e13dcdb

Browse files
committed
Fix typeless Update and Explain API
This commit will make the Update and Explain services to fall back to a typeless update/explain URL in case the `type` is explicitly set to the empty string. See #1099
1 parent d58c3ff commit e13dcdb

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

explain.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func (s *ExplainService) buildURL() (string, url.Values, error) {
202202
var path string
203203
var err error
204204

205-
if s.typ == "_doc" {
205+
if s.typ == "" || s.typ == "_doc" {
206206
path, err = uritemplates.Expand("/{index}/_explain/{id}", map[string]string{
207207
"id": s.id,
208208
"index": s.index,

update.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func (b *UpdateService) url() (string, url.Values, error) {
201201
// Build url
202202
var path string
203203
var err error
204-
if b.typ == "_doc" {
204+
if b.typ == "" || b.typ == "_doc" {
205205
path, err = uritemplates.Expand("/{index}/_update/{id}", map[string]string{
206206
"index": b.index,
207207
"id": b.id,

update_integration_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,60 @@ import (
88
"context"
99
"encoding/json"
1010
"testing"
11+
"time"
1112
)
1213

14+
func TestUpdateWithDoc(t *testing.T) {
15+
client := setupTestClientAndCreateIndexAndAddDocs(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
16+
17+
// Get original
18+
getRes, err := client.Get().Index(testIndexName).Id("1").Do(context.TODO())
19+
if err != nil {
20+
t.Fatal(err)
21+
}
22+
var original tweet
23+
if err := json.Unmarshal(getRes.Source, &original); err != nil {
24+
t.Fatal(err)
25+
}
26+
27+
// Partial update
28+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
29+
defer cancel()
30+
updRes, err := client.Update().
31+
Index(testIndexName).
32+
Id("1").
33+
Doc(map[string]interface{}{
34+
"message": "Updated message text.",
35+
}).
36+
Do(ctx)
37+
if err != nil {
38+
t.Fatal(err)
39+
}
40+
if updRes == nil {
41+
t.Fatal("response is nil")
42+
}
43+
if want, have := "updated", updRes.Result; want != have {
44+
t.Fatalf("want Result = %q, have %v", want, have)
45+
}
46+
47+
// Get new version
48+
getRes, err = client.Get().Index(testIndexName).Id("1").Do(context.TODO())
49+
if err != nil {
50+
t.Fatal(err)
51+
}
52+
var updated tweet
53+
if err := json.Unmarshal(getRes.Source, &updated); err != nil {
54+
t.Fatal(err)
55+
}
56+
57+
if want, have := original.User, updated.User; want != have {
58+
t.Fatalf("want User = %q, have %v", want, have)
59+
}
60+
if want, have := "Updated message text.", updated.Message; want != have {
61+
t.Fatalf("want Message = %q, have %v", want, have)
62+
}
63+
}
64+
1365
func TestUpdateWithScript(t *testing.T) {
1466
client := setupTestClientAndCreateIndexAndAddDocs(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
1567

0 commit comments

Comments
 (0)