美文网首页
golang elasticsearch 索引操作API ---

golang elasticsearch 索引操作API ---

作者: 一位先生_ | 来源:发表于2022-05-19 01:22 被阅读0次

    创建索引

    // 创建ES client
    client, err := elastic.NewClient()
    if err != nil {
        // Handle error
        panic(err)
    }
    
    // 执行ES请求需要提供一个上下文对象
    ctx := context.Background()
    
    // 索引mapping定义,这里仿微博消息结构定义
    const mapping = `
    {
      "mappings": {
        "properties": {
          "user": {
            "type": "keyword"
          },
          "message": {
            "type": "text"
          },
          "image": {
            "type": "keyword"
          },
          "created": {
            "type": "date"
          },
          "tags": {
            "type": "keyword"
          },
          "location": {
            "type": "geo_point"
          },
          "suggest_field": {
            "type": "completion"
          }
        }
      }
    }`
    // 创建索引
    _, err = client.CreateIndex("weibo").BodyString(mapping).Do(ctx)
    if err != nil {
        // Handle error
        panic(err)
    }
    

    删除索引
    删除blog索引

    client.DeleteIndex("blog").Do(ctx)
    

    检测索引是否存在
    // 检测下weibo索引是否存在

    exists, err := client.IndexExists("weibo").Do(ctx)
    if err != nil {
        // Handle error
        panic(err)
    }
    

    相关文章

      网友评论

          本文标题:golang elasticsearch 索引操作API ---

          本文链接:https://www.haomeiwen.com/subject/dnmxsrtx.html