Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for Partial Updating Objects #158

Open
furioursus opened this issue May 13, 2020 · 5 comments
Open

Allow for Partial Updating Objects #158

furioursus opened this issue May 13, 2020 · 5 comments

Comments

@furioursus
Copy link

Currently AFAICT Scout only allows full updating of objects in Algolia. I was wondering if we could instead update individual properties in an object as mentioned in their docs.

Use case: Page view ranking is an important part of our indexes, but given the number of relationships and additional fields we have on each standard record, pushing a nightly or even weekly update of those page views would be costly in terms of operations. It would be better if we could just update the pageview attribute of every one of those records.

@lindseydiloreto
Copy link
Contributor

This would be helpful for people who want to frequently update their Algolia records based on data from several of my plugins...

I could probably provide several other real-world use cases in addition to the one @onebrightlight mentioned. 🙂

@timkelty
Copy link
Collaborator

Yup. This would be nice.

FWIW, I do this kind of thing on a nightly cron using partialUpdateObjects. In this example I refresh a "random" property on every object, because the client wants a random sort that gets refreshed daily 🤷‍♀️.

<?php

namespace modules\appmodule\console\controllers;

use Algolia\AlgoliaSearch\SearchClient;
use Craft;
use rias\scout\Scout;
use Tightenco\Collect\Support\Collection;
use yii\console\ExitCode;
use yii\helpers\Console;

class AlgoliaController extends \craft\console\Controller
{
    public function actionRandomizeIndex(string $indexName): int
    {
        $scoutSettings = Scout::$plugin->getSettings();
        $scoutIndex = $scoutSettings->getIndices()
            ->firstWhere('indexName', $indexName);

        $index = Craft::$container->get(SearchClient::class)->initIndex($indexName);
        $ids = $scoutIndex->criteria->ids();
        $updates = Collection::make($ids)
            ->map(function ($id) use ($ids) {
                return [
                  'objectID' => $id,
                  'random' => rand(0, count($ids)),
                ];
            });

        $batches = Collection::make($index->partialUpdateObjects($updates->all()));
        $total = Collection::make($batches)->reduce(function ($carry, $item) {
            return $carry + count($item['objectIDs']);
        }, 0);

        $this->stdout("{$total} element(s) updated in {$batches->count()} batch(es).\n");

        return ExitCode::OK;
    }
}

Same kind of thing should work for ratings, etc. I would think, but it would be nice baked in.

@onebrightlight With your example, it seems like you'd still need to do this on a cron, right? Batching is what really saves you the operations, and partial updates just let you fit more in a batch.

@furioursus
Copy link
Author

@timkelty yes, it’d still need to be a cronjob for me. Are you saying that it can’t be a partial or batch job in that case?

@timkelty
Copy link
Collaborator

Nope, just clarifying.

That's exactly what I'm doing in my example above (I run craft appmodule/randomize-index products on a cron).

@howells
Copy link

howells commented Apr 9, 2024

Just wondered if there was any movement on this one - I update popularity metrics of my algolia entries via another (node) script, which means that when an entry is saved, those attributes are simply overwritten and I'd need to wait for the script to run again before they're updated. I'd love Scout to just update the attributes defined in scout, leaving anything else in-tact. Would that be possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants