---
title: Synchronising Data
slug: php-sdk/synchronizing-data
docTags: 
createdAt: 2024-05-13T16:03:58.257Z
---

## Creating or Updating Records

Batches of records can be sent to the indexing endpoint (`indexing.ksearchnet.com/v2/batch`) in order to add or update search data using the Indexing\BatchService.

Data is sent as a collection of Record objects (within a RecordIterator), which are transmitted as a single request. Note that if too many records are included in the same batch, the request will be rejected (either by the PHP-SDK validation or the API itself).
The implementing application is responsible for creating and processing batches.

Data contained within a Record includes:

| Field        | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id`         | `string` Required<br />Unique ID for the record in your indexes<br />Examples: `"1"`, `"categoryid__12345"`                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| `type`       | `string` Required<br />Type of record, such as product or category. Custom values are allowed.<br />Examples: `"KLEVU_PRODUCT"`, `"KLEVU_CATEGORY"`, `"KLEVU_CMS"`, `"ACME_RECORD"`<br />*Note: the type of record will determine the degree of data validation performed by the API with, for example, KLEVU\_PRODUCT records requiring attribute fields not present in other record types*                                                                                                                                                                                                                                                                                                                                       |
| `relations`  | `array`<br />Array of relation defintions, such as categories and parent products.<br />Example:<br />`[`<br />`    'categories' => [`<br />`        'values' => [`<br />`            'CATEGORY001',`<br />`            'CATEGORY002',`<br />`        ],`<br />`    ],`<br />`    'parentProduct' => [`<br />`        'values' => [`<br />`            'PARENT_PRODUCT_001',`<br />`        ],`<br />`    ],`<br />`]`<br /><br />*Note: no validation is performed on content of this field; refer to the API definition for details of structure and content*                                                                                                                                                                    |
| `attributes` | `array`<br />Array of record data, comprising core and custom attributes (if present)<br />Example:<br />`[`<br />`    'name' => [`<br />`        'default' => 'Men Regular Mid Rise Dark Blue Jeans',`<br />`        'fi-FI' => 'Miesten tavalliset Mid Rise tummansiniset farkut',`<br />`    ],`<br />`    'sku' => 'SKU0001',`<br />`    'image' => [`<br />`        'default' => [`<br />`            'url' => 'https://www.klevu.com/image.jpg',`<br />`            'type' => 'default',`<br />`            'height' => 100,`<br />`            'width' => 100,`<br />`        ],`<br />`     ],  `<br />`]`<br />*Note: validation is performed for attribute name validity, but missing or invalid fields are not checked* |
| `display`    | `array`<br />Array of data which should be returned in search queries for use in storefront templates<br />Example:<br />`[`<br />`    'default' => [`<br />`        'additionalProp1' => [],`<br />`    ],`<br />`    'additionalProp1' => [],`<br />`]`<br />*Note: validation is performed for attribute name validity*                                                                                                                                                                                                                                                                                                                                                                                                         |

:::hint{type="warning"}
IDs must be unique within your entire index, not just within a specific record type. For example, you could not send records with ID "1" for both "KLEVU\_PRODUCT" and "KLEVU\_CATEGORY".

If you are synchronising multiple record types which share IDs in your application, we recomend using a prefix for most or all records to ensure their uniqueness. For example `product__1` / `category__1`
:::

:::hint{type="info"}
Note, data validity is not checked within the model itself, but is performed by the RecordValidator. This validator is not executed until you interact with Klevu APIs via the Indexing\BatchService, meaning invalid ids or attribute names, for example, will not be picked up at time of definition.
:::

### Validation and Exception Handling

- Prior to transmitting data, the provided account credentials are validated as not empty and correctly formatted, throwing a `ValidationException` on failure.
- Prior to transmitting data, the Record data is validated, throwing a `ValidationException` on failure.
- Request exceptions, or responses identifying an invalid request throw a`BadRequestException`
- Client or Network exceptions, responses with unexpected status codes, or responses with a missing or invalid body content throw a `BadResponseException`

### Logging

- Request headers and body are logged prior to send at `DEBUG` level
- Data used to generate the request bearer token is logged at `DEBUG` level
- Response headers, body, and execution time are logged on successful request send at `DEBUG` levelDeleting Records&#x20;

*More information and code samples can be found under&#x20;*[Examples / How To > Data Indexing > Send Records for Indexing](docId\:wacVz7cvZ6qQV8kkNSyRB)

## Deleting Records

Batches of record IDs can be sent to the indexing endpoint (`indexing.ksearchnet.com/v2/batch/delete`) in order to remove search data using the Indexing\Batch\DeleteService.

Data is provided as an array of strings corresponding to the ID value used when creating the record (above); type is not required as ID is unique across all records.

### Validation and Exception Handling

- Prior to transmitting data, the provided account credentials are validated as not empty and correctly formatted, throwing a `ValidationException` on failure.
- Prior to transmitting data, the Record IDs are validated, throwing a `ValidationException` on failure.
- Request exceptions, or responses identifying an invalid request throw a`BadRequestException`
- Client or Network exceptions, responses with unexpected status codes, or responses with a missing or invalid body content throw a `BadResponseException`

### Logging

- Request headers and body are logged prior to send at `DEBUG` level
- Data used to generate the request bearer token is logged at `DEBUG` level
- Response headers, body, and execution time are logged on successful request send at `DEBUG` levelDeleting Records&#x20;

*More information and code samples can be found under&#x20;*[Examples / How To > Data Indexing > Delete Records from Inde](docId\:aQcZmHzhC20d6Ko4n9nxJ)
