Skip to content

Commit

Permalink
[DOCS] Adds getting started content based on the template (#1329)
Browse files Browse the repository at this point in the history
  • Loading branch information
szabosteve authored Jul 4, 2023
1 parent 116e376 commit dc469d4
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 0 deletions.
241 changes: 241 additions & 0 deletions docs/getting-started.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
[[getting-started-php]]
== Getting started

This page guides you through the installation process of the PHP client, shows
you how to instantiate the client, and how to perform basic Elasticsearch
operations with it.

[discrete]
=== Requirements

* TO DO
* http://getcomposer.org[composer]

If you don't have composer you can install it by running the following commands:

[source,shell]
--------------------------
curl -s http://getcomposer.org/installer | php
php composer.phar install
--------------------------


[discrete]
=== Installation

To install the latest version of the client, run the following command:

[source,shell]
--------------------------
composer require elasticsearch/elasticsearch
--------------------------

When you have installed elasticsearch-php you can start using it with the
`Client` class. You can use the `ClientBuilder` class to create this object:

[source,php]
--------------------------
require 'vendor/autoload.php';
$client = Elastic\Elasticsearch\ClientBuilder::create()->build();
--------------------------

Refer to the <<installation>> page to learn more.


[discrete]
=== Connecting

You can connect to the Elastic Cloud using an API key and the Elasticsearch
endpoint.

[source,php]
----
$client = ClientBuilder::create()
->setElasticsearchEndpoint('<elasticsearch-endpoint>')
->setApiKey('<api-key>')
->build();
----

Your Elasticsearch endpoint can be found on the **My deployment** page of your
deployment:

image::images/es_endpoint.jpg[alt="Finding Elasticsearch endpoint",align="center"]

You can generate an API key on the **Management** page under Security.

image::images/create_api_key.png[alt="Create API key",align="center"]

For other connection options, refer to the <<connecting>> section.


[discrete]
=== Operations

Time to use Elasticsearch! This section walks you through the basic, and most
important, operations of Elasticsearch. For more operations and more advanced
examples, refer to the <<operations>> page.


[discrete]
==== Creating an index

This is how you create the `my_index` index:

[source,php]
----
$client = ClientBuilder::create()->build();
$params = [
'index' => 'my_index'
];
// Create the index
$response = $client->indices()->create($params);
----


[discrete]
==== Indexing documents

This is a simple way of indexing a document:

[source,php]
----
$params = [
'index' => 'my_index',
'body' => [ 'testField' => 'abc']
];
// Document will be indexed to my_index/_doc/<autogenerated ID>
$response = $client->index($params);
----

You can bulk index documents with batches in a slightly more complex way:

.Bulk indexing with batches
[source,php]
----
$params = ['body' => []];
for ($i = 1; $i <= 1234567; $i++) {
$params['body'][] = [
'index' => [
'_index' => 'my_index',
'_id' => $i
]
];
$params['body'][] = [
'my_field' => 'my_value',
'second_field' => 'some more values'
];
// Every 1000 documents stop and send the bulk request
if ($i % 1000 == 0) {
$responses = $client->bulk($params);
// erase the old bulk request
$params = ['body' => []];
// unset the bulk response when you are done to save memory
unset($responses);
}
}
// Send the last batch if it exists
if (!empty($params['body'])) {
$responses = $client->bulk($params);
}
----


[discrete]
==== Getting documents

You can get documents by using the following code:

[source,php]
----
$params = [
'index' => 'my_index',
'id' => 'my_id'
];
// Get doc at /my_index/_doc/my_id
$response = $client->get($params);
----


[discrete]
==== Searching documents

This is how you can create a single match query with the PHP client:

[source,php]
----
$params = [
'index' => 'my_index',
'body' => [
'query' => [
'match' => [
'testField' => 'abc'
]
]
]
];
$results = $client->search($params);
----


[discrete]
==== Updating documents

This is how you can update a document, for example to add a new field:

[source,php]
----
$params = [
'index' => 'my_index',
'id' => 'my_id',
'body' => [
'doc' => [
'new_field' => 'abc'
]
]
];
// Update doc at /my_index/_doc/my_id
$response = $client->update($params);
----


[discrete]
==== Deleting documents

[source,php]
----
$params = [
'index' => 'my_index',
'id' => 'my_id'
];
// Delete doc at /my_index/_doc_/my_id
$response = $client->delete($params);
----


[discrete]
==== Deleting an index

[source,php]
----
$params = ['index' => 'my_index'];
$response = $client->indices()->delete($params);
----


[discrete]
== Further reading

* Use <<client-helpers>> for a more confortable experience with the APIs.
Binary file added docs/images/es_endpoint.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions docs/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ include::{asciidoc-dir}/../../shared/attributes.asciidoc[]

include::overview.asciidoc[]

include::getting-started.asciidoc[]

include::installation.asciidoc[]

include::connecting.asciidoc[]
Expand Down
3 changes: 3 additions & 0 deletions docs/overview.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ from one language to the next with minimal effort.
The client is designed to facilitate the API call using different way to read the
results using associative array, object, string or https://www.php-fig.org/psr/psr-7/[PSR-7].

Refer to the <<getting-started-php>> page for a step-by-step quick start with
the PHP client.

[discrete]
[[psr-7-standard]]
=== PSR 7 standard
Expand Down

0 comments on commit dc469d4

Please sign in to comment.