Last active
June 17, 2017 10:44
-
-
Save awaisweb/32a092de5a59c4c07b418affaec1551f to your computer and use it in GitHub Desktop.
ElasticSearch - Create Index, Mappings and Indexing using Php-Curl
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 | |
//First of all, create an Index | |
$baseUrl = 'http://localhost:9200/property'; | |
$curlInit = curl_init(); | |
curl_setopt($curlInit, CURLOPT_URL, $baseUrl); | |
curl_setopt($curlInit, CURLOPT_PORT, '9200'); | |
curl_setopt($curlInit, CURLOPT_TIMEOUT, 200); | |
curl_setopt($curlInit, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curlInit, CURLOPT_FORBID_REUSE, 0); | |
curl_setopt($curlInit, CURLOPT_CUSTOMREQUEST, 'PUT'); | |
$response = curl_exec($curlInit); | |
echo $response; | |
//Second Step: Create Mapping (Add Type and required fields) | |
$baseUrl = "http://localhost:9200/property/listing/_mapping"; | |
$docIndex = array ( "listing" => array ( | |
"properties" => array( | |
"id" => array ( | |
"type" => "string", | |
"index" => "no", | |
"null_value" => "na" | |
), | |
"title" => array ( | |
"type" => "string", | |
"index" => "analyzed", | |
"store" => "yes", | |
"null_value" => "na" | |
), | |
"detail" => array ( | |
"type" => "text", | |
"index" => "analyzed", | |
"store" => "yes", | |
"null_value" => "na" | |
), | |
"memberId" => array ( | |
"type" => "string", | |
"index" => "no", | |
"null_value" => "na" | |
) | |
) | |
)); | |
$jsonData = json_encode($docIndex); | |
$curlInit = curl_init(); | |
curl_setopt($curlInit, CURLOPT_URL, $baseUrl); | |
curl_setopt($curlInit, CURLOPT_PORT, '9200'); | |
curl_setopt($curlInit, CURLOPT_TIMEOUT, 200); | |
curl_setopt($curlInit, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curlInit, CURLOPT_FORBID_REUSE, 0); | |
curl_setopt($curlInit, CURLOPT_CUSTOMREQUEST, 'PUT'); | |
curl_setopt($curlInit, CURLOPT_POSTFIELDS, $jsonData); | |
$response = curl_exec($curlInit); | |
echo $response; | |
//Third Step: Index data - Such as add data to your database table | |
$jsonData = array( | |
"id" => '1', | |
"title" => 'Test Property Title', | |
"detail" => 'My First property listing', | |
"memberId" => '1', | |
); | |
$jsonData = json_encode($jsonData); | |
$baseUrl = 'http://localhost:9200/property/listing/1'; | |
$curlInit = curl_init(); | |
curl_setopt($curlInit, CURLOPT_URL, $baseUrl); | |
curl_setopt($curlInit, CURLOPT_PORT, '9200'); | |
curl_setopt($curlInit, CURLOPT_TIMEOUT, 2000); | |
curl_setopt($curlInit, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curlInit, CURLOPT_FORBID_REUSE, 0); | |
curl_setopt($curlInit, CURLOPT_CUSTOMREQUEST, 'PUT'); | |
curl_setopt($curlInit, CURLOPT_POSTFIELDS, $jsonData); | |
$response = curl_exec($curlInit); | |
curl_close($curlInit); | |
echo $response ; | |
// Forth Step: Search Property Listing | |
$json_doc = array( | |
"query" => [ | |
"match" => [ | |
"title" => "test" | |
] | |
] | |
); | |
$json_doc = json_encode($json_doc); | |
$baseUri = 'http://localhost:9200/property/listing/_search'; | |
$ci = curl_init(); | |
curl_setopt($ci, CURLOPT_URL, $baseUri); | |
curl_setopt($ci, CURLOPT_PORT, '9200'); | |
curl_setopt($ci, CURLOPT_TIMEOUT, 200); | |
curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ci, CURLOPT_FORBID_REUSE, 0); | |
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'GET'); | |
curl_setopt($ci, CURLOPT_POSTFIELDS, $json_doc); | |
$response = curl_exec($ci); | |
print_r($response); | |
exit; | |
//Remove Index and all its types and docs | |
$baseUrl = 'http://localhost:9200/property?pretty'; | |
$curlInit = curl_init(); | |
curl_setopt($curlInit, CURLOPT_URL, $baseUrl); | |
curl_setopt($curlInit, CURLOPT_PORT, '9200'); | |
curl_setopt($curlInit, CURLOPT_TIMEOUT, 2000); | |
curl_setopt($curlInit, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curlInit, CURLOPT_FORBID_REUSE, 0); | |
curl_setopt($curlInit, CURLOPT_CUSTOMREQUEST, 'DELETE'); | |
curl_setopt($curlInit, CURLOPT_POSTFIELDS, $jsonData); | |
$response = curl_exec($curlInit); | |
curl_close($curlInit); | |
echo $response ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment