Limited mmove calls

This commit is contained in:
Jérôme Renard 2013-11-29 11:28:37 +01:00
parent f7b8fcf842
commit 4f4a9961b2

28
goes.go
View File

@ -112,46 +112,52 @@ func (c *Connection) BulkSend(index string, documents []Document) (Response, err
// //
// I know it is unreadable I must find an elegant way to fix this. // I know it is unreadable I must find an elegant way to fix this.
bulkData := []byte{} // len(documents) * 2 : action + optional_sources
// + 1 : room for the trailing \n
bulkData := make([][]byte, len(documents)*2+1)
i := 0
for _, doc := range documents { for _, doc := range documents {
header := map[string]interface{}{ action, err := json.Marshal(map[string]interface{}{
doc.BulkCommand: map[string]interface{}{ doc.BulkCommand: map[string]interface{}{
"_index": doc.Index, "_index": doc.Index,
"_type": doc.Type, "_type": doc.Type,
"_id": doc.Id, "_id": doc.Id,
}, },
} })
temp, err := json.Marshal(header)
if err != nil { if err != nil {
return Response{}, err return Response{}, err
} }
temp = append(temp, '\n') bulkData[i] = action
bulkData = append(bulkData, temp[:]...) i++
if len(doc.Fields) > 0 { if len(doc.Fields) > 0 {
fields := map[string]interface{}{} fields := make(map[string]interface{}, len(doc.Fields))
for fieldName, fieldValue := range doc.Fields { for fieldName, fieldValue := range doc.Fields {
fields[fieldName] = fieldValue fields[fieldName] = fieldValue
} }
temp, err = json.Marshal(fields) sources, err := json.Marshal(fields)
if err != nil { if err != nil {
return Response{}, err return Response{}, err
} }
temp = append(temp, '\n') bulkData[i] = sources
bulkData = append(bulkData, temp[:]...) i++
} }
} }
// forces an extra trailing \n absolutely necessary for elasticsearch
bulkData[len(bulkData)-1] = []byte(nil)
r := Request{ r := Request{
Conn: c, Conn: c,
IndexList: []string{index}, IndexList: []string{index},
method: "POST", method: "POST",
api: "_bulk", api: "_bulk",
bulkData: bulkData, bulkData: bytes.Join(bulkData, []byte("\n")),
} }
return r.Run() return r.Run()