golang基礎--gopkg.in/olivere/elastic.v 5学習二(添削改査document)
14416 ワード
文書ディレクトリ
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方式
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
", 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$
BodysString方式
もちろん、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
", put2.Id, put2.Index, put2.Type)
}
Twitterの下のすべてのdocumentデータを再度調べてみると、もう1つ追加されています.
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$
documentの取得
コードによる取得
また、ある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
", 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$
クエリーdocument
SearchResult.Each
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
", searchResult.TookInMillis)
var ttyp Tweet
for _, item := range searchResult.Each(reflect.TypeOf(ttyp)) {
t := item.(Tweet)
fmt.Printf("Tweet by %s: %s
", t.User, t.Message)
}
fmt.Printf("Found a total of %d tweets
", 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
}
ここを通ってHits.TotalHits
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
", 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
", t.User, t.Message)
}
} else {
// No hits
fmt.Print("Found no tweets
")
}
}
結果は次のように出力されます.
Found a total of 2 tweets
Tweet by olivere: It's a Raggy Waltz
Tweet by olivere: Take Five
documentの削除
次に、ID=1のdocument削除結果をDeleteResponseで削除する.Found判定でOK
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
")
}
}
調べてみると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
documentの更新
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
", 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$