PHP code example of elastic / app-search-php

1. Go to this page and download the library: Download elastic/app-search-php 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 / app-search-php example snippets


  $apiEndpoint   = 'http://localhost:3002/';
  $apiKey        = 'private-XXXXXXXXXXXX';
  $clientBuilder = \Elastic\AppSearch\Client\ClientBuilder::create($apiEndpoint, $apiKey);

  $client = $clientBuilder->build();

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

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

    $documents = [
      ['id' => 'first-document', 'name' => 'Document name', 'description' => 'Document description'],
      ['id' => 'other-document', 'name' => 'Other document name', 'description' => 'Other description'],
    ];

    $indexingResults = $client->indexDocuments('my-engine', $documents);

    $searchParams = [
      'page'  => ['current' => 1, 'size' => 10]
    ];

    $searchResponse = $client->search('my-engine', 'search text', $searchParams);

[
    'meta' => [
      'warnings' => [],
      'page' => [
        'current' => 1,
        'total_pages' => 1,
        'total_results' => 1,
        'size' => 10
      ],
      'request_id' => 'feff7cf2359a6f6da84586969ef0ca89'
    ],
    'results' => [
      [
        'id' => ['raw' => 'first-document'],
        'name' => ['raw' => 'Document name'],
        'description' => ['raw' => ['Document description']
      ]
    ]
  ]
]