Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sample code to readme.md #103

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [Welcome!](#welcome)
- [Project Resources](#project-resources)
- [Code of Conduct](#code-of-conduct)
- [Sample code](#sample-code)
- [Compatibility with OpenSearch](#compatibility-with-opensearch)
- [License](#license)
- [Copyright](#copyright)
Expand All @@ -14,7 +15,7 @@
## Project Resources

* [Project Website](https://opensearch.org/)
* [User Guide](https://github.com/opensearch-project/opensearch-php/blob/main/USER_GUIDE.md)
* [User Guide And Sample Code](https://github.com/opensearch-project/opensearch-php/blob/main/USER_GUIDE.md)
* [Developer Guide](https://github.com/opensearch-project/opensearch-php/blob/main/DEVELOPER_GUIDE.md)
* [Downloads](https://opensearch.org/downloads.html).
* [Documentation](https://opensearch.org/docs/latest/)
Expand All @@ -30,6 +31,10 @@

This project has adopted the [Amazon Open Source Code of Conduct](https://github.com/opensearch-project/opensearch-php/blob/main/CODE_OF_CONDUCT.md). For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq), or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.

## Sample code

See [Sample Code](https://github.com/opensearch-project/opensearch-php/blob/main/USER_GUIDE.md).

## Compatibility with OpenSearch

See [Compatibility](COMPATIBILITY.md).
Expand All @@ -40,4 +45,4 @@ This project is licensed under the [Apache v2.0 License](https://github.com/open

## Copyright

Copyright OpenSearch Contributors. See [NOTICE](https://github.com/opensearch-project/opensearch-php/blob/main/NOTICE) for details.
Copyright OpenSearch Contributors. See [NOTICE](https://github.com/opensearch-project/opensearch-php/blob/main/NOTICE) for details.
349 changes: 278 additions & 71 deletions USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,84 +7,291 @@ Install this client using Composer into your project `composer req opensearch-pr
## Example usage

```php
<?php

<?php
require __DIR__ . '/vendor/autoload.php';

$client = (new \OpenSearch\ClientBuilder())
->setHosts(['https://localhost:9200'])
->setBasicAuthentication('admin', 'admin') // For testing only. Don't store credentials in code.
// or, if using AWS SigV4 authentication:
->setSigV4Region('us-east-2')
->setSigV4CredentialProvider(true)
->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
define('INDEX_NAME', 'test_elastic_index_name2');

class MyOpenSearchClass
{

protected ?\OpenSearch\Client $client;
protected $existingID = 1668504743;
protected $deleteID = 1668504743;
protected $bulkIds = [];


public function __construct()
{
//simple Setup
$this->client = OpenSearch\ClientBuilder::fromConfig([
'hosts' => [
'https://localhost:9200'
],
'retries' => 2,
'handler' => OpenSearch\ClientBuilder::multiHandler()
]);

// OR via Builder
// $this->client = (new \OpenSearch\ClientBuilder())
// ->setHosts(['https://localhost:9200'])
// ->setBasicAuthentication('admin', 'admin') // For testing only. Don't store credentials in code.
// // or, if using AWS SigV4 authentication:
// ->setSigV4Region('us-east-2')
// ->setSigV4CredentialProvider(true)
// ->setSSLVerification(false) // For testing only. Use certificate for validation
// ->build();
}


// Create an index with non-default settings.
public function createIndex()
{
$this->client->indices()->create([
'index' => INDEX_NAME,
'body' => [
'settings' => [
'index' => [
'number_of_shards' => 4
]
]
]
]
]
]);

// Create a document passing the id
$client->create([
'index' => $indexName,
'id' => 1,
'body' => [
'title' => 'Moneyball',
'director' => 'Bennett Miller',
'year' => 2011
]
]);

// Create a document without passing the id (will be generated automatically)
$client->create([
'index' => $indexName,
'body' => [
'title' => 'Remember the Titans',
'director' => 'Boaz Yakin',
'year' => 2000
]
]);

// Search for it
var_dump(
$client->search([
'index' => $indexName,
'body' => [
'size' => 5,
'query' => [
'multi_match' => [
'query' => 'miller',
'fields' => ['title^2', 'director']
]);
}

public function info()
{
// Print OpenSearch version information on console.
var_dump($this->client->info());
}

// Create a document
public function create()
{
$time = time();
$this->existingID = $time;
$this->deleteID = $time . '_uniq';


// Create a document passing the id
$this->client->create([
'id' => $time,
'index' => INDEX_NAME,
'body' => $this->getData($time)
]);

// Create a document passing the id
$this->client->create([
'id' => $this->deleteID,
'index' => INDEX_NAME,
'body' => $this->getData($time)
]);

// Create a document without passing the id (will be generated automatically)
$this->client->create([
'index' => INDEX_NAME,
'body' => $this->getData($time + 1)
]);

//This should throw an exception because ID already exists
// $this->client->create([
// 'id' => $this->existingID,
// 'index' => INDEX_NAME,
// 'body' => $this->getData($this->existingID)
// ]);
}

public function update()
{
$this->client->update([
'id' => $this->existingID,
'index' => INDEX_NAME,
'body' => [
//data must be wrapped in 'doc' object
'doc' => ['name' => 'updated']
]
]);
}

public function bulk()
{
$bulkData = [];
$time = time();
for ($i = 0; $i < 20; $i++) {
$id = ($time + $i) . rand(10, 200);
$bulkData[] = [
'index' => [
'_index' => INDEX_NAME,
'_id' => $id,
]
];
$this->bulkIds[] = $id;
$bulkData[] = $this->getData($time + $i);
}
//will not throw exception! check $response for error
$response = $this->client->bulk([
//default index
'index' => INDEX_NAME,
'body' => $bulkData
]);

//give elastic a little time to create before update
sleep(2);

// bulk update
for ($i = 0; $i < 15; $i++) {
$bulkData[] = [
'update' => [
'_index' => INDEX_NAME,
'_id' => $this->bulkIds[$i],
]
];
$bulkData[] = [
'doc' => [
'name' => 'bulk updated'
]
];
}

//will not throw exception! check $response for error
$response = $this->client->bulk([
//default index
'index' => INDEX_NAME,
'body' => $bulkData
]);
}
public function deleteByQuery(string $query)
{
if ($query == '') {
return;
}
$this->client->deleteByQuery([
'index' => INDEX_NAME,
'q' => $query
]);
}

// Delete a single document
public function deleteByID()
{
$this->client->delete([
'id' => $this->deleteID,
'index' => INDEX_NAME,
]);
}

public function search()
{
$docs = $this->client->search([
//index to search in or '_all' for all indices
'index' => INDEX_NAME,
'size' => 1000,
'body' => [
'query' => [
'prefix' => [
'name' => 'wrecking'
]
]
]
]
])
);

// Delete a single document
$client->delete([
'index' => $indexName,
'id' => 1,
]);

]);
var_dump($docs['hits']['total']['value'] > 0);

// Search for it
$docs = $this->client->search([
'index' => INDEX_NAME,
'body' => [
'size' => 5,
'query' => [
'multi_match' => [
'query' => 'miller',
'fields' => ['title^2', 'director']
]
]
]
]);
var_dump($docs['hits']['total']['value'] > 0);
}

public function getMultipleDocsByIDs()
{
$docs = $this->client->search([
//index to search in or '_all' for all indices
'index' => INDEX_NAME,
'body' => [
'query' => [
'ids' => [
'values' => $this->bulkIds
]
]
]
]);
var_dump($docs['hits']['total']['value'] > 0);
}

public function getOneByID()
{
$docs = $this->client->search([
//index to search in or '_all' for all indices
'index' => INDEX_NAME,
'size' => 1,
'body' => [
'query' => [
'bool' => [
'filter' => [
'term' => [
'_id' => $this->existingID
]
]
]
]
]
]);
var_dump($docs['hits']['total']['value'] > 0);
}

// Delete index
public function deleteByIndex()
{
$this->client->indices()->delete([
'index' => INDEX_NAME
]);
}

//simple data to index
public function getData($time = -1)
{
if ($time == -1) {
$time = time();
}
return [
'name' => date('c', $time) . " - i came in like a wrecking ball",
'time' => $time,
'date' => date('c', $time)
];
}
}

try {

$e = new MyOpenSearchClass();
$e->info();
$e->createIndex();
$e->create();
//give elastic a little time to create before update
sleep(2);
$e->update();
$e->bulk();
$e->getOneByID();
$e->getMultipleDocsByIDs();
$e->search();
$e->deleteByQuery('');
$e->deleteByID();
$e->deleteByIndex();
} catch (\Throwable $th) {
echo 'uncaught error ' . $th->getMessage() . "\n";
}

// Delete index
$client->indices()->delete([
'index' => $indexName
]);
```

## ClientBuilder
Expand Down