PHP : How to parse ElasticSearch JSON ?
ElasticSearch returns search result in JSON format. To parse the items from the search result in PHP first you need to decode the JSON data
$decoded = json_decode($json_data);
and from the decoded data, you can scan the data array for the granular information. For example, if I wanted the title
from the returned search result
I would do something like this :
<?php
$results = $decoded->hits->hits;
foreach ($results as $item) {
$id = $item->_id;
$title = $item->_source->title; // get the title
$short_description = $item->_source->short_description;
}
?>
NOTE : In case the search result from ElasticSearch is not recognize as JSON format.... you can always use json_encode
function such as the example code:
$json_data = json_encode($this->elasticsearch->advancedquery("index_type", $searchquery));
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+16.5k Golang : How to log each HTTP request to your web server?
+7.3k Swift : Convert (cast) Character to Integer?
+6.8k Golang : How to execute code at certain day, hour and minute?
+15.5k Golang : Gzip file example
+31.3k Golang : How to check if a date is within certain range?
+13.2k Golang : Overwrite previous output with count down timer
+10.8k Golang : Detect user location with HTML5 geo-location
+4.8k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error
+24.1k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+7.8k Golang : Get final balance from bit coin address example
+7.2k Golang : Oanda bot with Telegram and RSI example