App Search APIedit

This document includes examples for different API requests. Please refer to the Elastic App Search Documentation for more information, particularly the API Reference.

Enginesedit

use Elastic\EnterpriseSearch\AppSearch\Request;
use Elastic\EnterpriseSearch\AppSearch\Schema;
use Elastic\EnterpriseSearch\Client;

$client = new Client([
    'host' => 'http://localhost:3002',
    'app-search' => [
        'token' => '<insert here the API key>'
    ]
]);
$appSearch = $client->appSearch();

// Create an engine
$result = $appSearch->createEngine(
    new Request\CreateEngine(
        new Schema\Engine('videogames')
    )
);
print_r($result->asArray());

// List all engines
$result = $appSearch->listEngines();
print_r($result->asArray());

// Get an engine
$result = $appSearch->getEngine(
    new Request\GetEngine('videogames')
);
print_r($result->asArray());

// Delete an engine
$result = $appSearch->deleteEngine(
    new Request\DeleteEngine('videogames')
);
var_dump($result['deleted']);

Documentsedit

$engineName = 'videogames';

$id = 'Mr1064';
$doc = new Schema\Document($id);
$doc->title = 'Super Luigi 64';
$doc->description = 'A classic 3D videogame';

// Index documents
$result = $appSearch->indexDocuments(
    new Request\IndexDocuments($engineName, [$doc])
);

// List documents
$result = $appSearch->listDocuments(
    new Request\ListDocuments($engineName)
);
print_r($result->asArray());

// Get document(s) by ID:
$result = $appSearch->getDocuments(
    new Request\GetDocuments($engineName, [$id])
);
print_r($result->asArray());

// Delete a document
$result = $appSearch->deleteDocuments(
    new Request\DeleteDocuments($engineName, [$id])
);
print_r($result->asArray());

Searchedit

$engineName = 'videogames';

// Single Search
$search = new Schema\SearchRequestParams('luigi');

$result = $this->appSearch->search(
    new Request\Search($engineName, $search)
);
print_r($result->asArray());

Elasticsearch Search APIedit

Using this API you can search directly in App Search using the search API of Elasticsearch. You can use the Request\SearchEsSearch to perform a search query using the Elasticsearch API.

For more information about this feature you can read the Elasticsearch search API for App Search documentation.

In order to use the Request\SearchEsSearch endpoint you need to have an Elasticsearch OAuth2 token, you can read how to generate one here.

$engineName = 'videogames';

// Elasticsearch query
$searchParams = new Schema\EsSearchParams();
$searchParams->query = [
    'match' => [
        'title' => 'Super Luigi 64'
    ]
];

// This is the Elasticsearch token API (Bearer)
$elasticsearchApiKey = 'xxx';

$result = $this->appSearch->searchEsSearch(
    (new Request\SearchEsSearch($engineName, $searchParams))
        ->setAuthorization($elasticsearchApiKey)
);

print_r($result->asArray()); // Elasticsearch result in ['hits']['hits']