-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocument.go
More file actions
81 lines (73 loc) · 2.57 KB
/
document.go
File metadata and controls
81 lines (73 loc) · 2.57 KB
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
package esql
import (
"encoding/json"
"path"
)
// GetDocWithID twitter/_doc/0?_source=false
func (c *Client) GetDocWithID(id string) *Client {
if checkIndexName(c) {
return c
}
c.hostDB.Path = path.Join(c.hostDB.Path, "_doc", id)
return c.exec(c.hostDB.String())
}
// UpdateDoc adds or updates a typed JSON document in a specific index, making it searchable
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-index_.html
func (c *Client) UpdateDoc(id string, i interface{}) *Client {
if checkIndexName(c) {
return c
}
data, _ := json.Marshal(i)
c.method = "PUT"
c.hostDB.Path = path.Join(c.hostDB.Path, "_doc", id)
return c.exec(c.hostDB.String(), string(data))
}
// IndexDoc https://www.elastic.co/guide/en/elasticsearch/reference/6.5/docs-index_.html
func (c *Client) IndexDoc(id string, i interface{}) *Client {
if checkIndexName(c) {
return c
}
data, _ := json.Marshal(i)
c.method = "PUT"
c.hostDB.Path = path.Join(c.hostDB.Path, "_doc", id)
return c.exec(c.hostDB.String(), string(data))
}
//UpdatePartialDoc passing a partial document, which will be merged into the existing document
//https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#_updates_with_a_partial_document
func (c *Client) UpdatePartialDoc(id string, i interface{}) *Client {
if checkIndexName(c) {
return c
}
data, _ := json.Marshal(i)
c.method = "POST"
c.hostDB.Path = path.Join(c.hostDB.Path, "_doc", id, "_update")
return c.exec(c.hostDB.String(), string(data))
}
// AutoIndexDocs The index operation automatically creates an index if it has not been created before
// https://www.elastic.co/guide/en/elasticsearch/reference/6.5/docs-index_.html#_automatic_id_generation
func (c *Client) AutoIndexDoc(i interface{}) *Client {
data, _ := json.Marshal(i)
c.method = "POST"
c.hostDB.Path = path.Join(c.hostDB.Path, "_doc")
return c.exec(c.hostDB.String(), string(data))
}
// DeleteDoc deletes an existing index.
// default delete table
// delete doc
// https://www.elastic.co/guide/en/elasticsearch/reference/6.5/getting-started-delete-documents.html
func (c *Client) DeleteDoc(id string) *Client {
if checkIndexName(c) {
return c
}
c.method = "DELETE"
c.hostDB.Path = path.Join(c.hostDB.Path, "_doc", id)
return c.exec(c.hostDB.String())
}
//DeleteByQuerry todo: to support
//https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html
func (c *Client) DeleteByQuerry() *Client {
c.method = "POST"
c.hostDB.Path = path.Join(c.hostDB.Path, "_delete_by_query")
c.Serialize()
return c.exec(c.hostDB.String(), c.template)
}