Elasticsearch GeoMapping with PHP not working - php

I have the following mapping in Elasticsearch 2.2
"clinics" : {
"mappings" : {
"clinic" : {
"properties" : {
"address_1" : {
"type" : "string",
"analyzer" : "standard"
},
"city" : {
"type" : "string"
},
"country" : {
"type" : "string"
},
"id" : {
"type" : "long"
},
"location" : {
"type" : "geo_point"
},
"name" : {
"type" : "string",
"analyzer" : "standard"
},
"state" : {
"type" : "string"
},
"zipcode" : {
"type" : "string",
"analyzer" : "standard"
}
}
}
}
},
I am using PHP to add to the index with Elastiquent5. my toArray() is:
public function toArray()
{
$array = parent::toArray();
$object = new \stdClass();
$object->lat = $array['latitude'];
$object->lon = $array['longitude'];
$array['location'] = $object;
return $array;
}
When I try to add the item to the index i get the following error:
{"error":{"root_cause":[{"type":"remote_transport_exception","reason":"[Solarman][10.0.2.15:9300][indices:data/write/index[p]]"}],"type":"illegal_argument_exception","reason":"[location] is defined as an object in mapping [clinics] but this name is already used for a field in other types"},"status":400}
In json format it yields correctly, in php array it is:
array:10 [
"name" => "Clinic A"
"address_1" => "123 Main St"
"city" => "NY"
"state" => "NY"
"zipcode" => "11111"
"country" => "US"
"id" => 6968
"location" => {#526
+"lat": 45.2116373
+"lon": -72.1891546
}
]
If I manually json_encode this and use the curl -XPUT 'http://localhost:9200' -d ''
it goes through - however the PHP doesnt. Any idea where I went wrong?
clarification
My goal is to insert the coordinate that my model data is extracting to map to the location field inside the index since I already designed as a geo_point type as defined by my mapping
I do not want to designate a new field in order to solve this. Secondary note: as I mentioned doing a put with the json will yield successfully however not as a php object out with the php drivers for elastic search which yielded the error when it tried to create the document

As mentioned in the error message, [location] is defined as an object in mapping [clinics] but this name is already used for a field in properties. Changing the variable name like the following might work.
public function toArray()
{
$array = parent::toArray();
$object = new \stdClass();
$object->lat = $array['latitude'];
$object->lon = $array['longitude'];
$array['location_ob'] = $object;
return $array;
}

Related

PHP Multi Array Merging JSON

