PHP client
Preview
The Lucenia PHP client provides a safer and easier way to interact with your Lucenia cluster. Rather than using Lucenia from a browser and potentially exposing your data to the public, you can build an Lucenia client that takes care of sending requests to your cluster. The client contains a library of APIs that let you perform different operations on your cluster and return a standard response body.
This getting started guide illustrates how to connect to Lucenia, index documents, and run queries.
Setup
To add the client to your project, install it using composer:
composer require lucenia/lucenia-php
To install a specific major version of the client, run the following command:
composer require lucenia/lucenia-php:<version>
Then require the autload file from composer in your code:
require __DIR__ . '/vendor/autoload.php';
Connecting to Lucenia
To connect to the default Lucenia host, create a client object with the address https://localhost:9200
if you are using the Security plugin:
$client = (new \Lucenia\ClientBuilder())
->setHosts(['https://localhost:9200'])
->setBasicAuthentication('admin', 'admin') // For testing only. Don't store credentials in code.
->setSSLVerification(false) // For testing only. Use certificate for validation
->build();
Connecting to Lucenia Service Hosted on AWS
The following example illustrates connecting to Lucenia hosted on AWS:
$client = (new \Lucenia\ClientBuilder())
->setSigV4Region('us-east-2')
->setSigV4Service('es')
// Default credential provider.
->setSigV4CredentialProvider(true)
// Using a custom access key and secret
->setSigV4CredentialProvider([
'key' => 'awskeyid',
'secret' => 'awssecretkey',
])
->build();
Creating an index
To create an Lucenia index with custom settings, use the following code:
$indexName = 'test-index-name';
// Create an index with non-default settings.
$client->indices()->create([
'index' => $indexName,
'body' => [
'settings' => [
'index' => [
'number_of_shards' => 4
]
]
]
]);
Indexing a document
You can index a document into Lucenia using the following code:
$client->create([
'index' => $indexName,
'id' => 1,
'body' => [
'title' => 'Moneyball',
'director' => 'Bennett Miller',
'year' => 2011
]
]);
Searching for documents
The following code uses a multi_match
query to search for “miller” in the title and director fields. It boosts the documents where “miller” appears in the title field:
var_dump(
$client->search([
'index' => $indexName,
'body' => [
'size' => 5,
'query' => [
'multi_match' => [
'query' => 'miller',
'fields' => ['title^2', 'director']
]
]
]
])
);
Deleting a document
You can delete a document using the following code:
$client->delete([
'index' => $indexName,
'id' => 1,
]);
Deleting an index
You can delete an index using the following code:
$client->indices()->delete([
'index' => $indexName
]);
Sample program
The following sample program creates a client, adds an index with non-default settings, inserts a document, searches for the document, deletes the document, and then deletes the index:
<?php
require __DIR__ . '/vendor/autoload.php';
$client = (new \Lucenia\ClientBuilder())
->setHosts(['https://localhost:9200'])
->setBasicAuthentication('admin', 'admin') // For testing only. Don't store credentials in code.
->setSSLVerification(false) // For testing only. Use certificate for validation
->build();
$indexName = 'test-index-name';
// Print Lucenia version information on console.
var_dump($client->info());
// Create an index with non-default settings.
$client->indices()->create([
'index' => $indexName,
'body' => [
'settings' => [
'index' => [
'number_of_shards' => 4
]
]
]
]);
$client->create([
'index' => $indexName,
'id' => 1,
'body' => [
'title' => 'Moneyball',
'director' => 'Bennett Miller',
'year' => 2011
]
]);
// Search for it
var_dump(
$client->search([
'index' => $indexName,
'body' => [
'size' => 5,
'query' => [
'multi_match' => [
'query' => 'miller',
'fields' => ['title^2', 'director']
]
]
]
])
);
// Delete a single document
$client->delete([
'index' => $indexName,
'id' => 1,
]);
// Delete index
$client->indices()->delete([
'index' => $indexName
]);
?>