Created
August 30, 2011 22:44
-
-
Save phillro/1182306 to your computer and use it in GitHub Desktop.
Adding a document and searching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//This example was adapted from the documentation available at https://github.com/ruflin/Elastica | |
set_include_path(get_include_path() . PATH_SEPARATOR . realpath(dirname(__FILE__) . '/Elastica/lib')); | |
function elasticsearch_autoload($class) { | |
$file = str_replace('_', '/', $class) . '.php'; | |
require_once $file; | |
} | |
spl_autoload_register('elasticsearch_autoload'); | |
defined('BASE_PATH') || define('BASE_PATH', realpath(dirname(__FILE__))); | |
$userName='YOURUSERNAME'; | |
$password='YOURPASSWORD'; | |
$indexName='YOURINDEXNAME'; | |
$config = array( | |
'host' => 'esh01.elasticsearchhq.com', | |
'port' => '9200', | |
'transport' => 'https', | |
); | |
$client = new Elastica_Client($config); | |
$client->addHeader('Authorization', 'Basic '.encodeAuth($userName,$password)); | |
$index = $client->getIndex($indexName); | |
//getType will create if one does not exist | |
$type = $index->getType('user'); | |
//Add a document with an id of 1 | |
$doc1 = new Elastica_Document(1, | |
array('username' => 'hans', 'test' => array('2', '3', '5')) | |
); | |
$type->addDocument($doc1); | |
//Add a new document with an id of 2. Using an existing id will update. | |
$doc2 = new Elastica_Document(2, | |
array('username' => 'hans', 'test' => array('2', '3', '5')) | |
); | |
$type->addDocument($doc2); | |
$resultSet = $index->search(''); | |
echo var_export($resultSet, true); | |
function encodeAuth($userName, $password){ | |
$encodedAuth = base64_encode($userName.':'.$password); | |
return $encodedAuth; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment