Golang连接elasticsearch


本文摘自网络,作者,侵删。


title: "Golang连接elasticsearch"
date: 2021-02-10T21:41:32+08:00
draft: true
tags: ['go','elasticsearch']
author: "dadigang"
author_cn: "大地缸"
personal: "http://www.real007.cn"


关于作者

http://www.real007.cn/about

golang连接elasticsearch

package main

import (
    "github.com/olivere/elastic"
    "fmt"
    "context"
)

type Tweet struct {
    User string
    Message string
    Retweets int64
}

func main() {
    client, err := elastic.NewClient(elastic.SetURL("http://192.168.33.134:9200"))
    if err != nil {
        // Handle error
    }
    fmt.Println(client)
    // Use the IndexExists service to check if a specified index exists.

    exists, err := client.IndexExists("twitter").Do(context.Background())
    if err != nil {
        // Handle error
        panic(err)
    }
    if !exists {
        // Create a new index.
        mapping := `
{
    "settings":{
        "number_of_shards":1,
        "number_of_replicas":0
    },
    "mappings":{
        "doc":{
            "properties":{
                "user":{
                    "type":"keyword"
                },
                "message":{
                    "type":"text",
                    "store": true,
                    "fielddata": true
                },
            "retweets":{
                "type":"long"
            },
                "tags":{
                    "type":"keyword"
                },
                "location":{
                    "type":"geo_point"
                },
                "suggest_field":{
                    "type":"completion"
                }
            }
        }
    }
}
`
        createIndex, err := client.CreateIndex("twitter").Body(mapping).Do(context.Background())
        if err != nil {
            // Handle error
            panic(err)
        }
        if !createIndex.Acknowledged {
            // Not acknowledged
        }
    }

    // Index a tweet (using JSON serialization)
    tweet1 := Tweet{User: "olivere", Message: "Take Five", Retweets: 0}
    put1, err := client.Index().
        Index("twitter").
        Type("doc").
        Id("1").
        BodyJson(tweet1).
        Do(context.Background())
    if err != nil {
        // Handle error
        panic(err)
    }
    fmt.Printf("Indexed tweet %s to index %s, type %s\n", put1.Id, put1.Index, put1.Type)

    // Index a second tweet (by string)
    tweet2 := `{"user" : "olivere", "message" : "It's a Raggy Waltz"}`
    put2, err := client.Index().
        Index("twitter").
        Type("doc").
        Id("2").
        BodyString(tweet2).
        Do(context.Background())
    if err != nil {
        // Handle error
        panic(err)
    }
    fmt.Printf("Indexed tweet %s to index %s, type %s\n", put2.Id, put2.Index, put2.Type)

    // Get tweet with specified ID
    get1, err := client.Get().
        Index("twitter").
        Type("doc").
        Id("1").
        Do(context.Background())
    fmt.Println(get1)
    if err != nil {
        switch {
        case elastic.IsNotFound(err):
            panic(fmt.Sprintf("Document not found: %v", err))
        case elastic.IsTimeout(err):
            panic(fmt.Sprintf("Timeout retrieving document: %v", err))
        case elastic.IsConnErr(err):
            panic(fmt.Sprintf("Connection problem: %v", err))
        default:
            // Some other kind of error
            panic(err)
        }
    }

}


本文来自:简书

感谢作者:大地缸

查看原文:Golang连接elasticsearch

相关阅读 >>

如何利用Go-zero在Go中快速实现jwt认证

Go语言中包的使用

Go - 环境安装

Golang 开发的 web 有哪些框架?

Golang byte是什么

Go timer 是如何被调度的?

分享一款Golang style语法的Golang orm库

Golang学习笔记for循环语句

利用Golang反射机制(reflect)搭建本地leetcode调试器

一周 Go world 新鲜事

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...