Examples / How To
Data Indexing
Send Records for Indexing
6min
this guide assumes you have installed the latest version of klevu/php sdk , either via composer or from source, and autoloading is functioning as expected examples in this guide are for vanilla php and, as such, will use new to instantiate objects if you are using a modern framework such as magento or symfony, you should utilise your object manager for dependency injection and preferencing of interfaces where possible indexing record batches to send batches of records to klevu's indexer, first locate your klevu api and rest auth keys and create an accountcredentials object (see quickstart for steps) next, extract the catalog information (eg, products; categories; custom entities) to be sent from your application, including all applicable variants if you are using complex products as each system differs in storage and retrieval of data, the examples below simply contain hardcoded data as records of all types (klevu product, klevu category, etc) share an id pool, you should implement a naming convention to differentiate records whose ids would otherwise clash (for example, sending a product and a category, both with id 1) instantiate a new recorditerator object and populate with recordinterface items note, while you can create these using new record , it is recommended to use the recordfactory class for ease of development and future compatibility finally, create a new indexing\batchservice object and call the send method with the record collection, catching and handling any exception thrown during execution at this point, you will have an apiresponseinterface which can be checked for success status, as well as any errors returned by the klevu api \<?php declare(strict types=1); use klevu\phpsdk\exception\api\badrequestexception; use klevu\phpsdk\exception\api\badresponseexception; use klevu\phpsdk\exception\validationexception; use klevu\phpsdk\model\accountcredentials; use klevu\phpsdk\model\indexing\recordfactory; use klevu\phpsdk\model\indexing\recorditerator; use klevu\phpsdk\service\indexing\batchservice; $accountcredentials = new accountcredentials( jsapikey "klevu 123456780", restauthkey "abcde1234567890", ); $recordfactory = new recordfactory(); $records = new recorditerator( data \[ $recordfactory >create( data \[ 'id' => '1', 'type' => 'klevu product', 'relations' => \[ 'categories' => \[ 'values' => \[ 'category 1', ], ], ], 'attributes' => \[ 'name' => \[ 'default' => 'parent product', ], 'sku' => 'parent product', 'images' => \[ \[ 'url' => 'https //www klevu com/parent product jpg', 'type' => 'default', ], ], 'prices' => \[ \[ 'amount' => 42 0, 'currency' => 'gbp', 'type' => 'defaultprice', ], \[ 'amount' => 10 0, 'currency' => 'gbp', 'type' => 'saleprice', ], \[ 'amount' => 3 14, 'currency' => 'gbp', 'type' => 'startprice', ], \[ 'amount' => 50 0, 'currency' => 'gbp', 'type' => 'endprice', ], ], 'url' => 'https //www klevu com/parent product', 'instock' => true, 'shortdescription' => \[ 'default' => 'comfortable chair', ], 'description' => \[ 'default' => 'this is a very very comfortable chair', ], ], ], ), $recordfactory >create( data \[ 'id' => '2', 'type' => 'klevu product', 'relations' => \[ 'parentproduct' => \[ 'values' => \[ '1', ], ], ], 'attributes' => \[ 'name' => \[ 'default' => 'variant product', ], 'sku' => 'variant product', 'images' => \[ \[ 'url' => 'https //www klevu com/variant product jpg', 'type' => 'default', ], ], 'prices' => \[ \[ 'amount' => 42 0, 'currency' => 'gbp', 'type' => 'defaultprice', ], \[ 'amount' => 3 14, 'currency' => 'gbp', 'type' => 'saleprice', ], ], 'url' => 'https //www klevu com/variant product', 'instock' => true, ], ], ), ], ); $records >additem( item $recordfactory >create( data \[ 'id' => 'category 1', 'type' => 'klevu category', 'attributes' => \[ 'name' => \[ 'default' => 'example category', ], 'url' => 'https //www klevu com/simple product', 'categorypath' => 'foo/bar', ], ], ), ); $batchservice = new batchservice( // inject any relevant dependencies, such as http client; logger; or user agent provider // as well as behaviour config, such as invalidrecordmode ); $response = null; try { $response = $batchservice >send( accountcredentials $accountcredentials, records $records, ); } catch (validationexception $exception) { // one or more passed arguments are invalid } catch (badrequestexception $exception) { // klevu api rejected the request as invalid } catch (badresponseexception $exception) { // klevu api responded in an unexpected manner } $sentsuccessfully = $response? >issuccess() ? false; $messages = $response? >getmessages() ? \[]; handling invalid records prior to send, the php sdk will validate each record in the provided batch if any record fails this initial validation check, it will be removed from the batch to be sent if removing invalid records leads to an empty batch, a validationexception will be thrown and no api call will be attempted note, the api may still reject an entire batch based upon the validity of a single record in this instance, the operation will throw a badrequestexception this behaviour cannot be changed via the sdk to change the default behaviour and cause the internal validation to fail if any record is invalid, you can pass a flag to the batchservice during instantiation \<?php declare(strict types=1); use klevu\phpsdk\service\indexing\invalidrecordmode use klevu\phpsdk\service\indexing\batchservice; // invalid records will be removed from a batch // processing will continue for any valid records remaining // default behaviour $batchservice = new batchservice( invalidrecordmode invalidrecordmode skip, ); // presence of an invalid record will cause the entire batch // to be rejected prior to processing $batchservice = new batchservice( invalidrecordmode invalidrecordmode fail, ); source code reference api\model\ indexing\recordinterface exception\api\ badrequestexception exception\api\ badresponseexception exception\ validationexception model\ accountcredentials model\ indexing\record model\ indexing\recordfactory model\ indexing\recorditerator service\ indexing\batchservice