Elastic Search Indexing in php - php

I have a codeigniter application where I try to set up elasticsearch, I have my json which looks like this:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "features",
"_type": "liste",
"_id": "test",
"_score": 1.0,
"_source": {
"mnuActiveSection": "securite.features",
"rowsFeatures": [
{
"id": "50",
"idCategory": "5",
"Name": "01- Data",
"FeatureCategory": "Serie",
"NombrePrivileges": "9",
"ListePrivileges": "Admin"
},
{
"id": "51",
"idCategory": "5",
"Name": "02- Data 2",
"FeatureCategory": "Documentary",
"NombrePrivileges": "3",
"ListePrivileges": "Direction"
},
{
"id": "52",
"idCategory": "5",
"Name": "03- Data 3",
"FeatureCategory": "Films",
"NombrePrivileges": "7",
"ListePrivileges": "Super Admin"
}
]
}
}
]
}
}
I would like to display the list of features in my view, and to search the documents via elastic search. unfortunately I can not display the data json.
This is not what I will like, because I have the impression that I have all my data grouped in a single object array.
To be done I try to use a "foreach" but unfortunately I do not get what I'm looking for. I am stuck at this stage.
I would like for each data of my table to have a different _id for example to better visualize them.
public function liste()
{
$data["mnuActiveSection"] = "securite.features";
$Finder = new FeaturesFinder();
$data["rowsFeatures"] = $Finder->FindAll();
$this->load->library('elasticsearch');
foreach($data["rowsFeatures"] as $d){
$this->elasticsearch->index = 'features';
$this->elasticsearch->create();
$this->elasticsearch->add($type ='liste',$id = 'test',$data);
var_dump($d);
}
}
If anyone could help me find a track I'm interested.
thank you very much

I think what you're trying to do is this:
$i = 0;
foreach($data["rowsFeatures"] as $d){
$i++;
$this->elasticsearch->index = 'features';
$this->elasticsearch->create();
$this->elasticsearch->add($type ='liste',$id = $i,$data);
var_dump($d);
}

Related

How can I encode an array into json with additional data?

I am creating a json file from my array:
$file = json_encode($array);
The json file will look like this:
[
{
"name": "file1.html",
"date": "2019-01-29T20:33:57.000163Z",
"size": "348"
}
{
"name": "file2.xml",
"date": "2019-01-29T20:33:57.000167Z",
"size": "401"
}
{
"name": "file3.html",
"date": "2019-01-29T20:33:57.000171Z",
"size": "1314"
}
]
But I need to create a json file with some little bit different format. The output I need is:
{
"draw": 1,
"recordsTotal": 5000,
"recordsFiltered": 5000,
"data": [
{
"name": "file1.html",
"date": "2019-01-29T20:33:57.000163Z",
"size": "348"
}
{
"name": "file2.xml",
"date": "2019-01-29T20:33:57.000167Z",
"size": "401"
}
{
"name": "file3.html",
"date": "2019-01-29T20:33:57.000171Z",
"size": "1314"
}
]
}
Is this possible with json_encode?
Create a new array with rest of the info and assign current array data into it as well.
$newArray = array(
'draw'=> 1,
'recordsTotal'=> 5000,
'recordsFiltered'=> 5000,
'data'=>$array
);
$file = json_encode($newArray);

Combine $request->request and $request->files arrays

Im posting a multipart with text and files and trying to pass data to form, but these data are separated, so I want to combine them.
$request->request->all()
$request->files->all()
$form = $this->createForm(ParkingType::class, new Parking());
$form->submit($INeedToPassTheCombinedArray);
if ($form->isValid()) {
return $form->getData();
}
Both arrays have the same structure.
For example:
$request->request->all()
{
"name": "Test",
"taxId": "asd12",
"nationality": "england",
"parkings": [{
"total": 4,
"capacity": 928,
"places": [{
"total": 123,
"name": "test",
"address": "test"
},
{
"total": 123,
"name": "test",
"address": "test"
}
]
}]
}
$request->files->all()
{
"parkings": [{
"generalInfo": "File.pdf",
"places": [{
"logo": "File1.png"
},
{
"logo": "File2.png"
}
]
}]
}
I want to combine then in one single array, getting this:
{
"name": "Test",
"taxId": "asd12",
"nationality": "england",
"parkings": [{
"total": 4,
"capacity": 928,
"generalInfo": "File.pdf",
"places": [{
"total": 123,
"name": "test",
"address": "test",
"logo": "File1.png"
},
{
"total": 123,
"name": "test",
"address": "test",
"logo": "File2.png"
}
]
}]
}
I tried using array_merge, but the result is a single array which contain 2 arrays. It is not adding the data of one array in the respective position of the other array.
I want to know if there is some method for do this automatically and elegant.
Hope this will solve your problem
$data1 = json_decode($data,true);// first post array
$data2 = json_decode($file,true);//second file array
foreach($data1['parkings'] as $key=>&$val){ // Loop though one array
$val2 = $data2['parkings'][$key]; // Get the values from the other array
$val += $val2; // combine 'em
foreach($val['places'] as $k=>&$v){
$val3 = $val2['places'][$k]; // Get the values from the other array
$v += $val3; // combine 'em
}
}
echo json_encode($data1);
{
"name": "Test",
"taxId": "asd12",
"nationality": "england",
"parkings": [
{
"total": 4,
"capacity": 928,
"places": [
{
"total": 123,
"name": "test",
"address": "test",
"logo": "File1.png"
},
{
"total": 123,
"name": "test",
"address": "test",
"logo": "File2.png"
}
],
"generalInfo": "File.pdf"
}
]
}

