PHP code example of elastic / site-search

1. Go to this page and download the library: Download elastic/site-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/ */

    

elastic / site-search example snippets


  $apiKey        = 'XXXXXXXXXXXX';
  $clientBuilder = \Elastic\SiteSearch\Client\ClientBuilder::create($apiKey);

  $client = $clientBuilder->build();

  $engine = $client->getEngine('my-engine');

  $engine = $client->createEngine('my-engine', 'en');

  $documentTypes = $client->listDocumentTypes('my-engine');

    $documents = [
      [
        'external_id' => 'first-document',
        'fields'      => [
          ['name' => 'title', 'value' => 'First document title', 'type' => 'string'],
          ['name' => 'content', 'value' => 'Text for the first document.', 'type' => 'string'],
        ]
      ],
      [
        'external_id' => 'other-document',
        'fields'      => [
          ['name' => 'title', 'value' => 'Other document title', 'type' => 'string'],
          ['name' => 'content', 'value' => 'Text for the other document.', 'type' => 'string'],
        ]
      ],
    ];

    $indexingResults = $client->createOrUpdateDocuments('my-engine', 'my-document-type', $documents);

    $searchResponse = $client->search('my-engine', 'fulltext search query');

    $searchParams = ['per_page' => 10, 'page' => 2];
    $searchResponse = $client->search('my-engine', 'fulltext search query', $searchParams);