Lets install ElasticSearch client for php.
1) Open the terminal and go to the project folder that you want to create
create a new file called composer.json and write the following
{In case you want to user version 2.0 replace 1.0 with 2.0
"require": {
"elasticsearch/elasticsearch": "~1.0"
}
}
2) Donwload and install composer
curl -s http://getcomposer.org/installer | php
3) this will create a file composer.phar. run following command to install all dependancy
php composer.phar install --no-dev
4) Require Composer's autoloader. Composer also prepares an autoload file that's capable of autoloading all of the classes in any of the libraries that it downloads. Start elasticsearch service if it is not started.
/etc/init.d/elasticsearch start
5) Open a new php file which can connect to elasticsearch.
require 'vendor/autoload.php';
$client_builder = new Elasticsearch\Client();
//creating index
$params['body'] = array('testField' => 'abc');
$params['index'] = 'my_index';
$params['type'] = 'my_type';
$params['id'] = 'my_id';
$ret = $client_builder->index($params);
var_dump($ret);
//fetching index
$getParams = array();
$getParams['index'] = 'my_index';
$getParams['type'] = 'my_type';
$getParams['id'] = 'my_id';
$retDoc = $client_builder->get($getParams);
var_dump($retDoc);
//searching index
$searchParams['index'] = 'my_index';
$searchParams['type'] = 'my_type';
$searchParams['body']['query']['match']['testField'] = 'abc';
$retDoc = $client_builder->search($searchParams);
var_dump($retDoc);
?>
once done. Save the file as index.php and run it using command
php index.php
We assume tha you are in your project folder before you start the process. This file will create index 'my_index' in elasticsearch. file also shows demo for fetching index data or searching index data based on the fields. Hope this will provide you basic understanding of how elastic search can be integrated with Php.
No comments:
Post a Comment