通过bulk,批量创建/更新索引
(data []ElasticSearchData)
| 341 | |
| 342 | //通过bulk,批量创建/更新索引 |
| 343 | func (this *ElasticSearchClient) BuildIndexByBuck(data []ElasticSearchData) (err error) { |
| 344 | var bodySlice []string |
| 345 | if len(data) > 0 { |
| 346 | for _, item := range data { |
| 347 | action := fmt.Sprintf(`{"index":{"_index":"%v","_type":"%v","_id":%v}}`, this.Index, this.Type, item.Id) |
| 348 | bodySlice = append(bodySlice, action) |
| 349 | bodySlice = append(bodySlice, util.InterfaceToJson(item)) |
| 350 | } |
| 351 | api := this.Host + "_bulk" |
| 352 | body := strings.Join(bodySlice, "\n") + "\n" |
| 353 | if helper.Debug { |
| 354 | helper.Logger.Info("批量更新索引请求体") |
| 355 | helper.Logger.Info(body) |
| 356 | } |
| 357 | if resp, errResp := this.post(api).Body(body).Response(); errResp != nil { |
| 358 | err = errResp |
| 359 | } else { |
| 360 | if resp.StatusCode >= http.StatusMultipleChoices || resp.StatusCode < http.StatusOK { |
| 361 | b, _ := ioutil.ReadAll(resp.Body) |
| 362 | err = errors.New(resp.Status + ";" + string(b)) |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | return |
| 367 | } |
| 368 | |
| 369 | //创建索引 |
| 370 | func (this *ElasticSearchClient) BuildIndex(es ElasticSearchData) (err error) { |
no test coverage detected