Workplace Search APIedit

Here is reported a brief overview of the Workplace Search API. For more information please visit the Workplace Search API documentation.

Content Sourcesedit

To store some document in Workplace Search you need to create a content source. A content source is usually a third-party service like GitHub, Google Drive, or Dropbox. You can also build your own connectors using Custom API sources, which allows you to create unique content repositories on the platform and send any data to Workplace Search via uniquely identifiable endpoints.

In the following example we create a custom test content source.

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

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

// Create a custom content source
$content = new ContentSourceCreateDefinition('test');
$result = $workplace->createContentSource(
    new Request\CreateContentSource($content)
);
$sourceId = $result['id'] ?? null;

We need to store the sourceId since it’s required to perform other operations, like index a document.

Index documentsedit

To store a document in Workplace Search you need to perform an index operation. You can add one or more document at time.

In the following example we store only one document. You can specify multiple documents using the array.

// Index a document in content source $sourceId
$doc = new Schema\Document();
$doc->id = '1234';
$doc->title = "The Meaning of Time";
$doc->body = "Not much. It is a made up thing.";

$result = $workplace->indexDocuments(
    new Request\IndexDocuments($sourceId, [$doc])
);

Search documentsedit

To search for a document in Workplace Search you need to have an OAuth2 token. The search API requires a different authentication, using OAuth2, since this feature is typically executed form an external application using a custom search experience. To have a better understanding of the search experience in Workplace Search we suggest to read the Building Custom Search Experiences documentation page.

Once you have configured your OAuth Application for Search, you can request an OAuth2 token using the Confidential OAuth Flow or the Implicit OAuth Flow

$workplace = $client->workplaceSearch([
    'token' => 'xxx' // OAuth2 token
]);

$search = new Schema\SearchApiQuery();
$search->query = '<insert here the term to search>';

$result = $workplace->search(
    new Request\Search($search)
);

print_r($result->asArray());

Delete documentsedit

You can delete one or more documents in Workplace Search using the delete API. You need to specify the sourceId and one or more ID of the documents to be deleted.

// Delete one document
$docId = '1234';

$result = $workplace->deleteDocuments(
    new Request\DeleteDocuments($sourceId, [$docId])
);