Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

Algolia API request to add attribute for faceting with PHP laravel

I'm looking for some API request from PHP Laravel backend, which will add some new attribute to all my indices (including replicas) to attributes for faceting. Is there any option for this?

Exact case: In e-commerce administration I create new Product Attribute (for example color) and I attach it to Product Model. In toSearchableAttributes method I have function, which will dynamically add these attributes to array and they will sync to algolia. This is working well, but I need to add this newly created attribute to algolia's Facets. So I think, it would be best if I call some request to algolia when I store newly created attribute to my DB. Does Algolia have API for this?

question from:https://stackoverflow.com/questions/65835377/algolia-api-request-to-add-attribute-for-faceting-with-php-laravel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found a solution:

    $client = AlgoliaAlgoliaSearchSearchClient::create('APP_ID', 'APP_SECRET');
    $index = $client->initIndex('MAIN_INDEX_NAME');

    $attributesForFaceting = $index->getSettings()['attributesForFaceting'];
    $attributesForFaceting[] = 'searchable('.$newAttributeName.')';

    $index->setSettings([
        'attributesForFaceting' => $attributesForFaceting
    ]);

    foreach($index->getSettings()['replicas'] as $replicaName) {
        $index = $client->initIndex($replicaName);
        $index->setSettings([
            'attributesForFaceting' => $attributesForFaceting
        ]);
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...