This is a video tutorial and can be found at Youtube .
I've uploaded codes to Gthub. Master branch is contains the codes: Github
I've used PHP 7 for this tutorial but however you may use PHP 5.6 . To simplify the development environment, I've used docker and add it my docker composer file to github. My test docker buil contains mariaDb, nginx, phpmyadmin. This is really helpful while testing elasticsearch since I do not waste time for installing any software.
Ok, this is tutorial contains only simple CRUD operation. In next days, I'll add some advanced codes and publish them in separate branches. All codes are under www folder and this is also nginx and php7 common folder for running application.
In the beginning, I have inserted composer container, however them I did not use it. I’ve used my local composer. First I’ve added elasticsearch/elasticsearch to my project's composer.json file. To install stable latest release prefer to run this code from shell:
composer require elasticsearch/elasticsearch
This safest way to download dependencies. Our first codes is to include autoload file and create a connection to our elasticsearch server.
// index.php use Elasticsearch\ClientBuilder; require 'vendor/autoload.php'; $client = ClientBuilder::create() ->setHosts(['192.168.99.100']) ->build();
Meanwile, do not forget to run docker. This is shortly, first start interactive docker shell. Run following command:
docker-compose up -d
Yes, now our docker is running. To test elasticsearch browse to this local address with port number:
If you do not see any error message, elasctisearch container is running. Let's index some data
$params = [ 'index' => 'developers', 'type' => 'developer', 'body' => [ 'name' => 'Kemal', 'last_name' => 'Yenilmez'] ]; $response = $client->index($params);
Now we have index / insert our first document to elastic. In my example, I did not provider an ID so elastic generated for me. If you wish to insert yours, all you need is to add following line under type node:
'id' => 1001
Let's get our new document:
$params = [ 'index' => 'developers', 'type' => 'developer', 'id' => 'AVY_iVNwGOkYbLNl90-E' ]; $response = $client->get($params);
I've used elastic's own ID, please change with yours with exisiting ID. Mine is AVY_iVNwGOkYbLNl90-E or use 1001 if you used your own ID.
Let's update our document now:
$params = [ 'index' => 'developers', 'type' => 'developer', 'id' => 'AVY_iVNwGOkYbLNl90-E', 'body' => [ 'doc' => ['name' => 'Kemal', 'last_name' => 'Yen'] ] ]; $response = $client->update($params);
You see that, it's so easy to update document. Only you'll enter doc array index which fields that you want to update. I did not enter delete operation since it's so similar if you understood index and update. Only you will not add body, because you do not need to declare the fields, only index, type and ID is enough to delete the document.
ElasticSearch is a full-featured search server. It’s so easy to build a document search. Of course all of the code is to create an index to search them. Following code is a very simple piece of cake. However in the following tutorials, I will create more functional search queries.
$params = [ 'index' => 'developers', 'type' => 'developer', 'body' => [ 'query' => [ 'match' => [ 'name' => 'kemal' ] ] ] ]; $response = $client->search($params);
For the purposes of this tutorial, I try to use Docker, PHP and Elastic Basics in the same time. I do not have a microphone for that reason I couldn't add voice to video. For that reason I've tried to write this blog post.
I've uploaded codes to Gthub. Master branch is contains the codes:Github