-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Send raw json request (#177)
* feat(client): send raw json request Signed-off-by: imdhemy <imdhemy@gmail.com> * chore: update changelog Signed-off-by: imdhemy <imdhemy@gmail.com> * chore: use same attributes of the transport class Signed-off-by: imdhemy <imdhemy@gmail.com> * chore(guides): add raw json request guide Signed-off-by: imdhemy <imdhemy@gmail.com> * chore: code cleanup Signed-off-by: imdhemy <imdhemy@gmail.com> * chore(client): improve raw request tests Signed-off-by: imdhemy <imdhemy@gmail.com> * fix(cicd): fix link checker (#175) Signed-off-by: imdhemy <imdhemy@gmail.com> --------- Signed-off-by: imdhemy <imdhemy@gmail.com>
- Loading branch information
Showing
5 changed files
with
170 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Raw JSON Requests | ||
|
||
Opensearch client implements many high-level APIs out of the box. However, there are times when you need to send a raw | ||
JSON request to the server. This can be done using the `request()` method of the client. | ||
|
||
The `request()` method expects the following parameters: | ||
|
||
| Parameter | Description | | ||
|---------------|------------------------------------------------------------------------------| | ||
| `$method` | The HTTP method to use for the request, `GET`, `POST`, `PUT`, `DELETE`, etc. | | ||
| `$uri` | The URI to send the request to, e.g. `/_search`. | | ||
| `$attributes` | An array of request options. `body`, `params` & `options` | | ||
|
||
> ![NOTE] | ||
> The `request()` is a wrapper around the `\OpenSearch\Transport::performRequest()` method, this explains why [the | ||
> parameters](https://github.com/opensearch-project/opensearch-php/blob/bf9b9815e9636196295432baa9c0368bc2efadd8/src/OpenSearch/Transport.php#L99) | ||
> are intentionally similar. | ||
Most of the time, you will only need to provide the `body` attribute. The `body` attribute is the JSON payload to send | ||
to the server. The `params` attribute is used to set query parameters, and the `options` attribute is used to set | ||
additional request options. | ||
|
||
To send a raw JSON request you need to map the documentation to the `request()` method parameters. For example, [this | ||
request](https://opensearch.org/docs/2.12/search-plugins/keyword-search/#example) that query searches for the | ||
words `long live king` in the `shakespeare` index: | ||
|
||
``` | ||
GET shakespeare/_search | ||
{ | ||
"query": { | ||
"match": { | ||
"text_entry": "long live king" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Can be translated to the following `request()` method call: | ||
|
||
```php | ||
$response = $client->request('GET', '/shakespeare/_search', [ | ||
'body' => [ | ||
'query' => [ | ||
'match' => [ | ||
'text_entry' => 'long live king' | ||
] | ||
] | ||
] | ||
]); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters