Skip to content

Instantly share code, notes, and snippets.

@silverarm
Last active February 17, 2016 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silverarm/9657134efd9468832b7a to your computer and use it in GitHub Desktop.
Save silverarm/9657134efd9468832b7a to your computer and use it in GitHub Desktop.
<?php
$main_index = "client_docs";
$main_type = "documents";
$hosts = [
'http://localhost'
];
require __DIR__ . '/vendor/autoload.php';
$client = Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
// delete client index
$params = ['index' => $main_index];
// check if client index exists before trying to delete it
$index_exists = $client->indices()->exists ( $params );
if ( $index_exists ) {
try {
$response = $client->indices()->delete ( $params );
}
catch ( ElasticsearchException $e ) {
echo '\n\nError occurred when trying to delete an index.';
}
echo "\n\nResponse after deleting $main_index: "; print_r ( $response);
}
// Create client index
$params = ['index' => $main_index];
try {
$response = $client->indices()->create($params);
}
catch ( ElasticsearchException $e ) {
echo '\n\nError occurred when trying to create an index.';
}
echo "\n\nResponse after creating $main_index: "; print_r ( $response);
$json = '
{
"index":"' . $main_index . '",
"type":"' . $main_type . '",
"body":{
"' . $main_type . '":{
"properties":{
"file":{
"type":"attachment",
"fields": {
"content": {
"type": "string",
"term_vector":"with_positions_offsets",
"store": true
}
}
}
}
}
}
}';
$params = json_decode ( $json );
// Update the index mapping
try {
$response = $client->indices()->putMapping ( $params );
}
catch ( ElasticsearchException $e ) {
echo '\n\nError occurred when trying to create an index.';
}
if ( !is_array ( $response ) ) {
$response = json_decode ( $response, true );
}
echo "\n\nResponse after updating mapping for $main_index: "; print_r ( $response);
if ( $response['acknowledged'] == true ) {
$files_to_index[1] = "Test.docx"; // content of file is "This is a small test."
$files_to_index[2] = "Test.doc"; // content of file is "This is a small test."
foreach ( $files_to_index as $counter => $file_to_index ) {
$file_to_index = $_SERVER['DOCUMENT_ROOT'] . "/Documents/" . $file_to_index;
$file_contents = file_get_contents ( $file_to_index );
$file_encoded = base64_encode ( $file_contents );
$mime_type = check_mime ( $file_to_index );
$json = '
{
"index":"' . $main_index .'",
"type":"' . $main_type . '",
"id":"' . $counter . '",
"body":{
"file":{
"_content":"' . $file_encoded . '",
"_name":"' . $file_to_index .'",
"_content_type":"' . $mime_type . '",
"_indexed_chars":"-1",
"_detect_language":"true"
}
}
}';
$params = json_decode ( $json );
// Execute request
try {
$response = $client->index($params);
}
catch ( ElasticsearchException $e) {
echo '\n\nError occurred when trying to add document to index.';
}
echo "\n\nResponse after adding a document - $counter - to the $main_index: "; print_r ( $response);
}
}
// Search for text in an attachment
$search_text = "test";
$json = '
{
"index":"' . $main_index . '",
"type":"' . $main_type . '",
"body":{
"query":{
"match":{
"file.content": "' . $search_text . '"
}
},
"highlight": {
"fields": {
"file.content": {
}
}
}
}
}';
$params = json_decode ( $json );
// sleep for 10 seconds
sleep(10);
$response = $client->search ( $params );
echo "\n\nResponse when searching for text in $main_index: "; print_r ( $response);
function check_mime ( $filename ) {
$finfo = finfo_open ( FILEINFO_MIME_TYPE );
$mime = finfo_file ( $finfo, $filename );
finfo_close ( $finfo );
return $mime;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment