es6 版本的golang-sdk问题处理

ElasticSearch6版本的Golang SDK问题处理.

golang-sdk

es_version: 6.4.3

官方建议的sdk版本: https://github.com/olivere/elastic

注意:很好奇,作者其他版本都采用vN来标记版本,但是在v6的时候直接采用github版本

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
➜   go run test-es.go
# command-line-arguments
./test-es.go:198:33: invalid operation: searchResult.Hits.TotalHits > 0 (mismatched types *elastic.TotalHits and int)
./test-es.go:207:26: invalid indirect of hit.Source (type json.RawMessage)

# 第一个问题其实是因为新版的searchResult.Hits.TotalHits结构变化了(https://godoc.org/github.com/olivere/elastic#TotalHits)
# 当然也可以直接使用SearchResult的TotalHits()方法获取数据总条数
# 可以看这个SearchResult结构体
# https://godoc.org/github.com/olivere/elastic#SearchResult

将198行改为
if searchResult.TotalHits() > 0 {
或者
if searchResult.Hits.TotalHits.Value > 0 {
即可.建议改成第一种,因为解决后面的问题,依然不支持第二种


# 第二个问题是将hit.Source转换成json.RawMessage的时候异常
我们可以先把完整的es查询的searchResult打印出来
注释掉205-218行,同时将'encoding/json'先注释掉


➜  20190812-16 go run test-es.go
Elasticsearch returned with code 200 and version 6.4.3
Elasticsearch version 6.4.3
Indexed tweet 1 to index twitter, type tweet
Indexed tweet 2 to index twitter, type tweet
Got document 1 in version 824633831736 from index twitter, type tweet
panic: json: cannot unmarshal number into Go struct field SearchHits.total of type elastic.TotalHits

goroutine 1 [running]:
main.main()
	/Users/xuebiaoxu/Desktop/soul-docs/articles/20190812-16/test-es.go:177 +0x1f94
exit status 2

# wo call,发现此时连上es了,也写入了一些数据,但是在查询的时候出现问题
也就是在做termquery 的时候异常了,但是从官方的结构定义中并未发现相关异常
  termQuery := elastic.NewTermQuery("user", "olivere")
  searchResult, err := client.Search().
    Index("twitter").   // search in index "twitter"
    Query(termQuery).   // specify the query
    Sort("user", true). // sort by "user" field, ascending
    From(0).Size(10).   // take documents 0-9
    Pretty(true).       // pretty print request and response JSON
    Do(ctx)             // execute

# 瞎几把看了半天,最终看到官方的github上建议使用显示的版本来使用v6
即:
"github.com/olivere/elastic" 改为
"gopkg.in/olivere/elastic.v6"

➜  20190812-16 go run test-es.go
Elasticsearch returned with code 200 and version 6.4.3
Elasticsearch version 6.4.3
Indexed tweet 1 to index twitter, type tweet
Indexed tweet 2 to index twitter, type tweet
Got document 1 in version 824635992960 from index twitter, type tweet
Query took 15 milliseconds
Tweet by olivere: Take Five
Tweet by olivere: It's a Raggy Waltz
Found a total of 2 tweets
Found a total of 2 tweets
[0xc0002a0000 0xc0002a00e0]
New version of tweet "1" is now 4

# 终于使用es6的golang-sdk成功对es数据进行读写了,真几把恶心啊
curl -s http://172.29.202.140:9200/twitter/tweet/_search\?q\=five | python -m json.tool
{
    "_shards": {
        "failed": 0,
        "skipped": 0,
        "successful": 1,
        "total": 1
    },
    "hits": {
        "hits": [
            {
                "_id": "1",
                "_index": "twitter",
                "_score": 0.52354836,
                "_source": {
                    "created": "0001-01-01T00:00:00Z",
                    "message": "Take Five",
                    "retweets": 1,
                    "user": "olivere"
                },
                "_type": "tweet"
            }
        ],
        "max_score": 0.52354836,
        "total": 1
    },
    "timed_out": false,
    "took": 16
}


# 此时再把json.RawMessage那块去掉注释
# 取消205-218行,和import中的'encoding/json'的注释
➜  20190812-16 go run test-es.go
Elasticsearch returned with code 200 and version 6.4.3
Elasticsearch version 6.4.3
Indexed tweet 1 to index twitter, type tweet
Indexed tweet 2 to index twitter, type tweet
Got document 1 in version 824635991880 from index twitter, type tweet
Query took 8 milliseconds
Tweet by olivere: Take Five
Tweet by olivere: It's a Raggy Waltz
Found a total of 2 tweets
Found a total of 2 tweets
[0xc0002a4000 0xc0002a40e0]
## 这就是循环遍历出来的检索的内容
Tweet by olivere: Take Five
Tweet by olivere: It's a Raggy Waltz
New version of tweet "1" is now 6

知识星球

公众号