1. Go to this page and download the library: Download searchcraft/searchcraft-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/ */
searchcraft / searchcraft-php example snippets
use Searchcraft\Searchcraft;
// Using an admin key (full access)
$searchcraft_full = new Searchcraft('your-admin-key');
// Using a read-only key (search and read operations only)
$searchcraft_reader = new Searchcraft('your-read-key', Searchcraft::KEY_TYPE_READ);
// Using an ingest key (document operations only)
$searchcraft_ingestion = new Searchcraft('your-ingest-key', Searchcraft::KEY_TYPE_INGEST);
// Replace with your cluster endpoint
$searchcraft = new Searchcraft(
'your-api-key',
Searchcraft::KEY_TYPE_ADMIN,
'https://yourcluster.io'
);
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
$httpClient = new Client();
$requestFactory = new HttpFactory();
$streamFactory = new HttpFactory();
$searchcraft = new Searchcraft(
'your-api-key',
Searchcraft::KEY_TYPE_ADMIN,
'https://api.searchcraft.io/v1',
$httpClient,
$requestFactory,
$streamFactory
);
$results = $searchcraft->search()->query('my_index', 'search term', [
// Datetime field used to decay relevance for older documents,
// overriding the index's own setting for this query.
'time_decay_field' => 'publishedAt',
// Relevance weight for this index within a federation search.
'index_weighting' => 1.5,
// Ordering by a field means hits come back with a null score.
'order_by' => 'publishedAt',
'sort' => 'desc',
]);
$results = $searchcraft->search()->query('my_index', [
['occur' => 'must', 'fuzzy' => ['ctx' => 'breaking news']],
// A single unparsed term against one field — useful for programmatic
// filters.
['occur' => 'must', 'term' => ['ctx' => 'published', 'fields' => 'status']],
['occur' => 'mustnot', 'exact' => ['ctx' => 'category:opinion']],
]);
// Require at least two of the optional (`should`) clauses to match. The
// minimum cannot exceed the number of `should` clauses in the query.
// `minimum_number_should_match` was added in Searchcraft Engine 0.11.0.
$results = $searchcraft->search()->query('my_index', [
['occur' => 'should', 'fuzzy' => ['ctx' => 'election']],
['occur' => 'should', 'fuzzy' => ['ctx' => 'senate']],
['occur' => 'should', 'fuzzy' => ['ctx' => 'polls']],
], ['minimum_number_should_match' => 2]);
// Find documents similar to a reference document. `ctx` is the internal
// Searchcraft `document_id`, as returned on each search hit.
$results = $searchcraft->search()->query('my_index', [
'more-like-this' => ['ctx' => '1a2b3c4d']
]);
// Search across all indexes in a federation using federatedQuery
$searchResults = $searchcraft->search()->federatedQuery('my_federation', 'breaking news', [
'limit' => 20,
'offset' => 0,
'mode' => 'fuzzy'
]);
// Federation search with additional options
$searchResults = $searchcraft->search()->federatedQuery('my_federation', 'search term', [
'limit' => 50,
'offset' => 10,
'order_by' => 'publishedAt',
'sort' => 'desc',
'occur' => 'should',
'mode' => 'exact'
]);
// Document count for a single index
$stats = $searchcraft->index()->getIndexStats('blog');
// Aggregate statistics for every index the key can reach. The response
// carries index_count, total_document_count, and an indices list mapping
// each index name to its own document_count.
$allStats = $searchcraft->index()->getStats();
// The index's custom stopword list
$stopwords = $searchcraft->stopwords()->getStopwords('blog');
// The engine's built-in defaults for the index's language, as distinct
// from the custom list above
$defaults = $searchcraft->stopwords()->getDefaultStopwords('blog');
$searchcraft->stopwords()->addStopwords('blog', ['whilst', 'thereof']);
$searchcraft->stopwords()->deleteStopwords('blog', ['thereof']);
$searchcraft->stopwords()->deleteAllStopwords('blog');
// List all federations
$federations = $searchcraft->federation()->listFederations();
// Get details of a specific federation
$federation = $searchcraft->federation()->getFederation('galaxy_news_federation');
// Get all federations for a specific organization
$organizationFederations = $searchcraft->federation()->getFederationsByOrganization('4');
// The names of the indexes belonging to a federation
$indexNames = $searchcraft->federation()->listFederationIndexes('galaxy_news_federation');
// The federation's num_docs total, plus per-index num_docs and space_usage
$stats = $searchcraft->federation()->getFederationStats('galaxy_news_federation');
// Delete a federation
$result = $searchcraft->federation()->deleteFederation('old_federation');
// Returns whether AI features are enabled and which pieces
// (LLM provider, model, and search summary config) are configured.
$capabilities = $searchcraft->index()->getCapabilities('blog');
// Collect all events at once
$events = $searchcraft->search()->searchSummary('blog', 'search term', [
'limit' => 5,
'mode' => 'fuzzy',
]);
// Or stream them via a callback
$summaryText = '';
$searchcraft->search()->searchSummary(
'blog',
'search term',
['limit' => 5],
function (string $event, $data) use (&$summaryText) {
if ($event === 'delta' && isset($data['content'])) {
$summaryText .= $data['content'];
}
}
);
// Returns all authentication keys scoped to the given index.
$keys = $searchcraft->authentication()->getIndexKeys('blog');
use Searchcraft\Permissions;
$canReadAnalytics = $searchcraft->authentication()
->checkKeyPermissions(Permissions::READ_ANALYTICS);
$canManageContent = $searchcraft->authentication()
->checkKeyPermissions(Permissions::MODIFY_DOCUMENTS | Permissions::MODIFY_INDEX);
// Presets are available for the conventional key shapes.
$isAdmin = $searchcraft->authentication()
->checkKeyPermissions(Permissions::PRESET_ADMIN);
// Add-ons combine with a level: a read key that can request AI summaries.
$canSummarize = $searchcraft->authentication()
->checkKeyPermissions(Permissions::PRESET_READ | Permissions::PRESET_AI);
// Unauthenticated — works with or without an API key. Returns
// ['enabled' => bool]; when false, the other measure() endpoints are
// no-ops on the server.
$status = $searchcraft->measure()->getStatus();
if ($status['enabled']) {
// ... track events / query dashboards
}