main.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package main
  2. //func main() {
  3. // // 创建Elasticsearch客户端
  4. // cfg := elasticsearch.Config{
  5. // Addresses: []string{"http://localhost:9200"}, // 修改为你的Elasticsearch地址
  6. // }
  7. // es, err := elasticsearch.NewClient(cfg)
  8. // if err != nil {
  9. // log.Fatalf("Error creating the Elasticsearch client: %s", err)
  10. // }
  11. //
  12. // // 创建查询请求
  13. // query := map[string]interface{}{
  14. // "query": map[string]interface{}{
  15. // "bool": map[string]interface{}{
  16. // "must_not": map[string]interface{}{
  17. // "term": map[string]interface{}{
  18. // "_id": map[string]interface{}{
  19. // "field": "id",
  20. // },
  21. // },
  22. // },
  23. // },
  24. // },
  25. // }
  26. //
  27. // // 执行查询请求
  28. // res, err := es.Search(
  29. // es.Search.WithContext(context.Background()),
  30. // es.Search.WithIndex("qyxy_v1"),
  31. // es.Search.WithBody(esapi.NewJSONReader(query)),
  32. // es.Search.WithTrackTotalHits(true),
  33. // )
  34. // if err != nil {
  35. // log.Fatalf("Error executing search: %s", err)
  36. // }
  37. // defer res.Body.Close()
  38. //
  39. // if res.IsError() {
  40. // log.Fatalf("Error response: %s", res.Status())
  41. // }
  42. //
  43. // // 解析查询结果
  44. // var docResult map[string]interface{}
  45. // if err := json.NewDecoder(res.Body).Decode(&docResult); err != nil {
  46. // log.Fatalf("Error parsing the response body: %s", err)
  47. // }
  48. //
  49. // hits := docResult["hits"].(map[string]interface{})["hits"].([]interface{})
  50. //
  51. // // 迭代更新数据
  52. // for _, hit := range hits {
  53. // doc := hit.(map[string]interface{})
  54. // source := doc["_source"].(map[string]interface{})
  55. //
  56. // // 更新_id为id的值
  57. // source["_id"] = source["id"]
  58. //
  59. // // 准备更新请求
  60. // updateReq := map[string]interface{}{
  61. // "doc": source,
  62. // }
  63. // body, err := json.Marshal(updateReq)
  64. // if err != nil {
  65. // log.Fatalf("Error marshaling update request: %s", err)
  66. // }
  67. //
  68. // req := esapi.UpdateRequest{
  69. // Index: doc["_index"].(string),
  70. // DocumentID: doc["_id"].(string),
  71. // Body: esapi.NewJSONReader(body),
  72. // }
  73. //
  74. // // 执行更新请求
  75. // res, err := req.Do(context.Background(), es)
  76. // if err != nil {
  77. // log.Fatalf("Error updating document: %s", err)
  78. // }
  79. // defer res.Body.Close()
  80. //
  81. // if res.IsError() {
  82. // log.Fatalf("Error response: %s", res.Status())
  83. // }
  84. //
  85. // log.Printf("Document updated: %s", res.Status())
  86. // }
  87. //}