Go语言ElasticSearch的使用方法

101 阅读1分钟

引言

ElasticSearch是一个基于Lucene的搜索引擎,它可以处理PB级别的数据量,提供近实时的搜索体验。之前都是用C++通过curl的方式对ElasticSearch中的数据进行管理,最近因为业务上的调整,为了快速出产品,采用Go语言来管理,以下是相关接口的封装。

索引获取

func GetIndexMapping(indexName string) (map[string]interface{}, uint) {
    req := esapi.IndicesGetMappingRequest{
        Index: []string{indexName}, //索引名字
    }
    res, err := req.Do(context.Background(), rpc.Elastic_client)
       if err != nil {
           return nil, err
       }
    defer res.Body.Close()
    if res.IsError() {
        return nil, nil
    }
    var mapping map[string]interface{}
    if err := json.NewDecoder(res.Body).Decode(&mapping); err != nil {
        return nil, err
    }
    return mapping, nil
}

索引创建

func CreateNewIndex(indexName, mapping string) error {
    req := esapi.IndicesCreateRequest{
        Index: indexName,
        Body: strings.NewReader(mapping),
    }
    res, err := req.Do(context.Background(), rpc.Elastic_client)
    if err != nil {
        return errors.New("Error creating index:" + err.Error())
    }
    defer res.Body.Close()
    if res.IsError() {
        return errors.New("Failed to create index" + res.String())
    } else {
        log.Printf("New index '%s' created successfully.\n", indexName)
    }
    return nil
}

数据插入

func InsertProDataElastic(indexName string, ID string, body []byte) error {
    req := esapi.IndexRequest{
        Index: indexName,
        DocumentID: ID,
        Body: bytes.NewReader(body),
        Refresh: "true", // 可选,设置为"true"可以在每次索引操作后立即刷新索引
    }
// 执行请求
    res, err := req.Do(context.Background(), rpc.Elastic_client)
    if err != nil {
        return errors.New("Error indexing document:" + err.Error())
    }
    defer res.Body.Close()
// 检查响应状态
    if res.IsError() {
        return errors.New("Failed to index document:" + res.String())
    } else {
        var r map[string]interface{}
        if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
            return errors.New("Error parsing the response body:" + err.Error())
       } else {
         // 打印插入成功的文档ID
            fmt.Println("Indexed document ID:", r["_id"])
       }
    }
    return nil
}

数据获取

func GetDataElastic(indexName string, ID string) (string,error) {
    res, err := rpc.Elastic_client.Get(indexName, ID)
    if err != nil {
       return "", err
    }
    defer res.Body.Close()
    if res.IsError() {
        var e map[string]interface{}
        if err := json.NewDecoder(res.Body).Decode(&e); err == nil {
            if res.StatusCode == http.StatusNotFound {
               return "", err
            }
            return "", err
        } else {
            log.Println("Error parsing the response body:" + err.Error())
            return "", err     
        }
    }

    var doc MyDocument
    if err := json.NewDecoder(res.Body).Decode(&doc); err != nil {  
        log.Println("Error decoding the document body: ", err)
        return "", err
    }
    jsonData, err := json.Marshal(doc.Source)
    if err != nil {
        return "", err
    }
   return string(jsonData), nil
}

数据删除

func DeleteDataElastic(indexName string, ID string) error {
    req := esapi.DeleteRequest{
        Index: indexName,
        DocumentID: ID,
    }   
    res, err := req.Do(context.Background(), rpc.Elastic_client)
    if err != nil {
        return errors.New("Error deleting document:" + err.Error())
    }
    defer res.Body.Close()
    if res.IsError() {
        if res.StatusCode == 404 {
            fmt.Printf("Document with ID %s not found, no action taken.\n", ID)
            return nil
        } else {
            log.Fatalf("Error response: %s", res.String())
            return errors.New("Error deleting document:" + res.String())
        }
    } else {
       fmt.Printf("Document with ID %s successfully deleted.\n", ID)
    }
    return nil
}