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


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



Source Code Reference