Golang操作elasticsearch7
- 包:github.com/olivere/elastic/v7
-
elasticsearch版本7.6
首先能够定义一个全量,构建连接池var esCli *elastic.Client func init() { var err error esCli, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetURL("http://127.0.0.1:9200") if err != nil { panic(err) } }
应用办法
1、增加
func EsAdd(es_index string, es_body interface{}) string {
var id string
rsAdd, err := esCli.Index().
Index(es_index).
BodyJson(es_body).
Do(context.Background())
if err != nil {
panic(err)
} else {
id = rsAdd.Id
}
return id
}
PS:id是指ES主动生成的id,如果想自定义id能够在Index()前面加.Id(string)。目前用的是es7.6,所以能够不必加Type默认_doc,
也能够指定办法Type()。
2、批量增加
func EsAddBulk(es_index string, es_body []interface{}) {
bulkRequest := esCli.Bulk()
for _, v := range es_body {
tmp := v
req := elastic.NewBulkIndexRequest().Index(es_index).Doc(tmp)
bulkRequest = bulkRequest.Add(req)
}
_, err := bulkRequest.Do(context.Background())
if err != nil {
panic(err)
}
}
3、增加
func EsUpdate(es_index string, es_id string, es_body interface{}) {
_, err := esCli.Update().
Index(es_index).
Id(es_id).
Doc(es_body).
Do(context.Background())
if err != nil {
panic(err)
}
}
4、批量批改
func EsUpdateBulk(es_index string, es_body []map[string]string) {
bulkRequest := esCli.Bulk()
for _, v := range es_body {
tmp := v
tmps := make(map[string]string)
for is, vs := range tmp {
if is != "id" {
tmps[is] = vs
}
}
req := elastic.NewBulkUpdateRequest().Index(es_index).Id(tmp["id"]).Doc(tmps).DocAsUpsert(true)
bulkRequest = bulkRequest.Add(req)
}
_, err := bulkRequest.Do(context.Background())
if err != nil {
panic(err)
}
}
PS:批量批改和批量减少办法是不同的,批量批改的办法NewBulkUpdateRequest,要调用这个才有DocAsUpsert。
之前在NewBulkIndexRequest下始终没有找到DocAsUpsert。
另外用[]map[string]string是因为每个数据要批改的key可能不同,如果要批改的key雷同能够用interface 或 struct。
5、删除文档
func Delete(es_index string, id string) {
_, err := esCli.Delete().Index(es_index).Id(id).Do(context.Background())
if err != nil {
panic(err)
}
}
6、查问以后index是否存在
func ExistsIndex(es_index string) bool {
exists, err := esCli.IndexExists(es_index).Do(context.Background())
if err != nil {
panic(err)
}
return exists
}
7、查问
func Search(es_index string,page int ,li int) interface{} {
p := (page - 1) * li
collapsedata := elastic.NewCollapseBuilder("company")
esq := elastic.NewBoolQuery()
esq.Must(elastic.NewMatchPhraseQuery("company", "CO LTD."))
esq.Must(elastic.NewMatchQuery("country", "US"))
search := esCli.Search().
Index(es_index).
From(p).Size(li).
Query(esq).
Collapse(collapsedata).
Pretty(true)
searchResult, err := search.Do(context.Background())
if err != nil {
panic(err)
} else {
return searchResult
}
}
PS:NewMatchPhraseQuery是代表短语的匹配,Collapse是指定字段雷同后果的折叠
查找延长:
像下面的后果返回的是es所有的参数,有时候只想用_Souce那要在把外面的后果抽取进去。官网也配有办法:
func PrintTradedata(res *elastic.SearchResult, err error) tradedata {
if err != nil {
panic(err)
}
var typ tradedata
var re tradedata
for _, item := range res.Each(reflect.TypeOf(typ)) {
te := item.(tradedata)
jsonValue, _ := json.Marshal(te)
json.Unmarshal(jsonValue, &re)
}
return re
}
然而这种的话,也有肯定的限制性就是如果我还想要_id的话可能又要本人在循环一边构造获取。所以我去看来官网的办法,而后本人从新写一遍:
func PrintTradedata(res *elastic.SearchResult, err error) tradedata {
if err != nil {
panic(err)
}
var re tradedata
for _, item := range res.Hits.Hits {
if item.Source != nil {
jsonValue, _ := json.Marshal(item.Source)
json.Unmarshal(jsonValue, &re)
re.ID = item.Id
}
}
return re
}
这样就能提取出es的_id和_source的联合,可能感觉就这样几行为什么官网要封装多一个办法,
其实官网封装的办法还做构造的验证安全性要比我的高,如果只有_source举荐还是用官网的。
以上就是一些golang应用es7的根本办法,低的版本也实用不过要加上type。es最弱小的还是它的搜寻性能我只是写了根底的,应用bool查问,还有term、math、fuzzy、wildcard等等,还有翻页scroll和search after,大家都能够去elastic包中查看。如果有须要,等有空我也能够写一篇专门对于搜寻的用法。
上述内容欢送大家斧正和交换。
发表回复