-
Notifications
You must be signed in to change notification settings - Fork 2
/
v7.go
53 lines (45 loc) · 1.34 KB
/
v7.go
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
// 针对ElasticSearch V7.X 版本
package pelastic
import (
"context"
es7 "github.com/olivere/elastic/v7"
)
type Es7 struct {
conn *es7.Client
}
// IndexExists 检测指定index 是否存在
// indexName: string 索引名
func (es *Es7) IndexExists(indexName string) (bool, error) {
exist, err := es.conn.IndexExists(indexName).Do(context.Background())
return exist, err
}
// CreateIndex 创建索引
// indexName: string 索引名
// mappings: interface{} 指定索引映射
func (es *Es7) CreateIndex(indexName string, mappings string) (bool, error) {
createIndex, err := es.conn.CreateIndex(indexName).BodyString(mappings).Do(context.Background())
if err != nil || !createIndex.Acknowledged {
return false, err
}
return true, nil
}
// CreateDoc 创建文档
// indexName: string 索引名
// bodyContent: string
func (es *Es7) CreateDoc(indexName string, bodyContent string) (_id string, err error) {
res, err := es.conn.Index().Index(indexName).BodyString(bodyContent).Do(context.Background())
if err != nil {
return "", err
}
_id = res.Id
return _id, err
}
// DeleteIndex 删除索引
// indexNames: []string 索引名切片
func (es *Es7) DeleteIndex(indexNames []string) (bool, error) {
res, err := es.conn.DeleteIndex(indexNames...).Do(context.Background())
if err != nil || !res.Acknowledged {
return false, err
}
return true, nil
}