Examples / How To
Data Indexing

Add, Update, or Delete Indexing Attributes

8min

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.

Add or Update Attributes

The following instructions are applicable to both Add and Update operations. When data is sent to Klevu's APIs, an attribute will be updated if it already exists and created otherwise.

Attributes are identified via the attributeName property. Important: this field is case-insensitive, so if you already have an attribute named, for example, foo and send a request with the name FOO, the original attribute will be updated, however the name will remain lowercase.

To add or update custom attributes used by Klevu's JSON Indexing service, first locate your Klevu API and REST AUTH keys and create an AccountCredentials object (see Quickstart for steps).

Next, create an AttributeInterface object populated with the data to persist. Note, while you can create this using new Attribute, it is recommended to use the AttributeFactory class for ease of development and future compatibility.

Finally, instantiate an AttributeService object and call the put method with the Attribute object, catching and handling any exceptions thrown during execution.

If the attribute you are attempting to send is tagged as immutable ( AttributeInterface::isImmutable()), the SDK will prevent PUT operations during validation. In this case, disabling the immutable flag and sending will cause the API to reject the request and the SDK to throw a BadRequestException

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


Delete Attributes

Delete By Name

To delete custom attributes used by Klevu's JSON Indexing service, first locate your Klevu API and REST AUTH keys and create an AccountCredentials object (see Quickstart for steps).

Next, instantiate an AttributeService object and call the deleteByName method with the attributeName of the record you wish to remove, catching and handling any exceptions thrown during execution.

If the attribute is marked as immutable in Klevu's indexes, sending a DELETE request will cause the API to reject the request and the SDK to throw a BadRequestException

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


Delete Object

Alternatively, if you have an AttributeInterface already created, you can pass this to AttributeService::delete which acts as a wrapper around deleteByName.

Attributes passed to delete only require an attributeName property, which is internally extracted and passed to deleteByName . As such, attributes marked as immutable ( AttributeInterface::isImmutable will not be rejected by the SDK's validation.

PHP



Source Code Reference