go调用olivere包操作elasticsearch的常用基本操作

go调用olivere包操作elasticsearch的常用基本操作

不想看下面啰嗦的直接看官方文档:点这儿

完整示例代码:

package main

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

type User struct {
   
	Name    string    `json:"name"`
	Age     int       `json:"age"`
	Married bool      `json:"married"`
	Sex string        `json:"sex"`
	Created time.Time `json:"created, omitempty"`
	Tags     []string              `json:"tags,omitempty"`
	Location string                `json:"location,omitempty"`
	Suggest  *elastic.SuggestField `json:"suggest_field,omitempty"`
}

// 定义一些变量,mapping为定制的index字段类型
const mapping = `
{
	"settings":{
		"number_of_shards": 1,
		"number_of_replicas": 0
	},
	"mappings":{
			"properties":{
				"name":{
					"type":"text"
				},
				"age":{
					"type":"long"
				},
				"married":{
				"type":"boolean"
				},
				"created":{
					"type":"date"
				},
				"tags":{
					"type":"keyword"
				},
				"location":{
					"type":"geo_point"
				},
				"suggest_field":{
					"type":"completion"
				}
		}
	}
}`
const mapping1 = `
{
  "properties": {
    "sex": {
      "type": "text"
    }
  }
}
}`
var ctx = context.Background()
var esUrl string = "http://192.168.1.109:9200"

func main() {
   

	//连接客户端
	client, err := elastic.NewClient(elastic.SetURL(esUrl), elastic.SetSniff(false))
	if err != nil {
   
		// Handle error
		panic(err)
	}
	// Ping the Elasticsearch server to get e.g. the version number
	// ping通服务端,并获得服务端的es版本,本实例的es版本为version 7.6.1
	info, code, err := client.Ping(esUrl).Do(ctx)
	if err != nil {
   
		// Handle error
		panic(err)
	}
	fmt.Printf("Elasticsearch returned with code>: %d and version %s\n", code, info.Version.Number)
	// 获取版本号的直接API
	esVersion, err := client.ElasticsearchVersion(esUrl)
	if err != nil {
   
		panic(err)
	}
	fmt.Printf("es的版本为%s\n", esVersion)
	// 创建index前,先查看es引擎中是否存在自己想要创建的索引index
	//exists, err := client.IndexExists("user").Do(ctx)
	//if err != nil {
   
	//	panic(err)
	//}
	//if !exists {
   
	//	// 如果不存在,就创建
	//	createIndex, err := client.CreateIndex("user").BodyString(mapping1).Do(ctx)
	//	if err != nil {
   
	//		// Handle error
	//		panic(err)
	//	}
	//	if !createIndex.Acknowledged {
   
	//		// Not acknowledged ,创建失败
	//	}
	////
	//}
	//// 为已有的索引添加字段
	//
	//_, err = client.PutMapping().Index("user").BodyString(mapping1).Do(ctx)
	//if err != nil {
   
	//	fmt.Println(err)
	//	panic(err)
	//}

	// 添加文档方法1
	//user1 := User{Name:"bob",Sex:"male",Married:false,Age:23}
	//put1,err :=client.Index().Index("user").BodyJson(user1).Id("1").Do(ctx)
	//if err != nil{
   
	//	panic(err)
	//}
	//fmt.Printf("Indexed user %s to index %s, type %s\n", put1.Id, put1.Index, put1.Type) //Indexed user 1 to index user, type _doc

	//// 添加文档方法2
	//user2 := `{"name":"mike","sex":"male","married":true,"age":"22"}`
	//put2, err := client.Index().Index("user").BodyString(user2).Do(ctx)// 不指定id
	//if err != nil{
   
	//	panic(err)
	//}
	//fmt.Printf("Indexed user %s to index %s, type %s\n", put2.Id, put2.Index, put2.Type)//Indexed user 4-K2wXIB33YuyEzPYoAi to index user, type _doc

	// 查询
	get1, err := client.Get().Index
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值