partialUpdateObject() Deletes old values when used

I am uploading a few items into Algolia and later I update a few values using this function:

public function partialUpdateObject($partialObject, $createIfNotExists = true)
    {
        $queryString = $createIfNotExists ? '' : '?createIfNotExists=false';
        return $this->client->request(
            $this->context,
            'POST',
            '/1/indexes/'.$this->urlIndexName.'/'.urlencode($partialObject['objectID']).'/partial'.$queryString,
            array(),
            $partialObject,
            $this->context->writeHostsArray,
            $this->context->connectTimeout,
            $this->context->readTimeout
        );
    }

The first few times it works, but later when I use the update record I can se that I deletes other values in my record.
Eg:

When I first uploaded a record I had a value called thumbnail which contained a url.
When I later used the partialUpdateObject() to update a value called title the thumbnail value got removed from my record.

So how do I update certain values without removing other values? Lets say that I only want to update the title value of my record and nothing else

I am using https://github.com/algolia/algoliasearch-client-php together with https://github.com/algolia/algoliasearch-laravel

1 Like

Hi @kevin_thulin,

There is a limitation with partial updates, it won’t be able to partially update nested array values.

Would you mind sharing with use the datastructure you have, and the operations you do on them?

Cheers,

@rayrutjes
I made some changes to: https://github.com/algolia/algoliasearch-laravel and added two more functions:

public function getAlgoliaRecordPrice($indexName)
    {
        /** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
        $modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
        $record = null;
        $record = $this->toArray();
        if (isset($record['objectID']) == false) {
            $record['objectID'] = $modelHelper->getObjectId($this);
        }
        return $record;
    }

public function pushPriceToIndex()
    {
        /** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
        $modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
        $indices = $modelHelper->getIndices($this);
        /** @var \AlgoliaSearch\Index $index */
        foreach ($indices as $index) {
            $index->partialUpdateObject($this->getAlgoliaRecordPrice($index->indexName));
        }
    }

I added these functions so I dont have to reload relationships on my Models each time and also so that I could use the partialUpdateObject() function.

When I first upload a item I init this:

public function getAlgoliaRecord()
  {
      return array_merge($this->toArray(), [
            'company' => $this->company->name,
            'uploaded_at' => $this->item->uploaded_at,
            'thumbnail' => 'SomeImageUrl',
        ]);
  }

//Create a new item then push it using standard Algolia fuctions
$newItem->pushToIndex();

When I later tried to update the item I did it like this:

    $item = Item::where('id', $id)->first();
    $item->price = "someNewPrice";
    $item->save();
    $item->pushPriceToIndex();

This worked the first few times, but later when I used this it started to remove other values in my record.

Now I am using this which seems to work, it only update certain values and doesnt remove any other values:

        $updateAlgolia = new Item();
        $updateAlgolia->id = (int) $this->id;
        $updateAlgolia->price = (int) $this->price;
        $updateAlgolia->code = (int) 3453;
        $updateAlgolia->pushPriceToIndex();

Also, when I upload a new item to Algolia for the first time its json structure looks like this:

{
  "title": "Sometitle",
  "type": 1,
  "state": 8,
  "city": 74,
  "cat_id": 14,
  "price": 12000,
  "is_active": 1,
  "company_id": 1,
  "i_id": 8,
  "company": "CompanyName",
  "city": "SomeCity",
  "uploaded_at": 1491315573,
  "thumbnail": "someUrl",
  "objectID": "8"
}

So when I used the first example this fields would get removed:

"company": "CompanyName",
  "city": "SomeCity",
  "uploaded_at": 1491315573,
  "thumbnail": "someUrl",

Now they dont get removed when I use the second example

@rayrutjes did you check my updated question?