Skip to content

Commit

Permalink
Merge pull request #20 from cabify/pablo/add-index
Browse files Browse the repository at this point in the history
Add PostSearchIndex
  • Loading branch information
palcalde authored Apr 9, 2024
2 parents 81cac75 + 961eb6e commit ca3d5fc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
### Security
- Nothing

## [0.5.0] - 2024-04-09
### Added
- Added PostSearchIndex endpoint to give support for search indexes

## [0.4.1] - 2020-08-03
### Fixed
- Fix BulkGet request body
Expand Down
31 changes: 29 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ type DB struct {
// WithContext returns a new copy of the database object with the
// new context set. Use like:
//
// db.WithContext(ctx).Post(doc)
//
// db.WithContext(ctx).Post(doc)
func (db *DB) WithContext(ctx context.Context) *DB {
db2 := new(DB)
*db2 = *db
Expand Down Expand Up @@ -296,6 +295,34 @@ func (db *DB) PostView(ddoc, view string, result interface{}, opts Options, payl
return readBody(resp, &result)
}

// PostSearchIndex invokes a Search Index
// The ddoc parameter must be the name of the design document
// containing the index, but excluding the _design/ prefix.
//
// The output of the query is unmarshalled into the given result.
// The format of the result depends on the options. Please
// refer to the CouchDB HTTP API documentation for all the possible
// options that can be set.
//
// https://docs.couchdb.org/en/stable/ddocs/search.html
func (db *DB) PostSearchIndex(ddoc, index string, result interface{}, opts Options, payload Payload) error {
ddoc = strings.Replace(ddoc, "_design/", "", 1)
path, err := optpath(opts, viewJsonKeys, db.name, "_design", ddoc, "_search", index)
if err != nil {
return err
}
json, err := json.Marshal(payload)
if err != nil {
return err
}
body := bytes.NewReader(json)
resp, err := db.request(db.ctx, "POST", path, body)
if err != nil {
return err
}
return readBody(resp, &result)
}

// AllDocs invokes the _all_docs view of a database.
//
// The output of the query is unmarshalled into the given result.
Expand Down

0 comments on commit ca3d5fc

Please sign in to comment.