Skip to content
Advertisement

is there any way for algolia api to get record through objectID and update that record

hey i am just asking about Algolia API (search engine) once i stored record into indices then how an i update that

// composer autoload
require __DIR__ . '/vendor/autoload.php';
// if you are not using composer
// require_once 'path/to/algolia/folder/autoload.php';
$client = AlgoliaAlgoliaSearchSearchClient::create(
    'myapplicationid',
    'myadminid'
);
$index = $client->initIndex('testing');
$servername = "localhost";
$username = "root";
$password = "";
try {
    $conn = new PDO("mysql:host=$servername;dbname=test", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
$users = $conn->query("SELECT * FROM users");
$objects = [];
foreach ($users as $user) {
    $objects[] = [
        'objectID' => $user['user_id'],
        'user_id' => $user['user_id'],
        'first_name' => $user['user_first_name'],
        'last_name' => $user['user_last_name'],
        'contact_number' => $user['user_contact'],
    ];
}
$index->saveObjects($objects);

when i am looping record how can i add condition to get record through objectID from algolia if the record exist then update otherwise add record into Algolia

Advertisement

Answer

use partialUpdateObjects and set ‘createIfNotExists’ => true doc is here, https://www.algolia.com/doc/api-reference/api-methods/partial-update-objects/

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement