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
+18.5k Golang : Read input from console line
+7.8k Golang : Emulate NumPy way of creating matrix example
+6.3k Golang : How to validate ISBN?
+15.6k Golang : Get file permission
+8k Golang : How to check if input string is a word?
+8.5k Golang : Build and compile multiple source files
+5k Golang : Reclaim memory occupied by make() example
+12.7k Swift : Convert (cast) Int to String ?
+17.1k Golang : Defer function inside init()
+7.1k Golang : How to stop user from directly running an executable file?
+20.2k Golang : Underscore or snake_case to camel case example
+19.8k nginx: [emerg] unknown directive "passenger_enabled"