diff --git a/bulk_delete_request.go b/bulk_delete_request.go index 55b2cbcb0..2f7d1f15e 100644 --- a/bulk_delete_request.go +++ b/bulk_delete_request.go @@ -179,7 +179,7 @@ func (r *BulkDeleteRequest) Source() ([]string, error) { return nil, err } - lines := []string{string(body)} + lines := []string{unsafeBytesToString(body)} r.source = lines return lines, nil diff --git a/bulk_index_request.go b/bulk_index_request.go index fcbc71733..822f99671 100644 --- a/bulk_index_request.go +++ b/bulk_index_request.go @@ -231,7 +231,7 @@ func (r *BulkIndexRequest) Source() ([]string, error) { return nil, err } - lines[0] = string(body) + lines[0] = unsafeBytesToString(body) // "field1" ... if r.doc != nil { @@ -241,7 +241,7 @@ func (r *BulkIndexRequest) Source() ([]string, error) { if err != nil { return nil, err } - lines[1] = string(body) + lines[1] = unsafeBytesToString(body) case json.RawMessage: lines[1] = string(t) case *json.RawMessage: diff --git a/bulk_update_request.go b/bulk_update_request.go index a0ddf1805..9baa3806a 100644 --- a/bulk_update_request.go +++ b/bulk_update_request.go @@ -281,7 +281,7 @@ func (r *BulkUpdateRequest) Source() ([]string, error) { return nil, err } - lines[0] = string(body) + lines[0] = unsafeBytesToString(body) // 2nd line: {"doc" : { ... }} or {"script": {...}} var doc interface{} @@ -327,7 +327,7 @@ func (r *BulkUpdateRequest) Source() ([]string, error) { return nil, err } - lines[1] = string(body) + lines[1] = unsafeBytesToString(body) r.source = lines return lines, nil diff --git a/msearch.go b/msearch.go index 1f6aed0e5..78bbc810d 100644 --- a/msearch.go +++ b/msearch.go @@ -137,7 +137,7 @@ func (s *MultiSearchService) Do(ctx context.Context) (*MultiSearchResult, error) if err != nil { return nil, err } - lines = append(lines, string(header)) + lines = append(lines, unsafeBytesToString(header)) lines = append(lines, body) } body := strings.Join(lines, "\n") + "\n" // add trailing \n diff --git a/search_request.go b/search_request.go index 3a444b808..19ce7a4d6 100644 --- a/search_request.go +++ b/search_request.go @@ -523,7 +523,7 @@ func (r *SearchRequest) Body() (string, error) { if err != nil { return "", err } - return string(body), nil + return unsafeBytesToString(body), nil } switch t := r.source.(type) { default: @@ -531,7 +531,7 @@ func (r *SearchRequest) Body() (string, error) { if err != nil { return "", err } - return string(body), nil + return unsafeBytesToString(body), nil case *SearchSource: src, err := t.Source() if err != nil { @@ -541,7 +541,7 @@ func (r *SearchRequest) Body() (string, error) { if err != nil { return "", err } - return string(body), nil + return unsafeBytesToString(body), nil case json.RawMessage: return string(t), nil case *json.RawMessage: diff --git a/unsafe.go b/unsafe.go new file mode 100644 index 000000000..af5d935d0 --- /dev/null +++ b/unsafe.go @@ -0,0 +1,11 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import "unsafe" + +func unsafeBytesToString(b []byte) string { + return *(*string)(unsafe.Pointer(&b)) +}