how can two array marge each inside object key are matching it Cross?

I have two array user and game
$user =[
{
"name": "jone",
"id": "100"
},
{
"name": "Peters",
"id": "200"
}
]
$game = [
{
"name": "tennis",
"level": "05",
"user_id": "100"
},
{
"name": "football",
"level": "03",
"user_id": "100"
},
{
"name": "football",
"level": "05",
"user_id": "200"
}
]
I want to get a result like this using PHP / Laravel
$user = [
{
"name": "jone",
"id": "100"
"game": [
{
"name": "tennis",
"level": "05",
"user_id": "100"
},
{
"name": "football",
"level": "03",
"user_id": "100"
}
],
},
{
"name": "Peters",
"id": "200"
"game": [
{
"name": "football",
"level": "05",
"user_id": "200"
}
],
}
],
any one help me
I understand that I should not provide an answer to this "non-question". I still do so, as I think it might carry some learning value.
The idea is, not to cycle each game for each user (as the naive approach would be), as this simply doesn't scale. It is much better to use a matching array and then sort the games into it:
//Prepare matching array
$user_games=array();
foreach ($user as $u) {
$u['game']=array();
$user_games[$u['id']]=$u;
}
//Sort games into matching array
foreach ($game as $g) {
$user_games[$g['user_id']]['game'][]=$g;
}
This way a new game will not create n cycles (n being the number of users), but only one.
print_r($user_games);
creates the desired output. If the user IDs as indices are a problem, just use
print_r(array_values($user_games);

Easiest way of accessing deep-nested value in json response?

I have the following response (I cut the extra short):
{
"meta": {
"current_page": "1",
"last_page": "1",
"per_page": "15",
"total": "1",
"from": "1",
"to": "1"
},
"Products": [
{
"archived": "0",
"committed_stock": "0",
"created_at": "2015-05-10T17:39:53+00:00",
"deleted": "0",
"description": "desc",
"id": "43061710",
"links": {
"Users": [
{
"id": "107534",
"type": "created_by"
}
],
"Attributes": [
{
"id": "31538870"
}
]
}
}
]
}
Everytime I get this response, there will only be one item in "Attributes." What is the easiest way of grabbing this value? So far I have this:
$json = json_decode($json_data);
$json = json_decode($json_data, true);
echo $json["Products"][0]["links"]["Attributes"][0]["id"];
try this:
var_dump( $json->Products[0]->links->Attributes);
the object field could be ether also an object, or an array:
refer to field: $object->object
refer to array's i cell: $object->array[i]
P.S.
please edit the json, it's missing it's end...
You may also want to try some JSON Path libs for PHP: https://github.com/Peekmo/JsonPath

FuelPHP + JSON + Ajax

For example, when I use Model_Trabalhos::query()->related('categoria'), I Get a normal JSON like this:
{
"id": "1",
"categoria_id": "2",
"empresa": "Veja",
"nome": "Veja",
"thumb_pequena": "jobs/digital/veja/thumb.jpg",
"thumb_grande": "jobs/digital/veja/thumb_grande.jpg",
"destaque": "0",
"categoria": {
"id": "2",
"titulo": "Digital"
},
"imagens": {
"1": {
"id": "1",
"url": "jobs/digital/veja/1.png",
"legenda": "",
"job_id": "1"
},
"2": {
"id": "2",
"url": "jobs/digital/veja/2.png",
"legenda": "",
"job_id": "1"
}
}
}
instead, I wanted to receive back this:
[
{
"id": "3",
"categoria_id": "2",
"empresa": "Valor Econômico",
"nome": "Novo Site",
"thumb_pequena": "jobs/digital/valor-economico/thumb.jpg",
"thumb_grande": "jobs/digital/valor-economico/thumb_grande.jpg",
"destaque": "1",
"categoria": {
"id": "2",
"titulo": "Digital"
},
"imagens": [
{
"id": "3",
"url": "jobs/digital/valor-economico/1.png",
"legenda": "",
"job_id": "3"
}
]
}
]
You see? In the second case, it's wrapped in an array, and I wanted to know if there's a function in FuelPHP native that wrap the content to be ordered.
I'm in trouble... I'm using FuelPHP + ORM to get all my records from a database and generating a JSON to use with JavaScript and Ajax, but in Chrome, the JSON is not following the order by defined, is there any workaround for this problem?
If you're wanting your information in a certain way you could follow that query with a foreach loop that would add them to an array of arrays (translated to an array of JSON objects) once converted to JSON.
$query = Model_Trabalhos::query()->related('categoria')
$categories = array();
foreach ($query as $category) {
$categories[] = array(
'id' => $category->id,
...
);
}
return $categories;
It's an old question, but i had the same problem now and colud resolve like the answer that i posted in https://stackoverflow.com/a/34242106/5670195.
In case it is a function that converts into simple array, the objects returned as the relationship of ORM.

Categories