olivere/elastic.v5
来链接es,然后如何创建、删除索引
在学习之前,我们先通过 curl -X GET 'http://localhost:9200/_cat/indices?v'
来列出所有的index
zhiliaodeMBP:Desktop zhiliao$ curl -X GET 'http://localhost:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open index_safly -l4j2UPpTgu64jsk7jzUvA 5 1 1 0 4.4kb 4.4kb
yellow open index_1 np0ouRCDRC-eXDrilpSFjQ 5 1 3 0 11.8kb 11.8kb
yellow open index_temp qS7IWYm-ROOFBjnNgVCE4w 5 1 1 0 5.4kb 5.4kb
yellow open qrelations pemnB8e-SumcYVNQRyYNAA 5 1 88736 6301 14.3mb 14.3mb
yellow open qa v6avnFsfSResWMs0LcAXdQ 5 1 8 0 66.7kb 66.7kb
yellow open weather qFUsE8OiSmKzaXmj8TjIOA 5 1 0 0 955b 955b
yellow open question E6iJvA5RTpq1LnJQr748dQ 5 1 66974 8169 41.9mb 41.9mb
yellow open index_2 pgyxlcW9QHORddZeLNGeDg 5 1 2 0 8.2kb 8.2kb
yellow open my-index wNnhnRtNTkaVziWKU_7_mA 5 1 0 0 955b 955b
yellow open weather1 -l8fiEyNRD2gt1JpdlR2Ug 5 1 0 0 810b 810b
zhiliaodeMBP:Desktop zhiliao$
我们通过BodyJson方法,来创建一个Index为twitter,Type为tweet,ID为1的document,以下是代码
package main
import (
"fmt"
"gopkg.in/olivere/elastic.v5"
"log"
"os"
"time"
"context"
)
var host = "http://127.0.0.1:9200/"
func connect() *elastic.Client {
client, err := elastic.NewClient(
elastic.SetURL(host),
elastic.SetSniff(false),
elastic.SetHealthcheckInterval(10*time.Second),
elastic.SetGzip(true),
elastic.SetErrorLog(log.New(os.Stderr, "ELASTIC ", log.LstdFlags)),
elastic.SetInfoLog(log.New(os.Stdout, "", log.LstdFlags)))
if err != nil {
panic(err)
}
return client
}
type Tweet struct {
User string `json:"user"`
Message string `json:"message"`
Retweets int `json:"retweets"`
Image string `json:"image,omitempty"`
Created time.Time `json:"created,omitempty"`
Tags []string `json:"tags,omitempty"`
Location string `json:"location,omitempty"`
Suggest *elastic.SuggestField `json:"suggest_field,omitempty"`
}
func main() {
client := connect()
fmt.Println("client", client)
ctx := context.Background()
tweet1 := Tweet{User: "olivere", Message: "Take Five", Retweets: 0}
put1, err := client.Index().
Index("twitter").
Type("tweet").
Id("1").
BodyJson(tweet1).
Do(ctx)
if err != nil {
// Handle error
panic(err)
}
fmt.Printf("Indexed tweet %s to index %s, type %s\n", put1.Id, put1.Index, put1.Type)
}
我们通过curl -X GET 'http://localhost:9200/twitter/tweet/_search?pretty=true'
来查询下所有的document数据
zhiliaodeMBP:Desktop zhiliao$ curl -X GET 'http://localhost:9200/twitter/tweet/_search?pretty=true'
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "twitter",
"_type" : "tweet",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"user" : "olivere",
"message" : "Take Five",
"retweets" : 0,
"created" : "0001-01-01T00:00:00Z"
}
}
]
}
}
zhiliaodeMBP:Desktop zhiliao$
当然我们也可以利用BodyString的方式进行创建,以下是代码
func main() {
client := connect()
fmt.Println("client", client)
ctx := context.Background()
tweet2 := `{"user" : "olivere", "message" : "It's a Raggy Waltz"}`
put2, err := client.Index().
Index("twitter").
Type("tweet").
Id("2").
BodyString(tweet2).
Do(ctx)
if err != nil {
// Handle error
panic(err)
}
fmt.Printf("Indexed tweet %s to index %s, type %s\n", put2.Id, put2.Index, put2.Type)
}
我们再次查询Twitter下的所有的document数据,发现已经新增了一条
zhiliaodeMBP:Desktop zhiliao$ curl -X GET 'http://localhost:9200/twitter/tweet/_search?pretty=true'
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "twitter",
"_type" : "tweet",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"user" : "olivere",
"message" : "It's a Raggy Waltz"
}
},
{
"_index" : "twitter",
"_type" : "tweet",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"user" : "olivere",
"message" : "Take Five",
"retweets" : 0,
"created" : "0001-01-01T00:00:00Z"
}
}
]
}
}
zhiliaodeMBP:Desktop zhiliao$
我们还可以获取某个index下的某条document数据
可以通过GetResult.Found
来判断是否存在
func main() {
client := connect()
fmt.Println("client", client)
ctx := context.Background()
get1, err := client.Get().
Index("twitter").
Type("tweet").
Id("1").
Do(ctx)
if err != nil {
// Handle error
panic(err)
}
if get1.Found {
fmt.Printf("Got document %s in version %d from index %s, type %s\n", get1.Id, get1.Version, get1.Index, get1.Type)
}
}
日志输出如下:
Got document 1 in version 824634337176 from index twitter, type tweet
我们还可以通过输入命令行获取某条document数据
zhiliaodeMBP:Desktop zhiliao$ curl 'localhost:9200/twitter/tweet/1?pretty=true'
{
"_index" : "twitter",
"_type" : "tweet",
"_id" : "1",
"_version" : 2,
"found" : true,
"_source" : {
"user" : "olivere",
"message" : "Take Five",
"retweets" : 0,
"created" : "0001-01-01T00:00:00Z"
}
}
zhiliaodeMBP:Desktop zhiliao$
我们将twitter索引下的2条数据全部查出来,通过elastic.NewTermQuery("user", "olivere")
条件
func main() {
client := connect()
fmt.Println("client", client)
ctx := context.Background()
termQuery := elastic.NewTermQuery("user", "olivere")
searchResult, err := client.Search().
Index("twitter"). // search in index "twitter"
Query(termQuery). // specify the query
From(0).Size(10). // take documents 0-9
Pretty(true). // pretty print request and response JSON
Do(ctx) // execute
if err != nil {
panic(err)
}
fmt.Printf("Query took %d milliseconds\n", searchResult.TookInMillis)
var ttyp Tweet
for _, item := range searchResult.Each(reflect.TypeOf(ttyp)) {
t := item.(Tweet)
fmt.Printf("Tweet by %s: %s\n", t.User, t.Message)
}
fmt.Printf("Found a total of %d tweets\n", searchResult.TotalHits())
}
看log日志输出如下:
Query took 1 milliseconds
Tweet by olivere: It's a Raggy Waltz
Tweet by olivere: Take Five
Found a total of 2 tweets
我们看下这个SearchResult的结构
// SearchResult is the result of a search in Elasticsearch.
type SearchResult struct {
TookInMillis int64 `json:"took"` // search time in milliseconds
ScrollId string `json:"_scroll_id"` // only used with Scroll operations
Hits *SearchHits `json:"hits"` // the actual search hits
Suggest SearchSuggest `json:"suggest"` // results from suggesters
Aggregations Aggregations `json:"aggregations"` // results from aggregations
TimedOut bool `json:"timed_out"` // true if the search timed out
Error *ErrorDetails `json:"error,omitempty"` // only used in MultiGet
Profile *SearchProfile `json:"profile,omitempty"` // profiling results, if optional Profile API was active for this search
Shards *shardsInfo `json:"_shards,omitempty"` // shard information
}
通过curl -X GET 'http://localhost:9200/twitter/tweet/_search?pretty=true'
命令我们还可以参考,通过判断searchResult.Hits.TotalHits
,来进行获取操作,以下是SearchHits结构体
// SearchHits specifies the list of search hits.
type SearchHits struct {
TotalHits int64 `json:"total"` // total number of hits found
MaxScore *float64 `json:"max_score"` // maximum score of all hits
Hits []*SearchHit `json:"hits"` // the actual hits returned
}
所以我们也可以通过如下代码进行获取
func main() {
client := connect()
fmt.Println("client", client)
ctx := context.Background()
termQuery := elastic.NewTermQuery("user", "olivere")
searchResult, err := client.Search().
Index("twitter"). // search in index "twitter"
Query(termQuery). // specify the query
From(0).Size(10). // take documents 0-9
Pretty(true). // pretty print request and response JSON
Do(ctx) // execute
if err != nil {
panic(err)
}
if searchResult.Hits.TotalHits > 0 {
fmt.Printf("Found a total of %d tweets\n", searchResult.Hits.TotalHits)
// Iterate through results
for _, hit := range searchResult.Hits.Hits {
// hit.Index contains the name of the index
// Deserialize hit.Source into a Tweet (could also be just a map[string]interface{}).
var t Tweet
err := json.Unmarshal(*hit.Source, &t)
if err != nil {
// Deserialization failed
}
// Work with tweet
fmt.Printf("Tweet by %s: %s\n", t.User, t.Message)
}
} else {
// No hits
fmt.Print("Found no tweets\n")
}
}
结果输出如下:
Found a total of 2 tweets
Tweet by olivere: It's a Raggy Waltz
Tweet by olivere: Take Five
接下来我们删掉某条ID=1 的document
删除结果通过DeleteResponse.Found判断即可
Found bool `json:"found,omitempty"`
func main() {
client := connect()
fmt.Println("client", client)
ctx := context.Background()
res, err := client.Delete().
Index("twitter").
Type("tweet").
Id("1").
Do(ctx)
if err != nil {
// Handle error
panic(err)
}
if res.Found {
fmt.Print("Document deleted from from index\n")
}
}
我们查询下结果,发现ID=1的被删掉了
zhiliaodeMBP:Desktop zhiliao$ curl -X GET 'http://localhost:9200/twitter/tweet/_search?pretty=true'
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "twitter",
"_type" : "tweet",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"user" : "olivere",
"message" : "It's a Raggy Waltz"
}
}
]
}
}
zhiliaodeMBP:Desktop zhiliao
func main() {
client := connect()
fmt.Println("client", client)
res, err := client.Update().Index("twitter").Type("tweet").
Id("2").Doc(map[string]interface{}{"user": "safly","message":"我是修改后的message"}).Do(context.Background())
if err != nil {
println(err.Error())
}
fmt.Printf("update age %s\n", res.Result)
}
我们再次查询发现已经被更新
zhiliaodeMBP:Desktop zhiliao$ curl -X GET 'http://localhost:9200/twitter/tweet/_search?pretty=true'
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "twitter",
"_type" : "tweet",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"user" : "safly",
"message" : "我是修改后的message"
}
}
]
}
}
zhiliaodeMBP:Desktop zhiliao$