{"_id":"5a8fae0468264c001f20cc3c","category":{"_id":"5a8fae0268264c001f20cc02","version":"5a8fae0268264c001f20cc00","project":"5633ebff7e9e880d00af1a53","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-12-23T20:06:16.013Z","from_sync":false,"order":3,"slug":"quickstart-guides-1","title":"Quickstart Guides"},"parentDoc":null,"user":"5633ec9b35355017003ca3f2","project":"5633ebff7e9e880d00af1a53","version":{"_id":"5a8fae0268264c001f20cc00","project":"5633ebff7e9e880d00af1a53","__v":2,"createdAt":"2018-02-23T06:00:34.961Z","releaseDate":"2018-02-23T06:00:34.961Z","categories":["5a8fae0268264c001f20cc01","5a8fae0268264c001f20cc02","5a8fae0368264c001f20cc03","5a8fae0368264c001f20cc04","5a8fae0368264c001f20cc05","5a8fae0368264c001f20cc06","5a8fae0368264c001f20cc07","5a8fae0368264c001f20cc08","5a8fae0368264c001f20cc09","5abaa7eb72d6dc0028a07bf3"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"2.0.0","version":"2.0"},"__v":0,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-12-23T19:23:51.344Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":6,"body":"We recommend using the [official PHP client](https://github.com/elasticsearch/elasticsearch-php), as it is being actively developed alongside Elasticsearch. If you still need to add Bonsai to your app, see our [Installing the Add-on](doc:getting-started-with-heroku) guide.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Adding the Elasticsearch library\"\n}\n[/block]\nLike Heroku, we recommend Composer for dependency management in PHP projects, so you'll need to add the Elasticsearch library to your `composer.json` file:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{\\n    \\\"require\\\": {\\n        \\\"elasticsearch/elasticsearch\\\": \\\"1.0\\\"\\n    }\\n}\",\n      \"language\": \"json\"\n    }\n  ]\n}\n[/block]\nHeroku will automatically add the library when you deploy your code.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Using the library\"\n}\n[/block]\nThe elasticsearch-php library is configured almost entirely by associative arrays. To initialize your client, use the following code block:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// Initialize the parameters for the cluster\\n$params = array();\\n$params['hosts'] = array (\\n    getenv(\\\"BONSAI_URL\\\"),\\n);\\n\\n$client = new Elasticsearch\\\\Client($params);\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nNote that the host is pulled from the environment. When you add Bonsai to your app, the cluster will be automatically created and a `BONSAI_URL` variable will be added to your environment. The initialization process above allows you to access your cluster without needing to hardcode your authentication credentials.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Indexing your documents\"\n}\n[/block]\nBonsai does not support lazy index creation, so you will need to [create your index](http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/_index_operations.html) before you can send over your documents. You can specify a number of parameters to configure your new index; the code below is the minimum needed to get started:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$indexParams = array();\\n$indexParams['index']  = 'my_index';    //index\\n\\n$client->indices()->create($indexParams);\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nOnce your index has been created, you can add a document by specifying a body (an associative array of fields and data), target index, type and (optionally) a document ID. If you don't specify an ID, Elasticsearch will create one for you:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$params['body']  = array('testField' => 'abc');\\n\\n$params['index'] = 'my_index';\\n$params['type']  = 'my_type';\\n$params['id']    = 'my_id';\\n\\n// Document will be indexed to my_index/my_type/my_id\\n$ret = $client->index($params);\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"php","type":"basic","title":"PHP"}
We recommend using the [official PHP client](https://github.com/elasticsearch/elasticsearch-php), as it is being actively developed alongside Elasticsearch. If you still need to add Bonsai to your app, see our [Installing the Add-on](doc:getting-started-with-heroku) guide. [block:api-header] { "type": "basic", "title": "Adding the Elasticsearch library" } [/block] Like Heroku, we recommend Composer for dependency management in PHP projects, so you'll need to add the Elasticsearch library to your `composer.json` file: [block:code] { "codes": [ { "code": "{\n \"require\": {\n \"elasticsearch/elasticsearch\": \"1.0\"\n }\n}", "language": "json" } ] } [/block] Heroku will automatically add the library when you deploy your code. [block:api-header] { "type": "basic", "title": "Using the library" } [/block] The elasticsearch-php library is configured almost entirely by associative arrays. To initialize your client, use the following code block: [block:code] { "codes": [ { "code": "// Initialize the parameters for the cluster\n$params = array();\n$params['hosts'] = array (\n getenv(\"BONSAI_URL\"),\n);\n\n$client = new Elasticsearch\\Client($params);", "language": "php" } ] } [/block] Note that the host is pulled from the environment. When you add Bonsai to your app, the cluster will be automatically created and a `BONSAI_URL` variable will be added to your environment. The initialization process above allows you to access your cluster without needing to hardcode your authentication credentials. [block:api-header] { "type": "basic", "title": "Indexing your documents" } [/block] Bonsai does not support lazy index creation, so you will need to [create your index](http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/_index_operations.html) before you can send over your documents. You can specify a number of parameters to configure your new index; the code below is the minimum needed to get started: [block:code] { "codes": [ { "code": "$indexParams = array();\n$indexParams['index'] = 'my_index'; //index\n\n$client->indices()->create($indexParams);", "language": "php" } ] } [/block] Once your index has been created, you can add a document by specifying a body (an associative array of fields and data), target index, type and (optionally) a document ID. If you don't specify an ID, Elasticsearch will create one for you: [block:code] { "codes": [ { "code": "$params['body'] = array('testField' => 'abc');\n\n$params['index'] = 'my_index';\n$params['type'] = 'my_type';\n$params['id'] = 'my_id';\n\n// Document will be indexed to my_index/my_type/my_id\n$ret = $client->index($params);", "language": "php" } ] } [/block]