type employee struct { FirstName string`json:"first_name"` LastName string`json:"last_name"` Age int`json:"age"` About string`json:"about"` Interests []string`json:"interests"` }
var client *elastic.Client var host = "http://ip:9200/"
funcmain() {
}
// go init函数不能被其他函数调用,而是在main函数执行之前,自动被调用 funcinit() { errorLog := log.New(os.Stdout, "APP", log.LstdFlags) client, err := elastic.NewClient(elastic.SetErrorLog(errorLog), elastic.SetURL(host), elastic.SetSniff(false)) if err != nil { log.Fatal(err) } pingResult, code, err := client.Ping(host).Do(context.Background()) if err != nil { log.Fatal(err) } fmt.Printf("Elasticsearch returned with code %d and version %s\n", code, pingResult.Version.Number) version, err := client.ElasticsearchVersion(host) if err != nil { log.Fatal(err) } fmt.Printf("Elasticsearch version %s\n", version) }
// BodyJson is the document as a serializable JSON interface. func(s *IndexService)BodyJson(body interface{}) *IndexService { s.bodyJson = body return s }
// BodyString is the document encoded as a string. func(s *IndexService)BodyString(body string) *IndexService { s.bodyString = body return s }
type IndexService struct { ... bodyJson interface{} bodyString string }
type employee struct { FirstName string`json:"first_name"` LastName string`json:"last_name"` Age int`json:"age"` About string`json:"about"` Interests []string`json:"interests"` }
var client *elastic.Client var host = "http://ip :9200" var ctx = context.Background() // 执行ES请求需要提供一个上下文对象
funcmain() { create() }
// go init函数不能被其他函数调用,而是在main函数执行之前,自动被调用 funcinit() { errorLog := log.New(os.Stdout, "APP", log.LstdFlags) var err error client, err = elastic.NewClient(elastic.SetErrorLog(errorLog), elastic.SetURL(host), elastic.SetSniff(false)) if err != nil { log.Fatal(err) } }
type employee struct { FirstName string`json:"first_name"` LastName string`json:"last_name"` Age int`json:"age"` About string`json:"about"` Interests []string`json:"interests"` }
var client *elastic.Client var host = "http://ip:9200" var ctx = context.Background() // 执行ES请求需要提供一个上下文对象
type employee struct { FirstName string`json:"first_name"` LastName string`json:"last_name"` Age int`json:"age"` About string`json:"about"` Interests []string`json:"interests"` }
var client *elastic.Client var host = "http://ip:9200" var ctx = context.Background() // 执行ES请求需要提供一个上下文对象
type employee struct { FirstName string`json:"first_name"` LastName string`json:"last_name"` Age int`json:"age"` About string`json:"about"` Interests []string`json:"interests"` }
var client *elastic.Client var host = "http://ip:9200" var ctx = context.Background() // 执行ES请求需要提供一个上下文对象
funcmain() { gets() }
funcgets() { // 通过id查找 getResult, err := client.Get().Index("hx").Type("employee").Id("2").Do(ctx) if err != nil { log.Fatal(err) } if getResult.Found { fmt.Printf("Got document %s in version %d from index %s, type %s\n", getResult.Id, getResult.Version, getResult.Index, getResult.Type) } }
// go init函数不能被其他函数调用,而是在main函数执行之前,自动被调用 funcinit() { errorLog := log.New(os.Stdout, "APP", log.LstdFlags) var err error client, err = elastic.NewClient(elastic.SetErrorLog(errorLog), elastic.SetURL(host), elastic.SetSniff(false)) if err != nil { log.Fatal(err) } }
type employee struct { FirstName string`json:"first_name"` LastName string`json:"last_name"` Age int`json:"age"` About string`json:"about"` Interests []string`json:"interests"` }
var client *elastic.Client var host = "http://ip:9200" var ctx = context.Background() // 执行ES请求需要提供一个上下文对象
// 打印查询到的employee funcprintEmployee(result *elastic.SearchResult) { var typ employee for _, item := range result.Each(reflect.TypeOf(typ)) { // 从搜索结果中取数据的方法 emp, ok := item.(employee) if ok { fmt.Println(emp) } else { fmt.Println("断言失败!") } } fmt.Println() // 换行 }
// go init函数不能被其他函数调用,而是在main函数执行之前,自动被调用 funcinit() { errorLog := log.New(os.Stdout, "APP", log.LstdFlags) var err error client, err = elastic.NewClient(elastic.SetErrorLog(errorLog), elastic.SetURL(host), elastic.SetSniff(false)) if err != nil { log.Fatal(err) } }
type employee struct { FirstName string`json:"first_name"` LastName string`json:"last_name"` Age int`json:"age"` About string`json:"about"` Interests []string`json:"interests"` }
var client *elastic.Client var host = "http://ip:9200" var ctx = context.Background() // 执行ES请求需要提供一个上下文对象
// 打印查询到的employee funcprintEmployee(result *elastic.SearchResult) { var typ employee for _, item := range result.Each(reflect.TypeOf(typ)) { // 从搜索结果中取数据的方法 emp, ok := item.(employee) if ok { fmt.Println(emp) } else { fmt.Println("断言失败!") } } fmt.Println() // 换行 }
// go init函数不能被其他函数调用,而是在main函数执行之前,自动被调用 funcinit() { errorLog := log.New(os.Stdout, "APP", log.LstdFlags) var err error client, err = elastic.NewClient(elastic.SetErrorLog(errorLog), elastic.SetURL(host), elastic.SetSniff(false)) if err != nil { log.Fatal(err) } }