Skip to content

Instantly share code, notes, and snippets.

@mbfisher
Last active August 29, 2015 14:01
Show Gist options
  • Save mbfisher/a7f2a865cadb69f1afc3 to your computer and use it in GitHub Desktop.
Save mbfisher/a7f2a865cadb69f1afc3 to your computer and use it in GitHub Desktop.
Simple Elasticsearch interaction
<?php
require 'vendor/autoload.php';
$client = new Elasticsearch\Client([
'hosts' => ['127.0.0.1']
]);
echo "Resetting...\n";
$index = ['index' => 'freedomclinics.com'];
if ($client->indices()->exists($index)) {
$client->indices()->delete($index);
}
$client->indices()->create($index);
echo "Mapping...\n";
$mapping = [
'index' => 'freedomclinics.com',
'type' => 'document',
'body' => [
'document' => [
'properties' => [
'content' => [
'type' => 'attachment',
'fields' => [
'title' => ['store' => 'yes'],
'description' => ['store' => 'yes'],
'content' => [
'term_vector' => 'with_positions_offsets',
'store' => 'yes'
]
]
]
]
]
]
];
$client->indices()->putMapping($mapping);
echo "Indexing...\n";
$index = [
'body' => [
'title' => 'FreedomClinics.com | Home',
'description' => 'FreedomClinics.com',
'content' => base64_encode(file_get_contents('http://freedomclinics.com'))
],
'index' => 'freedomclinics.com',
'type' => 'document',
'id' => '/'
];
$client->index($index);
$index = [
'body' => [
'title' => 'Corporaate Brochure',
'description' => 'FreedomClinics Corporate Brochure',
'content' => base64_encode(stream_get_contents(fopen('http://freedomclinics.com/static/docs/guides/freedom-corporate-brochure.pdf', 'rb')))
],
'index' => 'freedomclinics.com',
'type' => 'document',
'id' => '/docs/guides/freedom-skin-guide.pdf'
];
$client->index($index);
echo "Querying...\n";
$query = [
'index' => 'freedomclinics.com',
'type' => 'document',
'body' => [
'query' => [
'match' => [
'content' => 'back pain'
]
]
]
];
$result = $client->search($query);
print_r($query);
echo "HITS\n";
foreach ($result['hits']['hits'] as $hit) {
echo $hit['_id'],"\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment