{"_id":"567af4c7c952030d0038c788","parentDoc":null,"user":"5633ec9b35355017003ca3f2","__v":13,"category":{"_id":"567afeb8802b2b17005ddea0","project":"5633ebff7e9e880d00af1a53","pages":["56b3cd7ac25dea2b00b0caf6"],"version":"5633ec007e9e880d00af1a56","__v":1,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-12-23T20:06:16.013Z","from_sync":false,"order":2,"slug":"quickstart-guides-1","title":"Quickstart Guides"},"project":"5633ebff7e9e880d00af1a53","version":{"_id":"5633ec007e9e880d00af1a56","project":"5633ebff7e9e880d00af1a53","__v":15,"createdAt":"2015-10-30T22:15:28.105Z","releaseDate":"2015-10-30T22:15:28.105Z","categories":["5633ec007e9e880d00af1a57","5633f072737ea01700ea329d","5637a37d0704070d00f06cf4","5637cf4e7ca5de0d00286aeb","564503082c74cf1900da48b4","564503cb7f1fff210078e70a","567af26cb56bac0d0019d87d","567afeb8802b2b17005ddea0","567aff47802b2b17005ddea1","567b0005802b2b17005ddea3","568adfffcbd4ca0d00aebf7e","56ba80078cf7c9210009673e","574d127f6f075519007da3d0","574fde60aef76a0e00840927","57a22ba6cd51b22d00f623a0"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.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":2,"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 Addon](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 Addon](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]