Seach Index Field Definition (attributes)

Permalink 1 user found helpful
Within attributes, you can define fields for your search index. This can be done using the variable "$searchIndexFieldDefinition" within your attribute (class).

This can be an array with 1 field, like so:

protected $searchIndexFieldDefinition = [
      'type'    => 'integer',
      'options' => [
         'default' => 0,
         'notnull' => false,
      ]
   ];


Or can be an array with multiple fields, like so:

protected $searchIndexFieldDefinition = [
      'field_one' => [
         'type'    => 'integer',
         'options' => [
            'default' => 0,
            'notnull' => false,
         ]
      ],
      'field_two'  => [
         'type'    => 'integer',
         'options' => [
            'default' => 0,
            'notnull' => false,
         ]
      ],


The last one will make columns in CollectionSearchIndexAttributes like "ak_attributehandle_fieldhandle". So if your attribute has an handle of "awesome" and we take the "field_two" from the last code example, it will be "ak_awesome_field_two".

Now I'm changing the definitions from a 5.7 Add-On and want to turn it from 1 (first code example) to multiple (second code example). Now, upon upgrading, no changes will take place. So there are missing columns in the database table. Also, 1 of the fields is actually already there (field_one), but it hasn't got this last bit added, since it was only 1 field. So it's "ak_awesome", without "field_one".

How do we handle an update like this in our Add-On? I've got this issue with an Add-On I have in the marketplace, and want smooth updates for users. Also for my clients of course, which I may be upgrading. What is the best way to solve this issue?

ramonleenders
 
aghouseh replied on at Permalink Reply
aghouseh
I was able to guess at what to do and it actually worked.

You need to get two objects to make this happen: an attribute key object and the attribute key category object. In my use case, I had an Express object ('product') with a custom attribute type ('product_views') with no search index definition. After adding the $searchIndexFieldDefinition property to the controller, I ran the following in a tool file.

// get your object / key however you need here.. this works for Express
$product_object = Express::getObjectByHandle('product');
$category = $product_object->getAttributeKeyCategory();
$key = $category->getByHandle('product_views');
// then running this rebuilds the index for this type
$key->getSearchIndexer()->updateSearchIndexKeyColumns($category, $key);


Total shot in the dark but it worked a charm. Hope that helps!