乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      go工具方法-elasticsearch

       印度阿三17 2021-01-15
      package es

      import (
      "context"
      "fmt"
      "github.com/olivere/elastic"
      "go-pkg/pkg/cfg"
      )
      //可參考https://github.com/ashion89/go-pkg/tree/master/pkg/es中測(cè)試
      var client *elastic.Client
      var ctx = context.Background()
      var config = cfg.GetConfig()


      //初始化es
      func Init() error {
      var url = config.Es.Url
      var user = config.Es.User
      var password = config.Es.Password

      var err error
      client, err = elastic.NewClient(
      elastic.SetURL(url),
      elastic.SetBasicAuth(user, password),
      elastic.SetSniff(false),
      )
      if err != nil {
      fmt.Println("NewClient err ",err)
      return err
      }
      info, code, err := client.Ping(url).Do(ctx)
      if err != nil {
      fmt.Println("Ping err ",err)
      return err
      }

      if code == 200 {
      fmt.Printf("connected to es: %s ,version: %s \n", info.ClusterName, info.Version.Number)
      }
      return nil
      }

      //獲取連接
      func getClient() *elastic.Client {
      if client != nil {
      return client
      } else {
      err := Init()
      fmt.Println("init err ",err)
      return client
      }
      }



      //添加數(shù)據(jù)到es
      import (
      "encoding/json"
      "errors"
      "fmt"
      "github.com/olivere/elastic"
      "go-pkg/pkg/util"
      "strconv"
      )

      const EsStdIdxCount = 10
      const EsMsgIdxName = "message_"
      const ALIASE = "_aliase" //別名
      const TYPE = "_doc"

      //account分庫(kù)
      func AddMessage(message map[string]interface{}, account string) error {
      idx := util.GetHashCode(account, EsStdIdxCount)
      id, _ := message["_id"].(string)
      delete(message, "_id")
      esClient := getClient()
      if esClient != nil {
      _, err := esClient.Index().Index(EsMsgIdxName strconv.Itoa(idx) ALIASE).Type(TYPE).Routing(account).Id(id).BodyJson(message).Do(ctx)
      if err != nil {

      return err
      }
      return nil
      } else {

      return errors.New("es client context deadlineexceeded")
      }
      }

      //查詢數(shù)據(jù)
      func SearchMessageAll(account string, msgContent string) (data []map[string]interface{}, err error) {
      idx := util.GetHashCode(account, EsStdIdxCount)
      qesMatch := elastic.NewMatchPhraseQuery("name", msgContent).Analyzer("standard") //匹配內(nèi)容
      query := elastic.NewBoolQuery().Must(qesMatch)

      searchResult, err := getClient().Search().TrackTotalHits(false).
      Index(EsMsgIdxName strconv.Itoa(idx) ALIASE).
      Type(TYPE).Routing(account).
      Query(query).
      From(0).Size(1000).
      Do(ctx)
      if err != nil {
      fmt.Printf("-----search doc from es err : %v ", err)
      return
      }
      for _, hit := range searchResult.Hits.Hits {
      var t map[string]interface{}
      err := json.Unmarshal(*hit.Source, &t)
      if err != nil {
      fmt.Printf("search doc es Unmarshal err: %v ", err)
      }
      data = append(data, t)
      }
      return data, nil
      }
      //util中方法
      package util
      import (
      "hash/crc32"
      )
      func GetHashCode(str string, count int) int {
      v := crc32.ChecksumIEEE([]byte(str))
      if v < 0 {
      v = -v
      }
      return int(v) % count
      }


      來(lái)源:https://www./content-4-822401.html

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多