PHP code example of b3n / php-azure-search

1. Go to this page and download the library: Download b3n/php-azure-search library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

b3n / php-azure-search example snippets


$azuresearch = new BenjaminHirsch\Azure\Search\Service(azure_url, azure_admin_key, azure_version);

$index = new BenjaminHirsch\Azure\Search\Index('name of your index');
$index->addField(new BenjaminHirsch\Azure\Search\Index\Field('field name 1', BenjaminHirsch\Azure\Search\Index\Field::TYPE_STRING, true))
       ->addField(new BenjaminHirsch\Azure\Search\Index\Field('field name 2', BenjaminHirsch\Azure\Search\Index\Field::TYPE_STRING))
       ->addSuggesters(new BenjaminHirsch\Azure\Search\Index\Suggest('livesearch', ['field name(s)']));

$azuresearch->createIndex($index);

$azuresearch->deleteIndex('name of the index to delete');

$data['value'][] = [
    '@search.action' => BenjaminHirsch\Azure\Search\Index::ACTION_UPLOAD,
    'field name 1' => <your value for field name 1>,
    'field name 2' => <your value for field name 2>
];

$azuresearch->uploadToIndex('name of your index', $data);

$azuresearch->suggestions('name of your index', 'your term', 'livesearch')

$azuresearch->search('name of your index', 'your term');