|
@@ -4,6 +4,7 @@ import (
|
|
|
"bytes"
|
|
|
"context"
|
|
|
"encoding/json"
|
|
|
+ "errors"
|
|
|
"eta/eta_mini_ht_api/common/component/config"
|
|
|
logger "eta/eta_mini_ht_api/common/component/log"
|
|
|
"eta/eta_mini_ht_api/common/contants"
|
|
@@ -152,6 +153,16 @@ type Hit struct {
|
|
|
Highlight json.RawMessage `json:"highlight"`
|
|
|
}
|
|
|
|
|
|
+type Doc struct {
|
|
|
+ Index string `json:"_index"`
|
|
|
+ Type string `json:"_type"`
|
|
|
+ ID string `json:"_id"`
|
|
|
+ Version float64 `json:"_version"`
|
|
|
+ SeqNo float64 `json:"_seq_no"`
|
|
|
+ PrimaryTerm float64 `json:"_primary_term"`
|
|
|
+ Found bool `json:"found"`
|
|
|
+ Source json.RawMessage `json:"_source"`
|
|
|
+}
|
|
|
type ShardsInfo struct {
|
|
|
Total int `json:"total"`
|
|
|
Successful int `json:"successful"`
|
|
@@ -695,25 +706,27 @@ func (es *ESClient) Exist(indexName string, docId int) (exist bool, err error) {
|
|
|
res, err := getRequest.Do(context.Background(), es.es())
|
|
|
if err != nil {
|
|
|
logger.Error("es获取文档是否存在失败: %v", err)
|
|
|
+ return
|
|
|
}
|
|
|
defer res.Body.Close()
|
|
|
-
|
|
|
// 检查文档是否存在
|
|
|
if res.IsError() {
|
|
|
// 如果文档不存在,通常返回 404 Not Found
|
|
|
if res.StatusCode == 404 {
|
|
|
logger.Info("文档不存在.")
|
|
|
- return false, nil
|
|
|
+ err = errors.New("ES文档不存在")
|
|
|
+ return
|
|
|
} else {
|
|
|
// 其他错误
|
|
|
var e map[string]interface{}
|
|
|
if err = json.NewDecoder(res.Body).Decode(&e); err != nil {
|
|
|
logger.Error("解析es应答失败: %v", err)
|
|
|
- return false, err
|
|
|
+ return
|
|
|
} else {
|
|
|
// Print the response status and error information.
|
|
|
logger.Error("[%s] %s: %s\n", res.Status(), e["error"].(map[string]interface{})["type"], e["error"].(map[string]interface{})["原因"])
|
|
|
- return false, nil
|
|
|
+ err = errors.New("获取ES记录失败")
|
|
|
+ return
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
@@ -723,6 +736,56 @@ func (es *ESClient) Exist(indexName string, docId int) (exist bool, err error) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func (es *ESClient) Get(indexName string, docId int) (doc Doc, err error) {
|
|
|
+
|
|
|
+ getRequest := esapi.GetRequest{
|
|
|
+ Index: indexName,
|
|
|
+ DocumentID: strconv.Itoa(docId),
|
|
|
+ }
|
|
|
+ // 执行请求
|
|
|
+ res, err := getRequest.Do(context.Background(), es.es())
|
|
|
+ if err != nil {
|
|
|
+ logger.Error("es获取文档是否存在失败: %v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer res.Body.Close()
|
|
|
+ // 检查文档是否存在
|
|
|
+ if res.IsError() {
|
|
|
+ // 如果文档不存在,通常返回 404 Not Found
|
|
|
+ if res.StatusCode == 404 {
|
|
|
+ logger.Info("文档不存在.")
|
|
|
+ err = errors.New("ES文档不存在")
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ // 其他错误
|
|
|
+ var e map[string]interface{}
|
|
|
+ if err = json.NewDecoder(res.Body).Decode(&e); err != nil {
|
|
|
+ logger.Error("解析es应答失败: %v", err)
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ // Print the response status and error information.
|
|
|
+ logger.Error("[%s] %s: %s\n", res.Status(), e["error"].(map[string]interface{})["type"], e["error"].(map[string]interface{})["原因"])
|
|
|
+ err = errors.New("获取ES记录失败")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 如果文档存在
|
|
|
+ body, readErr := io.ReadAll(res.Body)
|
|
|
+ if readErr != nil {
|
|
|
+ logger.Error("获取es应答失败: %v", err)
|
|
|
+ err = readErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = json.Unmarshal(body, &doc)
|
|
|
+ if err != nil {
|
|
|
+ logger.Error("反序列化es应答失败: %v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
//
|
|
|
//func CreateIndex(indexName string) error {
|
|
|
// resp, err := esClient.es().Indices.
|