I've got JSON output that comes to me like this
{ [objects{}] [to] [from] [total] }
and due to the API I'm pulling this from I can only pull 500 records at a time, what I'm trying to do is merge the objects part of multiple json outputs into a larger recordset.
I've tried using array_merge with json_encode and json_decode but it's not working, I'm assuming it's because of the other arrays (to/from/total) that are impacting my ability to merge.
Any idea how I could go about this, I was thinking of a line by line merge, but issue with this is different types of arrays have different elements (I'm only trying to merge arrays with the same elements).
Edit: hmmm json formatting didn't come through well even with code tags
{
"objects" : [ {
"uid" : "f7534a54-fd17-4bc2-9a26-8a567082cc86",
"name" : "host_10.1.1.1",
"type" : "host",
"domain" : {
"uid" : "8fe7e918-8530-4a7d-851c-bf8a7c3889c9",
"name" : "Lab-Ext_VPN",
"domain-type" : "domain"
},
"ipv4-address" : "10.1.1.1"
}, {
"uid" : "1ba63e41-4d2c-48ea-a970-66a806089ff5",
"name" : "host_10.1.1.10",
"type" : "host",
"domain" : {
"uid" : "8fe7e918-8530-4a7d-851c-bf8a7c3889c9",
"name" : "Lab-Ext_VPN",
"domain-type" : "domain"
},
"ipv4-address" : "10.1.1.10"
}, {
"uid" : "d798b126-25de-4138-b25d-b1edfe83d259",
"name" : "host_10.1.1.100",
"type" : "host",
"domain" : {
"uid" : "8fe7e918-8530-4a7d-851c-bf8a7c3889c9",
"name" : "Lab-Ext_VPN",
"domain-type" : "domain"
},
"ipv4-address" : "10.1.1.100"
}, {
"uid" : "247e1b2e-a45d-457e-87f7-78a6acdeea9f",
"name" : "host_10.1.1.101",
"type" : "host",
"domain" : {
"uid" : "8fe7e918-8530-4a7d-851c-bf8a7c3889c9",
"name" : "Lab-Ext_VPN",
"domain-type" : "domain"
},
"ipv4-address" : "10.1.1.101"
}, {
"uid" : "381e59a0-2094-422a-b538-8f607ec58384",
"name" : "host_10.1.1.102",
"type" : "host",
"domain" : {
"uid" : "8fe7e918-8530-4a7d-851c-bf8a7c3889c9",
"name" : "Lab-Ext_VPN",
"domain-type" : "domain"
},
"ipv4-address" : "10.1.1.102"
} ],
"from" : 1,
"to" : 5,
"total" : 614
`
You have 4 elements in the given json, object to from and total
I am assuming, you hav to merge the object from two JSONs, get minimum value of from, get maximum value from to and sum of total
function my_merge($json_1,$json_2){
$array_1 = json_decode($json_1,true);
$array_2 = json_decode($json_2,true);
return ['objects'=>array_merge($array_1['objects'],$array_2['objects']),'from'=>min($array_1['from'],$array_2['from']),'to'=>max($array_1['to'],$array_2['to']),'total'=>($array_1['total']+$array_2['total'])];
}
Above, first, json_decode with second parameter true to decode and convert JSON to array.
Second, merge the index [0] of both $array_1 and $array_2 as it contains the object. Do the min max and summation for total and return the resultant array.

conversion to PHP, update and insert in subdocument

I am having trouble converting the statement to php
Here is the structure of the data:
db.rooms.find({"_id" : "1620888265"}).pretty();
{
"_id" : "1620888265",
"start" : 1620988265,
"end" : null,
"users" : [
{
"name" : "owner",
"score" : 0.9,
"singin" : 1620895469
},
{
"name" : "user",
"singin" : 1620895769
}
],
"questions" : [
{
"id": 1
"title" : "X",
"released" : false
}
]
}
Here the command that works for me:
db.rooms.updateOne({"_id" : "1620970325obn","questions.ident":2},{$set : {"questions.$.released":true,"questions.$.time":'1415'}});
Once translated to PHP (not working for me):
$rooms_db->updateOne(array("_id" => $room,"questions.ident"=>$ident), array ('$set'=>array('questions.$.released' => true,'questions.$.time'=>$time)));
The problem has been that the variable "$ident" was a number, but for some reason PHP treated it as a String. This can be seen with var_dump ($ident) .
"questions.ident"=>(int)$ident), this fixed the problem

Insert array in array at mongodb with php

MongoDB schema:
{
"_id" : ObjectId("60057ff5c34000008a00214a"),
"Name" : "Test Store",
"AFM" : "099360666",
"DOY" : "ffa",
"Address" : "faf",
"TEL" : "sss",
"Products" : [
{
"_id" : ObjectId("600584b8c34000008a00214d"),
"pID" : "099360666/1",
"pNAME" : "old scholl 2",
"pPRICE" : "55",
"pBRAND" : "vans",
"pDESCRIPTION" : "eco frinedly",
"pSIZE" : "45,44",
"pDEPARTMENT" : "Men/Shoes/Trainers",
"pTHUMBNAIL" : "http://127.0.0.1/pricedoc/assets/img/products/p1.jpg",
"pQUANTITY" : "7",
"pCOMMENTS" : {
"Comment" : [ ]
}
}
],
"nextProductCounter" : 2
}
I want to add comments through a html form using php to add comment at db.
The best I've got is this, but seems to not work
$new_comm = array(
"username" => "guest",
"date" => new \MongoDB\BSON\UTCDateTime(strtotime(date('d-m-Y 00:00'))),
"text" => "excellent product",
"rating" => "4"
);
$collection->updateOne(
array("Products._id" => new MongoDB\BSON\ObjectId("60058012c34000008a00214b")),
array('$push' => array("Products.pCOMMENTS" => array("Comment"=>$new_comm)))
);
Why the pCOMMENTS array isn't updating?
Expected results
"pCOMMENTS" : {
"Comment" : {
"username" : "guest",
"date" : "18/1/2021",
"text" : "excellent product",
"rating" : "4"
}
}
You need to use positional operator
Reference
$collection->updateOne(
array("Products._id" => new MongoDB\BSON\ObjectId("60058012c34000008a00214b")),
array('$push' => array("Products.$.pCOMMENTS" => array("Comment"=>$new_comm)))
);

MongoDB - PHP modifying array's object values

I have a field which stores an array of objects. I want to modify the object's "status" value depending on the "id" value the object possesses.
"authors" : [
{
"id" : "18",
"first_name" : "Buddhika",
"last_name" : "Chathuranga",
"$$hashKey" : "object:35",
"status" : NumberLong(0)
},
{
"id" : "3", // search for this number
"first_name" : "Pasindu",
"last_name" : "Priyanath",
"$$hashKey" : "object:43",
"status" : NumberLong(0) // modify this to 1
}
],
We can leverage positional update in MongoDB to update values inside array.
Mongo Positional Update
Please find script below.
db.authors.update(
{"authors.id": "3"},
{ $set: { "authors.$.status" : NumberLong(1) }}
)
Update in Mongo DB php:
db.collection.update( criteria, objNew, upsert, multi );
Detail Explanation

PHP MongoDB update doesn't work

{
"__v" : 2,
"_id" : ObjectId("54dc4fd10ac1f6a066a0646c"),
"desc" : "test1",
"lists" : [
{
"index" : 1,
"text" : "__point",
"_id" : ObjectId("54dc4fda0ac1f6a066a0646d"),
"createdAt" : ISODate("2015-02-12T07:01:46.390Z")
},
{
"index" : 2,
"text" : "__point",
"_id" : ObjectId("54dc4fdd0ac1f6a066a0646e"),
"createdAt" : ISODate("2015-02-12T07:01:49.668Z")
}
],
"name" : "test" }
The test array looks like this. i want to add 'category' field and add value '0' in lists checking _id.
i tried
$collection->update(array('_id' => new MongoId("54dc4fd10ac1f6a066a0646c")),array('$set' => array(new MongoId("54dc4fda0ac1f6a066a0646d").".lists.category" => '0')));
like this but it doesn't work.
set third parameter of update to true.
i think there is some problem in finding which embedded document for updated.
try this one might be work.
$collection->update(array('_id' => new MongoId("54dc4fd10ac1f6a066a0646c"), 'lists._id' => "54dc4fda0ac1f6a066a0646d"), array('$set' => array("lists.$.category" => '0')), true);
Hi I don't know more about PHP but you can do this from following script
db.collectionName.find({
"lists": {
"$exists": true
}
}).forEach(function(data){
for(vari=0;i<data.lists.length;i++){
db.collectionName.update({
"_id": data._id,
"lists.index": data.lists[
i
].index
},
{
"$set": {
"lists.$.category": 0
}
},
true,
true);
}
})

Categories