From 6953cb673b550db881a81c06b026f91ed8ceb8c3 Mon Sep 17 00:00:00 2001 From: Shyim <6224096+shyim@users.noreply.github.com> Date: Fri, 19 Nov 2021 19:33:20 +0000 Subject: [PATCH] Add PHP client Signed-off-by: GitHub --- _clients/index.md | 1 + _clients/php.md | 100 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 _clients/php.md diff --git a/_clients/index.md b/_clients/index.md index 2f3513dd..3fbecdb0 100644 --- a/_clients/index.md +++ b/_clients/index.md @@ -19,6 +19,7 @@ For example, a 1.0.0 client works with an OpenSearch 1.1.0 cluster, but might no * [OpenSearch Python client]({{site.url}}{{site.baseurl}}/clients/python/) * [OpenSearch JavaScript (Node.js) client]({{site.url}}{{site.baseurl}}/clients/javascript/) * [OpenSearch Go client]({{site.url}}{{site.baseurl}}/clients/go/) +* [OpenSearch PHP client]({{site.url}}{{site.baseurl}}/clients/php/) ## Legacy clients diff --git a/_clients/php.md b/_clients/php.md new file mode 100644 index 00000000..db5027b6 --- /dev/null +++ b/_clients/php.md @@ -0,0 +1,100 @@ +--- +layout: default +title: PHP client +nav_order: 90 +--- + +# PHP client + +The OpenSearch PHP client provides a safer and easier way to interact with your OpenSearch cluster. Rather than using OpenSearch from the browser and potentially exposing your data to the public, you can build an OpenSearch client that takes care of sending requests to your cluster. + +The client contains a library of APIs that let you perform different operations on your cluster and return a standard response body. The example here demonstrates some basic operations like creating an index, adding documents, and searching your data. + +## Setup + +To add the client to your project, install it using [composer]](https://getcomposer.org/): + +```bash +composer require opensearch-project/opensearch-php +``` + +To install a specific major version of the client, run the following command: + +```bash +composer require opensearch-project/opensearch-php: +``` + +Then require the autload file from composer in your code: + +```php +require __DIR__ . '/vendor/autoload.php'; +``` + +## Sample code + +```php +setHosts(['https://localhost:9200']) + ->setBasicAuthentication('admin', 'admin') // For testing only. Don't store credentials in code. + ->setSSLVerification(false) // For testing only. Use certificate for validation + ->build(); + +$indexName = 'test-index-name'; + +// Print OpenSearch version information on console. +var_dump($client->info()); + +// Create an index with non-default settings. +$client->indices()->create([ + 'index' => $indexName, + 'body' => [ + 'settings' => [ + 'index' => [ + 'number_of_shards' => 4 + ] + ] + ] +]); + +$client->create([ + 'index' => $indexName, + 'id' => 1, + 'body' => [ + 'title' => 'Moneyball', + 'director' => 'Bennett Miller', + 'year' => 2011 + ] +]); + +// Search for it +var_dump( + $client->search([ + 'index' => $indexName, + 'body' => [ + 'size' => 5, + 'query' => [ + 'multi_match' => [ + 'query' => 'miller', + 'fields' => ['title^2', 'director'] + ] + ] + ] + ]) +); + +// Delete a single document +$client->delete([ + 'index' => $indexName, + 'id' => 1, +]); + + +// Delete index +$client->indices()->delete([ + 'index' => $indexName +]); +```