Skip to content

Commit

Permalink
Merge pull request #7 from maximelamure/add_update_by_query
Browse files Browse the repository at this point in the history
Add update_by_query support
  • Loading branch information
maximelamure authored Dec 23, 2022
2 parents cb1a70e + 5478b12 commit dd6bfb4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ CRUD:
Process:

* Bulk
* UpdateByQuery


Queries:

Expand Down
21 changes: 21 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Client interface {
Suggest(indexName, data string) ([]byte, error)
GetIndicesFromAlias(alias string) ([]string, error)
UpdateAlias(remove []string, add []string, alias string) (*Response, error)
UpdateByQuery(indexName, query string) (*UpdateByQueryResult, error)
}

// A SearchClient describes the client configuration to manage an ElasticSearch index.
Expand Down Expand Up @@ -339,6 +340,26 @@ func (c *client) UpdateAlias(remove []string, add []string, alias string) (*Resp
return esResp, nil
}

// UpdateByQuery updates documents that match the specified query.
// https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html
func (c *client) UpdateByQuery(indexName, query string) (*UpdateByQueryResult, error) {
url := c.Host.String() + "/" + indexName + "/_update_by_query"
reader := bytes.NewBufferString(query)
response, err := sendHTTPRequest("POST", url, reader)
if err != nil {
return &UpdateByQueryResult{}, err
}

esResp := &UpdateByQueryResult{}
err = json.Unmarshal(response, esResp)
if err != nil {
return &UpdateByQueryResult{}, err
}

return esResp, nil

}

func getAliasQuery(remove []string, add []string, alias string) string {
actions := make([]string, len(remove)+len(add))

Expand Down
19 changes: 19 additions & 0 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,22 @@ type MSearchQuery struct {
type MSearchResult struct {
Responses []SearchResult `json:"responses"`
}

type UpdateByQueryResult struct {
Took int `json:"took"`
TimedOut bool `json:"timed_out"`
Total int `json:"total"`
Updated int `json:"updated"`
Deleted int `json:"deleted"`
Batches int `json:"batches"`
VersionConflicts int `json:"version_conflicts"`
Noops int `json:"noops"`
Retries struct {
Bulk int `json:"bulk"`
Search int `json:"search"`
} `json:"retries"`
ThrottledMillis int `json:"throttled_millis"`
RequestsPerSecond int `json:"requests_per_second"`
ThrottledUntilMillis int `json:"throttled_until_millis"`
Failures []interface{} `json:"failures"`
}

0 comments on commit dd6bfb4

Please sign in to comment.