Skip to content

Instantly share code, notes, and snippets.

@violetyk
Created February 7, 2013 09:43
Show Gist options
  • Save violetyk/4729884 to your computer and use it in GitHub Desktop.
Save violetyk/4729884 to your computer and use it in GitHub Desktop.
<?php
// import
$ch = curl_init();
$index = 'test'; // like database
$type = 'users'; // like table
$id = 1;
$data = array(
'name' => 'violetyk',
'email' => '[email protected]',
'profile' => '柴犬と暮らしています。日本酒と銭湯とVimが好きです。',
);
curl_setopt_array($ch, array(
CURLOPT_URL => sprintf('http://localhost:9200/%s/%s/%d', $index, $type, $id),
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => json_encode($data),
));
$result = curl_exec($ch);
curl_close($ch);
if ($result !== false) {
var_dump(json_decode($result, 'UTF-8'));
}
// search ---
$ch = curl_init();
$query['query'] = array(
'match' => array(
'name' => 'violetyk'
)
);
curl_setopt_array($ch, array(
CURLOPT_URL => sprintf('http://localhost:9200/%s/%s/_search', $index, $type),
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode($query),
));
$result = curl_exec($ch);
curl_close($ch);
if ($result !== false) {
var_dump(json_decode($result, 'UTF-8'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment