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
+10.5k Golang : Generate random integer or float number
+5.3k Golang : Experimental Jawi programming language
+15k Golang : Find commonalities in two slices or arrays example
+14.1k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+5.9k List of Golang XML tutorials
+7.6k Golang : Process json data with Jason package
+7k Default cipher that OpenSSL used to encrypt a PEM file
+16.8k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+10.1k Golang : Convert octal value to string to deal with leading zero problem
+5.4k PHP : Hide PHP version information from curl
+10.4k Golang : How to profile or log time spend on execution?
+4.9k MariaDB/MySQL : Form select statement or search query with Chinese characters