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 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\attribute; use klevu\phpsdk\model\indexing\attributefactory; use klevu\phpsdk\model\indexing\datatype; use klevu\phpsdk\service\indexing\attributesservice; $accountcredentials = new accountcredentials( jsapikey "klevu 123456780", restauthkey "abcde1234567890", ); $attributefactory = new attributefactory(); $attribute = $attributefactory >create( data \[ attribute field attribute name => 'example attribute', attribute field datatype => datatype multivalue, attribute field label => \[ 'default' => 'example attribute', ], attribute field searchable => true, attribute field filterable => false, attribute field returnable => true, attribute field immutable => false, ], ); $attributesservice = new attributesservice( // inject any relevant dependencies, such as http client; logger; or user agent provider ); $response = null; try { $response = $attributesservice >put( accountcredentials $accountcredentials, attribute $attribute, ); } catch (validationexception $exception) { // one or more supplied arguments are missing or invalid } catch (badrequestexception $exception) { // klevu api rejected the request as invalid } catch (badresponseexception $exception) { // klevu api responded in an unexpected manner } $persistedsuccessfully = $response? >issuccess(); $messages = $response? >getmessages(); 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 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\service\indexing\attributesservice; $accountcredentials = new accountcredentials( jsapikey "klevu 123456780", restauthkey "abcde1234567890", ); $attributesservice = new attributesservice( // inject any relevant dependencies, such as http client; logger; or user agent provider ); try { $response = $attributesservice >deletebyname( accountcredentials $accountcredentials, attributename 'example attribute', ); } catch (validationexception $exception) { // one or more supplied arguments are missing or invalid } catch (badrequestexception $exception) { // klevu api rejected the request as invalid } catch (badresponseexception $exception) { // klevu api responded in an unexpected manner } $deletedsuccessfully = $response? >issuccess(); $messages = $response? >getmessages(); 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 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\attribute; use klevu\phpsdk\model\indexing\attributefactory; use klevu\phpsdk\service\indexing\attributesservice; $accountcredentials = new accountcredentials( jsapikey "klevu 123456780", restauthkey "abcde1234567890", ); $attributefactory = new attributefactory(); $attributesservice = new attributesservice( // inject any relevant dependencies, such as http client; logger; or user agent provider ); try { $response = $attributesservice >delete( accountcredentials $accountcredentials, attribute $attributefactory >create( data \[ attribute field attribute name => 'example attribute', ], ), ); } catch (validationexception $exception) { // one or more supplied arguments are missing or invalid } catch (badrequestexception $exception) { // klevu api rejected the request as invalid } catch (badresponseexception $exception) { // klevu api responded in an unexpected manner } $deletedsuccessfully = $response? >issuccess(); $messages = $response? >getmessages(); source code reference api\model\indexing\ attributeinterface exception\api\ badrequestexception exception\api\ badresponseexception exception\ validationexception model\ accountcredentials model\indexing\ attribute model\indexing\ attributefactory model\indexing\ datatype service\indexing\ attributesservice