Last active
August 29, 2015 14:01
-
-
Save mbfisher/a7f2a865cadb69f1afc3 to your computer and use it in GitHub Desktop.
Simple Elasticsearch interaction
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 | |
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