islacraig03
shared a link
How to use Elasticsearch with PHP | Web Development Blog — web-development-blog.com

Elasticsearch is an open-source full-text search engine which allows you to store and search data in real time. You can search for phrases as well and it will give you the results within seconds depending on how large the Elasticsearch database is. This engine outputs results faster than SQL and is one of the most used search around the web. There are two ways you can use Elasticsearch with PHP; one with using curl and the other by using official client of Elasticsearch for PHP. In this tutorial I will show you how to use Elasticsearch using its PHP Client. Before we start, make sure that Elasticsearch is installed on your local machine. Here’s how you can do it: Installing Elasticsearch for PHP We will install it using composer. In your localhost root folder, create a new directory and name it elastic_php. In the same directory, create a new file composer.json and paste the following text in it:{ "require": { "elasticsearch/elasticsearch": "~2.0" }}Run composer install. Now that we have have installed it, let’s connect it with PHP. Connecting Elasticsearch with PHP Now create a new PHP script file and name it index.php inside the elastic_php directory and paste the following code in it:<?phprequire 'vendor/autoload.php';$client = Elasticsearch\ClientBuilder::create()->build(); if ($client) { echo 'connected';}Save it and run the script. Make sure Elasticsearch is running or you will get a connection error. Indexing data in Elasticsearch Now that we are connected to Elasticsearch, let’s index some data in it. Open your index.php file and change the following code in it.<?phprequire 'vendor/autoload.php';$client = Elasticsearch\ClientBuilder::create()->build(); $params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id2', 'body' => [ 'first field' => 'Adding My First Field In Elasticsearch' ],];$response = $client->index($params);echo $response['created'];When you run the above code you will get 1 as output. Getting data from Elasticsearch Now we have an index, let’s get data from it. Now replace the code with the following in your index.php and run it:<?phprequire 'vendor/autoload.php';$client = Elasticsearch\ClientBuilder::create()->build(); $params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id',]; $response = $client->get($params);echo $response['_source']['first field'];You will get the field that we have added.Adding My First Field In Elasticsearch Searching in Elasticsearch Let’s search for some data in Elasticsearch. Now open your index.php file again replace it with the following code.12345678910111213141516171819202122232425262728<?phprequire 'vendor/autoload.php';$client = Elasticsearch\ClientBuilder::create()->build(); $params = [ 'index' => 'my_index', 'type' => 'my_type', 'body' => [ 'query' => [ 'match' => [ 'first field' => 'first fiel' ], ], ],]; $response = $client->search($params);$hits = count($response['hits']['hits']);$result = null;$i = 0; while ($i < $hits) { $result[$i] = $response['hits']['hits'][$i]['_source']; $i++;}foreach ($result as $key => $value) { echo $value['first field'] . "<br>";}Now let’s understand the code a bit. When we search for our query in Elasticsearch it returns with a lot of results including our specific query result. We need to fetch our result from it and for that, first we need to check how many results we have:$hits = count($response['hits']['hits']);Then we fetch our results from it which will be less than its hits because it’s an array.while ($i < $hits) { $result[$i] = $response['hits']['hits'][$i]['_source']; $i++;} After that we print the result in a browser. Now run the code provided above and you will get the following result (again):Adding My First Field In Elasticsearch  Conclusion In this tutorial you learned how to connect Elasticsearch with PHP and how to save and retrieve data from it. You also learned how to perform a search with PHP and Elasticsearch. If there is anything that confuses you, please leave a comment below and we’ll come up with the solution for you! About the author Ahmed Khan is the PHP Community Manager at Cloudways and writes about MySQL and PHP. An experienced professional in PHP related topics, Ahmed loves to spend his free time watching The Flash, Game of Thrones and reading DC Comics. Follow him on Twitter and Facebook to keep up with his work and insights on PHP hosting.

Read More



This site uses cookies to give the best and personalised experience. By continuing to browse the site you are agreeing to our use of cookies. Find out more here.

Sign Up or Login

Invalid username or password
Not yet on CircleMe? Join now
Please input a email
Please input a valid email
Max 50 characters
Email already in use
{{email_serverError}}
Please input a username
Min 3 characters
Max 20 characters
Please use only A-Z, 0-9 and "_"
Username is taken
Please input a password
Min 6 characters
Max 20 characters
By clicking the icons, you agree to CircleMe terms & conditions
Already have an account? Login Now