Elasticseach doesn't map all given fields [PHP] - php

I'm working with Elasticsearch-PHP client. I want to index my datas, but I have a problem with mapping. There is a problem: I create my data array, everything works fine, but when I add this array to my index body => my_data_array some datas show up, but not all of them. I don't know why. I just digged and try all of following steps but nothing changed.
I just attached my snippets.
This my controller file where I index datas:
{
$params = ['body' => []];
foreach($all_ads as $key => $ads){
$params['body'][] = [
'index' => [
'_index' => 'demo_data',
'_type' => 'demo',
'_id' => $ads->id
]
];
$params['body'][] = $ads->indexParams();
}
$responses = $client->bulk($params);
this is result json object:
"response": [
{
"id": 85345,
"old_id": "5088063",
"user_id": "2706",
"category_id": "15",
"type": "3",
"title": array[3],
"slug": "",
"sub_region_id": "8",
"condition": "1",
"username": "John Doe",
"price": "82000",
"price_type": "1",
"no_phone": "0",
"views": "29",
"hot": "0",
"vip": "0",
"price_measure": "0"
}
I have a datafield and it is not visible here.
Its data mapping structure
'data' => [
'type' => 'object',
'properties' => [
'key_id' => ['type' => 'integer'],
'value_id' => ['type' => 'integer'],
'key' => ['type' => 'keyword'],
'value' => ['type' => 'keyword']
]
]
How can I fix it?
I just check some question from here and github issue forum but nothing helps. Thanks!

You have to follow solution of the question. Its maybe helpful for you.
Elasticsearch mapping not working as expected
Ask Question

Related

Wrong order of keys in received payload with Spatie Webhook Client in Laravel

I'm trying to send this data in a request:
{
"TABLENAME":"USERS",
"DATA": [
{
"ID": "1",
"NAME":"JOHN",
"AGE":"33",
"GENDER":"M",
"DATE":"2000000",
"CITY":"LONDON"
},
{
"ID": "2",
"NAME":"MARK",
"AGE":"35",
"GENDER":"M",
"DATE":"2000000",
"CITY":"MUNICH"
}
]
}
I'm retrieving all the hook data and storing it in my hook handler like:
$data = $this->webhookCall;
And access the payload through:
$payload = $data->payload;
The $payload first of all is an array and the key values are in reverse order, and looks like this:
array (
'DATA' =>
array (
0 =>
array (
'ID' => '1',
'AGE' => '33',
'CITY' => 'LONDON',
'DATE' => '2000000',
'NAME' => 'JOHN',
'GENDER' => 'M',
),
1 =>
array (
'ID' => '2',
'AGE' => '35',
'CITY' => 'MUNICH',
'DATE' => '2000000',
'NAME' => 'MARK',
'GENDER' => 'M',
),
),
'TABNAME' => 'USERS',
)
Also when trying to save to db:
$hook = new USER;
$hook->name = $data->name;
$hook->url = $data->url;
$hook->headers = json_encode($data->headers);
$hook->payload = json_encode($data->payload);
$hook->exception = $data->exception;
$hook->save();
The data is still with the wrong key order:
{"DATA": [{"ID": "1", "AGE": "33", "CITY": "LONDON", "DATE": "2000000", "NAME": "JOHN", "GENDER": "M"}, {"ID": "2", "AGE": "35", "CITY": "MUNICH", "DATE": "2000000", "NAME": "MARK", "GENDER": "M"}], "TABNAME": "USERS"}
I have tried to reverse the $payload array:
$hook->payload = json_encode(array_reverse($data->payload));
but the data is still in the wrong order in the db.
Can someone please tell me what is wrong here and why this behavior?
And also why does an object sent in a request is being converted to an array? Is this normal behavior for php or specific for this package that I'm using?
Thanks

Retrieving data from two collections in MongoDB with PHP having _id as a value already

I'm trying to use aggregate to return token and kyc_status in an API call.
I have below data in tokens collection
{
"_id": {
"$oid": "5fca628feb1f03d82c76e85f"
},
"user_id": "5fca62d0eb1f03d82c76e860",
"token": "token_here",
"valid_till": "2021-01-03",
"created": "2020-12-04 20:27:05",
"modified": "2020-12-04 20:27:05"
}
then I have below data in users collection
{
"_id": {
"$oid": "5fca62d0eb1f03d82c76e860"
},
"seller_type_id": "",
"status": {
"$numberInt": "1"
},
"kyc_status": "pending",
"username": "user_name",
"password": "password_here",
"email": "email_here",
"primary_contact": "primary",
"secondary_contact": "secondary"
}
Now i have id 5fca62d0eb1f03d82c76e860 in $user_id PHP variable
I'm using the below code to achieve to show kyc_status and token
$result = $collection->aggregate( [
[ '$lookup' => [
'from' => 'users',
'localField' => '_id',
'foreignField' => 'user_id',
'as' => 'user_role'
]
],
[ '$unwind' => '$user_info' ],
[
'$project' => [ 'kyc_staus' => '$user_info.kyc_status' ]
]
]
);
I do not get any out put when I print the $result variable it shows me a whole lot of data but does not show what I want, I searched and tried most of StackOverflow answers to the same but did not get any.
Can anyone let me know where i am goring wrong / what should i do to get the desired output?

Laravel collection formatting key to field

I have a laravel collection that has this format:
{
"24385528032901": [
{
"time": "2020-06-30T22:30:00.000000Z",
"conso_prod": "Prod",
"meter_id": "24385528032901",
"delta": "0",
},
{
"time": "2020-06-30T23:00:00.000000Z",
"conso_prod": "Prod",
"meter_id": "24385528032901",
"delta": "2",
}
],
"24385528032777": [
{
"time": "2020-06-30T22:30:00.000000Z",
"conso_prod": "Prod",
"meter_id": "24385528032777",
"delta": "0",
},
{
"time": "2020-06-30T23:00:00.000000Z",
"conso_prod": "Prod",
"meter_id": "24385528032777",
"delta": "5",
}
], etc.
}
I'm having an hard time converting it to a chartJS linechart graph format:
[
[
'label' => '24385528032901',
'data' => $measures->map->delta,
], [
'label' => '24385528032777',
'data' => $measures->map->delta,
],
]
I know there is a collection method to do this, but can't find it anymore. Anyone ?
If i got your question right and you want to get meter_id and delta as label and data you can get meter_id and delta fields only from your collection then use collection's map helper to map these fields to your new field names like so
Model::get(['meter_id', 'delta'])->map(function (Model $model) {
return [
'label' => $model->meter_id,
'data' => $model->delta,
];
})->toArray()

Update single field value in nested array without modifying other ones?

I am trying to modify single field value, but whenever I do it it erases all other fields leaving updated field only, how can I modify a single field witouth affecting the other ones?
so far this is what I'm doing:
//
$updateResult2 = $coll_rutas->updateOne(
/* LIMIT TO ROUTE ID */
[
"_id" => new \MongoDB\BSON\ObjectId($ruta_id)
],
/* UPDATE VALUE */
[
'$set' => [
'destinos.$[d]' => [
"horas" => "1",
"minutos" => "15",
"millas" => "150",
"orden" => "1",
"notas" => "demo notes",
],
],
],
/* WHERE FILTER */
[
"upsert" => true,
'arrayFilters' => [
['d.ciudad_id' => 47]
]
]
);
//var_dump($updateResult2); exit;
This is the original mongo Object:
"destinos": [
{
"ciudad_id": "47",
"nombre_destino": "Salt Lake City, Utah",
"horas": "0",
"minutos": "0",
"millas": "0",
"orden": 1,
"notas": null,
"origenes": [
{
"ciudad_id": "45",
"nombre_origen": "Provo, Utah",
"orden": 1,
"notas": null,
"base_price": 0
}
]
}
],
With my changes it erases origenes array, ciudad_id and nombre destino, leaving it this way:
"destinos": [
{
"horas": "1",
"minutos": "15",
"millas": "150",
"orden": "1",
"notas": "demo notes"
]
}
],
With this code:
'$set' => [
'destinos.$[d]' => [
"horas" => "1",
"minutos" => "15",
"millas" => "150",
"orden" => "1",
"notas" => "demo notes",
],
]
you are targeting the destinos.$[d] element and replacing all of its contents with the new data structure.
You need to use the .(dot) notation to target each element in the nested array. It's a bit verbose but this would work:
'$set' => [
'destinos.$[d].horas' => "1",
'destinos.$[d].minutos' => "15",
'destinos.$[d].millas' => "150",
'destinos.$[d].orden' => "1",
'destinos.$[d].notas' => "demo notes"
]

How do I extract subdocument in laravel mongodb

Hello Good Developers,
I am using jenssegers/laravel-mongodb package to query my MongoDB from Laravel.
Here's Fiddle for my query: https://mongoplayground.net/p/qzbNN8Siy-3
I have following JSON
[{
"id": "GLOBAL_EDUCATION",
"general_name": "GLOBAL_EDUCATION",
"display_name": "GLOBAL_EDUCATION",
"profile_section_id": 0,
"translated": [
{
"con_lang": "US-EN",
"country_code": "US",
"language_code": "EN",
"text": "What is the highest level of education you have completed?",
"hint": null
},
{
"con_lang": "US-ES",
"country_code": "US",
"language_code": "ES",
"text": "\u00bfCu\u00e1l es su nivel de educaci\u00f3n?",
"hint": null
}...
{
....
}
]
I am trying to run following command
db.collection.find({ 'id': "GLOBAL_EDUCATION" },{_id:0, id:1, general_name:1, translated:{ $elemMatch: {con_lang: "US-EN"} }})
Expecting result like this
[
{
"general_name": "GLOBAL_EDUCATION",
"id": "GLOBAL_EDUCATION",
"translated": [
{
"con_lang": "US-EN",
"country_code": "US",
"hint": null,
"language_code": "EN",
"text": "What is the highest level of education you have completed?"
}
]
}
]
Everything is fine while query directly in MoDB but issue arise when I am trying this in Laravel.
I've tried every possible known function from MongoDB package. but Not able to do this.
here's my Array
$findArray = [
[
'id' => "GLOBAL_EDUCATION",
],
[
'_id' => 0,
'id' => 1,
'general_name' => 1,
'translated' => [
'$elemMatch' => ['con_lang' => "US-EN"]
],
]
];
$model = GlobalQuestions::raw()->find($findArray) //OR
$data = GlobalQuestions::raw(function($collection) use ($findArray){
return $collection->find($findArray);
});
What I am doing wrong here, is this kind of Find() not possible here and I've to do this by aggregation?
Since no-one answered this, I am posting the solution if someone is having the same issue.
Doing some more R&D on the same I was able to do this using where and Project as well by Aggregation Pipelines.
----- Using Where() and Project() ------
$projectArray = [
'_id' => 0,
'id' => 1,
'general_name' => 1,
'translated' => [
'$elemMatch' => ['con_lang' => "FR-FR"]
],
];
$data = GlobalQuestions::where('id', '=', 'GLOBAL_EDUCATION')
->project($projectArray)
->get();
--- Using Aggregation and $unwind ---
$data = GlobalQuestions::raw(function($collection) {
return $collection->aggregate([
[
'$match' => [
'id' => "GLOBAL_EDUCATION"
]
],
[
'$unwind' => '$translated',
],
[
'$match' => [
'translated.con_lang' => "US-EN"
]
],
[
'$project' => [
'_id'=> 0,
'id'=> 1,
'general_name' => 1,
'translated' => 1,
]
]
]);
})->first();

Categories