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
+8.4k Golang : Number guessing game with user input verification example
+9.2k Golang : How to use Gorilla webtoolkit context package properly
+8.1k Golang : What fmt.Println() can do and println() cannot do
+15.6k Golang : Get all local users and print out their home directory, description and group id
+29.8k Golang : Saving(serializing) and reading file with GOB
+5.4k Golang : Customize scanner.Scanner to treat dash as part of identifier
+11.6k Golang : Concatenate (combine) buffer data example
+15.7k Golang : How to convert(cast) IP address to string?
+22.3k Golang : Repeat a character by multiple of x factor
+22.3k Golang : Match strings by wildcard patterns with filepath.Match() function
+20.5k Golang : How to get own program name during runtime ?
+8k Golang : How to feed or take banana with Gorilla Web Toolkit Session package