Skip to content

Commit 9a7a03f

Browse files
authored
feat: add create api for indexing (#175)
* feat: add create api for indexing * chore: update docs
1 parent f666b07 commit 9a7a03f

File tree

8 files changed

+219
-6
lines changed

8 files changed

+219
-6
lines changed

core/elastic/api.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,14 @@ type API interface {
7575
CreateIndex(name string, settings map[string]interface{}) error
7676

7777
Index(indexName, docType string, id interface{}, data interface{}, refresh string) (*InsertResponse, error)
78-
78+
Create(indexName, docType string, id interface{}, data interface{}, refresh string) (*InsertResponse, error)
7979
Update(indexName, docType string, id interface{}, data interface{}, refresh string) (*InsertResponse, error)
8080

81-
Bulk(data []byte) (*util.Result, error)
82-
8381
Get(indexName, docType, id string) (*GetResponse, error)
8482
Delete(indexName, docType, id string, refresh ...string) (*DeleteResponse, error)
83+
84+
Bulk(data []byte) (*util.Result, error)
85+
8586
Count(ctx context.Context, indexName string, body []byte) (*CountResponse, error)
8687
Search(indexName string, query *SearchRequest) (*SearchResponse, error)
8788

core/elastic/index.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ func (this *ResponseBase) GetBoolByJsonPath(path string) (interface{}, error) {
255255
// InsertResponse is a index response object
256256
type InsertResponse struct {
257257
ResponseBase
258+
OK bool `json:"ok,omitempty"` //for v0.x only
258259
Result string `json:"result"`
259260
Index string `json:"_index"`
260261
Type string `json:"_type"`

core/orm/orm.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ type ORM interface {
6666

6767
Save(ctx *Context, o interface{}) error
6868

69+
Create(ctx *Context, o interface{}) error
70+
6971
Update(ctx *Context, o interface{}) error
7072

7173
Delete(ctx *Context, o interface{}) error
@@ -344,7 +346,7 @@ func Create(ctx *Context, o interface{}) error {
344346
return err
345347
}
346348

347-
err = getHandler().Save(ctx, o)
349+
err = getHandler().Create(ctx, o)
348350
if err != nil {
349351
return err
350352
}

docs/content.en/docs/release-notes/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Information about release notes of INFINI Framework is provided here.
1313
### 🚀 Features
1414
- feat: add hooks for ORM data operation #167
1515
- feat: merge term filters to terms filter with same field #173
16+
- feat: add create api for indexing #175
1617

1718
### 🐛 Bug fix
1819
- fix: HTTP headers config was not applied with plugin `http`

modules/elastic/adapter/elasticsearch/v0.go

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,72 @@ func (c *ESAPIV0) Index(indexName, docType string, id interface{}, data interfac
318318
if err != nil {
319319
return &elastic.InsertResponse{}, err
320320
}
321-
if !(esResp.Result == "created" || esResp.Result == "updated" || esResp.Shards.Successful > 0) {
321+
if !(esResp.OK || esResp.Result == "created" || esResp.Result == "updated" || esResp.Shards.Successful > 0) {
322+
return nil, errors.New(string(resp.Body))
323+
}
324+
325+
return esResp, nil
326+
}
327+
328+
func (c *ESAPIV0) Create(indexName, docType string, id interface{}, data interface{}, refresh string) (*elastic.InsertResponse, error) {
329+
330+
if docType == "" {
331+
docType = TypeName0
332+
}
333+
334+
indexName = util.UrlEncode(indexName)
335+
336+
u, _ := url.Parse(fmt.Sprintf("%s/%s/%s", c.GetEndpoint(), indexName, docType))
337+
338+
if id != "" {
339+
u.Path = fmt.Sprintf("%s/%s", u.Path, id)
340+
q := u.Query()
341+
q.Set("op_type", "create")
342+
u.RawQuery = q.Encode()
343+
}
344+
345+
if refresh != "" {
346+
q := u.Query()
347+
q.Set("refresh", "true")
348+
u.RawQuery = q.Encode()
349+
}
350+
351+
url := u.String()
352+
353+
var (
354+
js []byte
355+
err error
356+
)
357+
if dataBytes, ok := data.([]byte); ok {
358+
js = dataBytes
359+
} else {
360+
js, err = json.Marshal(data)
361+
}
362+
363+
if global.Env().IsDebug {
364+
log.Trace("creating doc: ", url, ",", string(js))
365+
}
366+
367+
if err != nil {
368+
return nil, err
369+
}
370+
371+
resp, err := c.Request(nil, util.Verb_POST, url, js)
372+
373+
if err != nil {
374+
return nil, err
375+
}
376+
377+
if global.Env().IsDebug {
378+
log.Trace("creating response: ", string(resp.Body))
379+
}
380+
381+
esResp := &elastic.InsertResponse{}
382+
err = json.Unmarshal(resp.Body, esResp)
383+
if err != nil {
384+
return &elastic.InsertResponse{}, err
385+
}
386+
if !(esResp.OK || esResp.Result == "created" || esResp.Result == "updated" || esResp.Shards.Successful > 0) {
322387
return nil, errors.New(string(resp.Body))
323388
}
324389

modules/elastic/adapter/elasticsearch/v7.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
"fmt"
4545
"infini.sh/framework/core/errors"
4646
"net/http"
47+
"net/url"
4748

4849
log "github.com/cihub/seelog"
4950
"github.com/segmentio/encoding/json"
@@ -324,6 +325,69 @@ func (c *ESAPIV7) Index(indexName, docType string, id interface{}, data interfac
324325
return esResp, nil
325326
}
326327

328+
func (c *ESAPIV7) Create(indexName, docType string, id interface{}, data interface{}, refresh string) (*elastic.InsertResponse, error) {
329+
330+
if docType == "" {
331+
docType = TypeName7
332+
}
333+
indexName = util.UrlEncode(indexName)
334+
335+
u, _ := url.Parse(fmt.Sprintf("%s/%s/%s", c.GetEndpoint(), indexName, docType))
336+
337+
if id != "" {
338+
u.Path = fmt.Sprintf("%s/%s", u.Path, id)
339+
q := u.Query()
340+
q.Set("op_type", "create")
341+
u.RawQuery = q.Encode()
342+
}
343+
344+
if refresh != "" {
345+
q := u.Query()
346+
q.Set("refresh", "true")
347+
u.RawQuery = q.Encode()
348+
}
349+
350+
url := u.String()
351+
352+
var (
353+
js []byte
354+
err error
355+
)
356+
if dataBytes, ok := data.([]byte); ok {
357+
js = dataBytes
358+
} else {
359+
js, err = json.Marshal(data)
360+
}
361+
362+
if global.Env().IsDebug {
363+
log.Debug("creating doc: ", url, ",", string(js))
364+
}
365+
366+
if err != nil {
367+
return nil, err
368+
}
369+
370+
resp, err := c.Request(nil, util.Verb_POST, url, js)
371+
if err != nil {
372+
return nil, err
373+
}
374+
375+
if global.Env().IsDebug {
376+
log.Trace("creating response: ", string(resp.Body))
377+
}
378+
379+
esResp := &elastic.InsertResponse{}
380+
err = json.Unmarshal(resp.Body, esResp)
381+
if err != nil {
382+
return &elastic.InsertResponse{}, err
383+
}
384+
if !(esResp.Result == "created") {
385+
return nil, errors.New(string(resp.Body))
386+
}
387+
388+
return esResp, nil
389+
}
390+
327391
func (c *ESAPIV7) UpdateMapping(indexName string, docType string, mappings []byte) ([]byte, error) {
328392
indexName = util.UrlEncode(indexName)
329393

modules/elastic/adapter/elasticsearch/v8.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import (
3232
"errors"
3333
"fmt"
3434
"net/http"
35+
"net/url"
36+
"path"
3537

3638
log "github.com/cihub/seelog"
3739
"infini.sh/framework/core/elastic"
@@ -243,6 +245,69 @@ func (c *ESAPIV8) Index(indexName, docType string, id interface{}, data interfac
243245
return esResp, nil
244246
}
245247

248+
func (c *ESAPIV8) Create(indexName, docType string, id interface{}, data interface{}, refresh string) (*elastic.InsertResponse, error) {
249+
250+
indexName = util.UrlEncode(indexName)
251+
252+
basePath := ""
253+
if id != "" {
254+
basePath = fmt.Sprintf("/%s/_create/%s", indexName, id)
255+
} else {
256+
basePath = fmt.Sprintf("/%s/_doc", indexName)
257+
}
258+
259+
u, err := url.Parse(c.GetEndpoint())
260+
if err != nil {
261+
return nil, fmt.Errorf("invalid endpoint: %w", err)
262+
}
263+
u.Path = path.Join(u.Path, basePath)
264+
265+
q := u.Query()
266+
if refresh != "" {
267+
q.Set("refresh", refresh)
268+
}
269+
u.RawQuery = q.Encode()
270+
271+
url := u.String()
272+
273+
var (
274+
js []byte
275+
)
276+
if dataBytes, ok := data.([]byte); ok {
277+
js = dataBytes
278+
} else {
279+
js, err = json.Marshal(data)
280+
}
281+
282+
if global.Env().IsDebug {
283+
log.Debug("creating doc: ", url, ",", string(js))
284+
}
285+
286+
if err != nil {
287+
return nil, err
288+
}
289+
290+
resp, err := c.Request(nil, util.Verb_POST, url, js)
291+
if err != nil {
292+
return nil, err
293+
}
294+
295+
if global.Env().IsDebug {
296+
log.Trace("creating response: ", string(resp.Body))
297+
}
298+
299+
esResp := &elastic.InsertResponse{}
300+
err = json.Unmarshal(resp.Body, esResp)
301+
if err != nil {
302+
return &elastic.InsertResponse{}, err
303+
}
304+
if !(esResp.Result == "created") {
305+
return nil, errors.New(string(resp.Body))
306+
}
307+
308+
return esResp, nil
309+
}
310+
246311
func (c *ESAPIV8) Flush(indexName string) ([]byte, error) {
247312
url := "/_flush"
248313
if indexName != "" {

modules/elastic/orm.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (handler *ElasticORM) GetIndexName(o interface{}) string {
134134
return fmt.Sprintf("%s%s", handler.Config.IndexPrefix, indexName)
135135
}
136136

137-
func (handler *ElasticORM) Get(ctx *api.Context,o interface{}) (bool, error) {
137+
func (handler *ElasticORM) Get(ctx *api.Context, o interface{}) (bool, error) {
138138

139139
id := getIndexID(o)
140140
if id == "" {
@@ -172,6 +172,20 @@ func (handler *ElasticORM) GetBy(field string, value interface{}, t interface{})
172172
return handler.Search(t, &query)
173173
}
174174

175+
func (handler *ElasticORM) Create(ctx *api.Context, o interface{}) error {
176+
var refresh string
177+
if ctx != nil {
178+
refresh = ctx.Refresh
179+
}
180+
181+
docID := getIndexID(o)
182+
if global.Env().IsDebug {
183+
log.Debug("docID:", docID)
184+
}
185+
_, err := handler.Client.Create(handler.GetIndexName(o), "", docID, o, refresh)
186+
return err
187+
}
188+
175189
func (handler *ElasticORM) Save(ctx *api.Context, o interface{}) error {
176190
var refresh string
177191
if ctx != nil {

0 commit comments

Comments
 (0)