123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883 |
- package es
- 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"
- "fmt"
- "github.com/elastic/go-elasticsearch/v7"
- "github.com/elastic/go-elasticsearch/v7/esapi"
- "io"
- "strconv"
- "strings"
- "sync"
- )
- type ESBase interface {
- GetId() string
- }
- var (
- esOnce sync.Once
- esClient *ESClient
- )
- type ESClient struct {
- esOp *elasticsearch.Client
- }
- type SearchType string
- const (
- MatchAll = "match_all"
- Match = "match"
- CountWithDocIds = "count_with_doc_ids"
- Range = "range"
- MatchAllByCondition = "match_all_by_condition"
- RangeByCondition = "range_by_condition"
- RangeByConditionWithDocIds = "range_by_condition_with_doc_ids"
- RangeByConditionWithDocIdsNoLimit = "range_by_condition_with_doc_ids_no_limit"
- RangeWithDocIds = "range_with_doc_ids"
- LimitByScore = "limit_by_score"
- HomeSearch = "home_search"
- )
- func GetInstance() *ESClient {
- esOnce.Do(func() {
- // 检查是否成功获取到RedisConfig实例,没有配置则不进行redis初始化
- if esConf, ok := config.GetConfig(contants.ES).(*config.ESConfig); ok {
- logger.Info("初始化es")
- // 这里可以添加初始化Redis的逻辑
- esClient = newEs(esConf)
- logger.Info("es地址:%v", esConf.GetUrl())
- }
- })
- return esClient
- }
- func (es *ESClient) es() *elasticsearch.Client {
- return es.esOp
- }
- func newEs(config *config.ESConfig) *ESClient {
- elasticsearch.NewDefaultClient()
- client, err := elasticsearch.NewClient(
- elasticsearch.Config{
- Addresses: []string{config.GetUrl()},
- // A list of Elasticsearch nodes to use.
- Username: config.GetUserName(),
- Password: config.GetPassword(), // Password for HTTP Basic Authentication.
- },
- )
- if err != nil {
- logger.Error("连接ES失败:%v", err)
- panic("启动es失败")
- }
- return &ESClient{esOp: client}
- }
- func init() {
- if GetInstance() == nil {
- panic("初始化es失败")
- }
- logger.Info("es初始化成功")
- }
- // BulkInsert 批量创建文档
- func (es *ESClient) BulkInsert(indexName string, docs []ESBase) (err error) {
- // 创建批量请求
- bulkBody := new(bytes.Buffer)
- for _, doc := range docs {
- enc := json.NewEncoder(bulkBody)
- if err = enc.Encode(map[string]interface{}{
- "index": map[string]interface{}{
- "_index": indexName,
- "_id": doc.GetId(),
- },
- }); err != nil {
- logger.Error("生成es批处理请求参数失败: %s", err)
- }
- if err = enc.Encode(doc); err != nil {
- logger.Error("生成es批处理文档失败: %s", err)
- }
- }
- bulkReq := esapi.BulkRequest{
- Body: bytes.NewReader(bulkBody.Bytes()),
- Refresh: "true",
- }
- res, err := bulkReq.Do(context.Background(), es.esOp)
- if err != nil {
- logger.Error("es批处理创建失败: %s", err)
- }
- defer res.Body.Close()
- if res.IsError() {
- var e map[string]interface{}
- if err = json.NewDecoder(res.Body).Decode(&e); err != nil {
- logger.Error("解析es应答失败: %v", err)
- } else {
- logger.Error("es请求失败: %s: %v\n", res.Status(), err)
- }
- }
- return
- }
- type ESResponse struct {
- Took int `json:"took"`
- Count int `json:"count"`
- TimedOut bool `json:"timedOut"`
- Hits Hits `json:"hits"`
- _Shards ShardsInfo `json:"_shards"`
- }
- type Hits struct {
- Total TotalHits `json:"total"`
- MaxScore float64 `json:"maxScore"`
- Hits []Hit `json:"hits"`
- }
- type TotalHits struct {
- Value int `json:"value"`
- Relation string `json:"relation"`
- }
- type Hit struct {
- Index string `json:"_index"`
- Type string `json:"_type"`
- ID string `json:"_id"`
- Score float64 `json:"_score"`
- Source json.RawMessage `json:"_source"`
- 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"`
- Skipped int `json:"skipped"`
- Failed int `json:"failed"`
- }
- type ESQueryRequest struct {
- IndexName string
- From int
- Size int
- Key string
- Column string
- Condition string
- ConditionValue string
- Sorts []string
- Type SearchType
- RangeColumn string
- DocIds []string
- Max interface{}
- Min interface{}
- MinScore float64
- }
- func (req *ESQueryRequest) CreateESQueryRequest(index string, column string, key string, from int, size int, sorts []string, searchType SearchType) *ESQueryRequest {
- return &ESQueryRequest{
- IndexName: index,
- Type: searchType,
- From: from,
- Size: size,
- Key: key,
- Column: column,
- Sorts: sorts,
- }
- }
- func (req *ESQueryRequest) Limit(limit int) *ESQueryRequest {
- req.Size = limit
- return req
- }
- func (req *ESQueryRequest) WithScore(score float64) *ESQueryRequest {
- req.MinScore = score
- return req
- }
- func (req *ESQueryRequest) Range(from int64, to int64, column string) *ESQueryRequest {
- req.RangeColumn = column
- req.Max = to
- req.Min = from
- return req
- }
- func (req *ESQueryRequest) Before(max interface{}, column string) *ESQueryRequest {
- req.RangeColumn = column
- req.Max = max
- return req
- }
- func (req *ESQueryRequest) ByCondition(column string, value string) *ESQueryRequest {
- req.Condition = column
- req.ConditionValue = value
- return req
- }
- func (req *ESQueryRequest) WithDocs(docIds []string) *ESQueryRequest {
- req.DocIds = docIds
- return req
- }
- func (req *ESQueryRequest) parseJsonQuery() (queryMap map[string]interface{}) {
- switch req.Type {
- case MatchAll:
- queryMap = map[string]interface{}{
- "query": map[string]interface{}{
- "match": map[string]interface{}{
- req.Column: req.Key,
- },
- },
- }
- return
- case MatchAllByCondition:
- queryMap = map[string]interface{}{
- "query": map[string]interface{}{
- "bool": map[string]interface{}{
- "must": []map[string]interface{}{
- {
- "match": map[string]interface{}{
- req.Column: req.Key,
- },
- },
- {
- "term": map[string]interface{}{
- req.Condition: req.ConditionValue,
- },
- },
- },
- },
- },
- }
- return
- case Match:
- queryMap = map[string]interface{}{
- "query": map[string]interface{}{
- "match": map[string]interface{}{
- req.Column: req.Key,
- },
- },
- "highlight": map[string]interface{}{
- "fields": map[string]interface{}{
- req.Column: map[string]interface{}{},
- },
- "pre_tags": []string{"<span style='color:#0078E8'>"},
- "post_tags": []string{"</span>"},
- },
- }
- return
- case CountWithDocIds:
- queryMap = map[string]interface{}{
- "query": map[string]interface{}{
- "bool": map[string]interface{}{
- "must": []map[string]interface{}{
- {
- "match": map[string]interface{}{
- req.Column: req.Key,
- },
- },
- },
- "filter": map[string]interface{}{
- "terms": map[string]interface{}{
- "_id": req.DocIds,
- },
- },
- },
- },
- }
- return
- case Range:
- queryMap = map[string]interface{}{
- "query": map[string]interface{}{
- "match": map[string]interface{}{
- req.Column: req.Key,
- },
- },
- "highlight": map[string]interface{}{
- "fields": map[string]interface{}{
- req.Column: map[string]interface{}{},
- },
- "pre_tags": []string{"<span style='color:#0078E8'>"},
- "post_tags": []string{"</span>"},
- },
- "post_filter": map[string]interface{}{
- "range": map[string]interface{}{
- req.RangeColumn: map[string]interface{}{
- "gte": req.Min,
- "lte": req.Max,
- },
- },
- },
- }
- return
- case RangeWithDocIds:
- queryMap = map[string]interface{}{
- "query": map[string]interface{}{
- "match": map[string]interface{}{
- req.Column: req.Key,
- },
- },
- "highlight": map[string]interface{}{
- "fields": map[string]interface{}{
- req.Column: map[string]interface{}{},
- },
- "pre_tags": []string{"<span style='color:#0078E8'>"},
- "post_tags": []string{"</span>"},
- },
- "post_filter": map[string]interface{}{
- "bool": map[string]interface{}{
- "must": []map[string]interface{}{
- {
- "range": map[string]interface{}{
- req.RangeColumn: map[string]interface{}{
- "gte": req.Min,
- "lte": req.Max,
- },
- },
- },
- {
- "terms": map[string]interface{}{
- "_id": req.DocIds,
- },
- },
- },
- },
- },
- }
- return
- case RangeByCondition:
- queryMap = map[string]interface{}{
- "query": map[string]interface{}{
- "match": map[string]interface{}{
- req.Column: req.Key,
- },
- },
- "highlight": map[string]interface{}{
- "fields": map[string]interface{}{
- req.Column: map[string]interface{}{},
- },
- "pre_tags": []string{"<span style='color:#0078E8'>"},
- "post_tags": []string{"</span>"},
- },
- "post_filter": map[string]interface{}{
- "bool": map[string]interface{}{
- "must": []map[string]interface{}{
- {
- "range": map[string]interface{}{
- req.RangeColumn: map[string]interface{}{
- "gte": req.Min,
- "lte": req.Max,
- },
- },
- },
- {
- "term": map[string]interface{}{
- req.Condition: req.ConditionValue,
- },
- },
- },
- },
- },
- }
- return
- case RangeByConditionWithDocIds:
- queryMap = map[string]interface{}{
- "query": map[string]interface{}{
- "match": map[string]interface{}{
- req.Column: req.Key,
- },
- },
- "highlight": map[string]interface{}{
- "fields": map[string]interface{}{
- req.Column: map[string]interface{}{},
- },
- "pre_tags": []string{"<span style='color:#0078E8'>"},
- "post_tags": []string{"</span>"},
- },
- "post_filter": map[string]interface{}{
- "bool": map[string]interface{}{
- "must": []map[string]interface{}{
- {
- "range": map[string]interface{}{
- req.RangeColumn: map[string]interface{}{
- "gte": req.Min,
- "lte": req.Max,
- },
- },
- },
- {
- "term": map[string]interface{}{
- req.Condition: req.ConditionValue,
- },
- },
- {
- "terms": map[string]interface{}{
- "_id": req.DocIds,
- },
- },
- },
- },
- },
- }
- return
- case RangeByConditionWithDocIdsNoLimit:
- queryMap = map[string]interface{}{
- "query": map[string]interface{}{
- "match": map[string]interface{}{
- req.Column: req.Key,
- },
- },
- "highlight": map[string]interface{}{
- "fields": map[string]interface{}{
- req.Column: map[string]interface{}{},
- },
- "pre_tags": []string{"<span style='color:#0078E8'>"},
- "post_tags": []string{"</span>"},
- },
- "post_filter": map[string]interface{}{
- "bool": map[string]interface{}{
- "must": []map[string]interface{}{
- {
- "term": map[string]interface{}{
- req.Condition: req.ConditionValue,
- },
- },
- {
- "terms": map[string]interface{}{
- "_id": req.DocIds,
- },
- },
- },
- },
- },
- }
- return
- case LimitByScore:
- queryMap = map[string]interface{}{
- "query": map[string]interface{}{
- "match": map[string]interface{}{
- req.Column: req.Key,
- },
- },
- "highlight": map[string]interface{}{
- "fields": map[string]interface{}{
- req.Column: map[string]interface{}{},
- },
- "pre_tags": []string{"<span style='color:#0078E8'>"},
- "post_tags": []string{"</span>"},
- },
- "post_filter": map[string]interface{}{
- "bool": map[string]interface{}{
- "must": []map[string]interface{}{
- {
- "terms": map[string]interface{}{
- "_id": req.DocIds,
- },
- },
- },
- },
- },
- "min_score": req.MinScore,
- }
- return
- case HomeSearch:
- queryMap = map[string]interface{}{
- "query": map[string]interface{}{
- "bool": map[string]interface{}{
- "should": []map[string]interface{}{
- {
- "bool": map[string]interface{}{
- "must": []map[string]interface{}{
- {
- "match": map[string]interface{}{
- "title": req.Key,
- },
- },
- {
- "term": map[string]interface{}{
- "status": "PUBLISH",
- },
- },
- {
- "range": map[string]interface{}{
- "publishedTime": map[string]interface{}{
- "lte": req.Max,
- },
- },
- },
- },
- },
- },
- {
- "bool": map[string]interface{}{
- "must": []map[string]interface{}{
- {
- "match": map[string]interface{}{
- "mediaName": req.Key,
- },
- },
- {
- "range": map[string]interface{}{
- req.RangeColumn: map[string]interface{}{
- "lte": req.Max,
- },
- },
- },
- },
- },
- },
- },
- },
- },
- "highlight": map[string]interface{}{
- "fields": map[string]interface{}{
- "title": map[string]interface{}{},
- "mediaName": map[string]interface{}{},
- },
- "pre_tags": []string{"<span style='color:#0078E8'>"},
- "post_tags": []string{"</span>"},
- },
- }
- return
- default:
- queryMap = map[string]interface{}{}
- return
- }
- }
- // /*
- // *
- //
- // 搜索
- //
- // indexName 访问索引名
- // query 搜索条件
- // from 开始搜索位置
- // size 搜索条数
- // sort 排序
- // */
- func (es *ESClient) Count(params *ESQueryRequest) (response ESResponse, err error) {
- queryMap := params.parseJsonQuery()
- jsonQuery, _ := json.Marshal(queryMap)
- logger.Info("查询语句: %s", string(jsonQuery))
- request := esapi.CountRequest{
- Index: []string{params.IndexName},
- Body: strings.NewReader(string(jsonQuery)),
- }
- res, err := request.Do(context.Background(), esClient.esOp)
- defer res.Body.Close()
- if err != nil {
- logger.Error("es查询失败: %s", err)
- }
- if res.IsError() {
- var e map[string]interface{}
- if err = json.NewDecoder(res.Body).Decode(&e); err != nil {
- logger.Error("解析es应答失败: %v", err)
- } else {
- logger.Error("es请求失败: %s: %v\n", res.Status(), e)
- }
- }
- body, err := io.ReadAll(res.Body)
- if err != nil {
- logger.Error("获取es应答失败: %v", err)
- }
- return parseESResponse(body)
- }
- func (es *ESClient) Search(params *ESQueryRequest) (response ESResponse, err error) {
- queryMap := params.parseJsonQuery()
- jsonQuery, _ := json.Marshal(queryMap)
- logger.Info("查询语句: %s", string(jsonQuery))
- indexes := strings.Split(params.IndexName, ",")
- request := esapi.SearchRequest{
- Index: indexes,
- Body: strings.NewReader(string(jsonQuery)),
- From: ¶ms.From,
- Size: ¶ms.Size,
- Sort: params.Sorts,
- }
- res, err := request.Do(context.Background(), esClient.esOp)
- defer res.Body.Close()
- if err != nil {
- logger.Error("es查询失败: %s", err)
- }
- if res.IsError() {
- var e map[string]interface{}
- if err = json.NewDecoder(res.Body).Decode(&e); err != nil {
- logger.Error("解析es应答失败: %v", err)
- } else {
- logger.Error("es请求失败: %s: %v\n", res.Status(), e)
- }
- }
- body, err := io.ReadAll(res.Body)
- if err != nil {
- logger.Error("获取es应答失败: %v", err)
- }
- return parseESResponse(body)
- }
- func parseESResponse(body []byte) (ESResponse, error) {
- var response ESResponse
- if err := json.Unmarshal(body, &response); err != nil {
- return ESResponse{}, err
- }
- for _, hit := range response.Hits.Hits {
- var source map[string]interface{}
- if err := json.Unmarshal(hit.Source, &source); err != nil {
- return ESResponse{}, err
- }
- }
- return response, nil
- }
- func (es *ESClient) GetSource(hits Hits) []Hit {
- return hits.Hits
- }
- func (es *ESClient) GetCount(hits Hits) []Hit {
- return hits.Hits
- }
- // /*
- // *
- // 添加es
- // indexName 索引名
- // id es的id
- // body es的值
- // */
- func (es *ESClient) Update(indexName string, id int, doc interface{}) bool {
- jsonUpdate := map[string]interface{}{
- "doc": doc,
- }
- jsonDoc, _ := json.Marshal(jsonUpdate)
- logger.Info("查询语句: %s", string(jsonDoc))
- req := esapi.UpdateRequest{
- Index: indexName,
- DocumentID: strconv.Itoa(id),
- Body: strings.NewReader(string(jsonDoc)),
- Refresh: "true",
- }
- res, err := req.Do(context.Background(), es.es())
- defer res.Body.Close()
- if err != nil {
- logger.Error("es查询失败: %s", err)
- }
- if res.IsError() {
- var e map[string]interface{}
- if err = json.NewDecoder(res.Body).Decode(&e); err != nil {
- logger.Error("解析es应答失败: %v", err)
- } else {
- logger.Error("es请求失败: %s: %v\n", res.Status(), e)
- }
- }
- body, err := io.ReadAll(res.Body)
- if err != nil {
- logger.Error("获取es应答失败: %v", err)
- }
- fmt.Printf("%s\n", string(body))
- return true
- }
- func (es *ESClient) InsertOrUpdate(indexName string, id int, doc interface{}) (success bool) {
- if exist, existErr := es.Exist(indexName, id); existErr == nil && exist {
- return es.Update(indexName, id, doc)
- } else {
- return es.CreateDocument(indexName, id, doc)
- }
- }
- // Delete *
- // 删除
- // indexName 索引名
- // id es的id
- // */
- func (es *ESClient) Delete(indexName string, query interface{}) bool {
- jsonQuery, _ := json.Marshal(query)
- refresh := new(bool)
- *refresh = true
- logger.Info("查询语句: %s", string(jsonQuery))
- req := esapi.DeleteByQueryRequest{
- Index: []string{indexName},
- Body: strings.NewReader(string(jsonQuery)),
- Refresh: refresh,
- }
- res, err := req.Do(context.Background(), es.es())
- defer res.Body.Close()
- if err != nil {
- logger.Error("es查询失败: %s", err)
- }
- if res.IsError() {
- var e map[string]interface{}
- if err = json.NewDecoder(res.Body).Decode(&e); err != nil {
- logger.Error("解析es应答失败: %v", err)
- } else {
- logger.Error("es请求失败: %s: %v\n", res.Status(), e)
- }
- }
- body, err := io.ReadAll(res.Body)
- if err != nil {
- logger.Error("获取es应答失败: %v", err)
- }
- fmt.Printf("%s\n", string(body))
- return true
- }
- func (es *ESClient) Exist(indexName string, docId int) (exist bool, 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 {
- // 如果文档存在
- logger.Info("doc存在")
- return true, nil
- }
- }
- 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.
- // Create(indexName).
- // Do(context.Background())
- // if err != nil {
- // logger.Error("创建ES索引失败:%v", err)
- // return err
- // }
- // fmt.Printf("index:%#v\n", resp.Index)
- // return nil
- //}
- // DeleteIndex 删除索引
- //
- // func DeleteIndex(indexName string) error {
- // _, err := esClient.es().Indices. // 表明是对索引的操作,而Index则表示是要操作具体索引下的文档
- // Delete(indexName).
- // Do(context.Background())
- // if err != nil {
- // fmt.Printf("delete index failed,err:%v\n", err)
- // return err
- // }
- // fmt.Printf("delete index successed,indexName:%s", indexName)
- // return nil
- // }
- //
- // CreateDocument 创建文档
- func (es *ESClient) CreateDocument(indexName string, id int, doc interface{}) (success bool) {
- jsonDoc, _ := json.Marshal(doc)
- logger.Info("查询语句: %s", string(jsonDoc))
- // 添加文档
- indexRequest := esapi.IndexRequest{
- Index: indexName,
- DocumentID: strconv.Itoa(id),
- Body: strings.NewReader(string(jsonDoc)),
- Refresh: "true",
- }
- // 执行请求
- res, err := indexRequest.Do(context.Background(), es.es())
- if err != nil {
- logger.Error("ES创建文档失败: %s", err)
- return false
- }
- defer res.Body.Close()
- // 检查文档是否成功创建
- if res.IsError() {
- var e map[string]interface{}
- if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
- logger.Error("解析ES应答失败: %s", err)
- } else {
- // Print the response status and error information.
- logger.Error("[%s] %s: %s\n", res.Status(), e["error"].(map[string]interface{})["类型"], e["错误"].(map[string]interface{})["原因"])
- }
- return false
- } else {
- // 如果文档成功创建
- logger.Info("创建文档成功")
- return true
- }
- }
|