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
+18k Golang : Logging with logrus
+16.7k Golang : How to save log messages to file?
+5.8k Golang : Dealing with backquote
+10.2k Golang : Get local time and equivalent time in different time zone
+9.3k Golang : Eroding and dilating image with OpenCV example
+6.2k Golang : Combine slices of complex numbers and operation example
+11.8k Golang : convert(cast) string to integer value
+13.8k Golang : Convert IP version 6 address to integer or decimal number
+22.5k Golang : Calculate time different
+15.2k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+14k Golang : Parsing or breaking down URL
+18.1k Golang : Display list of time zones with GMT