package main //func main() { // // 创建Elasticsearch客户端 // cfg := elasticsearch.Config{ // Addresses: []string{"http://localhost:9200"}, // 修改为你的Elasticsearch地址 // } // es, err := elasticsearch.NewClient(cfg) // if err != nil { // log.Fatalf("Error creating the Elasticsearch client: %s", err) // } // // // 创建查询请求 // query := map[string]interface{}{ // "query": map[string]interface{}{ // "bool": map[string]interface{}{ // "must_not": map[string]interface{}{ // "term": map[string]interface{}{ // "_id": map[string]interface{}{ // "field": "id", // }, // }, // }, // }, // }, // } // // // 执行查询请求 // res, err := es.Search( // es.Search.WithContext(context.Background()), // es.Search.WithIndex("qyxy_v1"), // es.Search.WithBody(esapi.NewJSONReader(query)), // es.Search.WithTrackTotalHits(true), // ) // if err != nil { // log.Fatalf("Error executing search: %s", err) // } // defer res.Body.Close() // // if res.IsError() { // log.Fatalf("Error response: %s", res.Status()) // } // // // 解析查询结果 // var docResult map[string]interface{} // if err := json.NewDecoder(res.Body).Decode(&docResult); err != nil { // log.Fatalf("Error parsing the response body: %s", err) // } // // hits := docResult["hits"].(map[string]interface{})["hits"].([]interface{}) // // // 迭代更新数据 // for _, hit := range hits { // doc := hit.(map[string]interface{}) // source := doc["_source"].(map[string]interface{}) // // // 更新_id为id的值 // source["_id"] = source["id"] // // // 准备更新请求 // updateReq := map[string]interface{}{ // "doc": source, // } // body, err := json.Marshal(updateReq) // if err != nil { // log.Fatalf("Error marshaling update request: %s", err) // } // // req := esapi.UpdateRequest{ // Index: doc["_index"].(string), // DocumentID: doc["_id"].(string), // Body: esapi.NewJSONReader(body), // } // // // 执行更新请求 // res, err := req.Do(context.Background(), es) // if err != nil { // log.Fatalf("Error updating document: %s", err) // } // defer res.Body.Close() // // if res.IsError() { // log.Fatalf("Error response: %s", res.Status()) // } // // log.Printf("Document updated: %s", res.Status()) // } //}