PHP code example of ellinaut / elasticsearch-7-connector
1. Go to this page and download the library: Download ellinaut/elasticsearch-7-connector 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/ */
ellinaut / elasticsearch-7-connector example snippets
use Ellinaut\ElasticsearchConnector\Connection\DsnConnectionFactory;
use Ellinaut\ElasticsearchConnector\NameProvider\RawNameProvider;
use Ellinaut\ElasticsearchConnector\ElasticsearchConnector;
$elasticsearch = new ElasticsearchConnector(
new DsnConnectionFactory('http://127.0.0.1:9200'),
new RawNameProvider(),
new RawNameProvider()
);
namespace App\IndexManager;
use Ellinaut\ElasticsearchConnector\Index\IndexManagerInterface;
use Ellinaut\ElasticsearchConnector\Index\IndexManagerTrait;
class CustomIndexManager implements IndexManagerInterface {
use IndexManagerTrait;
/**
* @return array
*/
protected function getIndexDefinition() : array{
return [
'mappings' => [
'properties' => [
'test' => [
'type' => 'keyword',
],
],
],
];
}
}
/** @var \Ellinaut\ElasticsearchConnector\ElasticsearchConnector $elasticsearch */
$elasticsearch->addIndexManager('custom_index', new App\IndexManager\CustomIndexManager());
/** @var \Ellinaut\ElasticsearchConnector\ElasticsearchConnector $elasticsearch */
// Creates the index. Will throw an exception if the index already exists.
$elasticsearch->createIndex('custom_index');
// Creates the index only if it does not exist.
$elasticsearch->createIndexIfNotExist('custom_index');
// Deletes the index if it exists, then create the index new.
$elasticsearch->recreateIndex('custom_index');
// Migrate all documents from the index to a (new) migration index,
// then recreate the old index and moves all the documents back.
$elasticsearch->updateIndex('custom_index');
// Deletes the index if it exists.
$elasticsearch->deleteIndex('custom_index');
namespace App\Document;
use Ellinaut\ElasticsearchConnector\Document\DocumentMigratorInterface;
class CustomDocumentMigrator implements DocumentMigratorInterface {
/**
* @param array $previousSource
* @return array
*/
public function migrate(array $previousSource) : array{
if(!array_key_exists('test',$previousSource)){
$previousSource['test'] = 'Test';
}
return $previousSource;
}
}
/** @var \Ellinaut\ElasticsearchConnector\ElasticsearchConnector $elasticsearch */
$elasticsearch->addDocumentMigrator('custom_index', new App\Document\CustomDocumentMigrator());
namespace App\PipelineManager;
use Ellinaut\ElasticsearchConnector\Index\PipelineManagerInterface;
use Ellinaut\ElasticsearchConnector\Index\PipelineManagerTrait;
class CustomPipelineManager implements PipelineManagerInterface {
use PipelineManagerTrait;
/**
* @return array
*/
protected function getPipelineDefinition() : array{
return [
'description' => 'Your custom pipeline which converts content from field "test" to lowercase.',
'processors' => [
[
'lowercase' => [
'field' => 'test',
],
],
],
];
}
}
/** @var \Ellinaut\ElasticsearchConnector\ElasticsearchConnector $elasticsearch */
$elasticsearch->addPipelineManager('custom_pipeline', new App\PipelineManager\CustomPipelineManager());
/** @var \Ellinaut\ElasticsearchConnector\ElasticsearchConnector $elasticsearch */
// Creates all registered pipelines.
$elasticsearch->createPipelines();
// Creates only the given pipeline.
$elasticsearch->createPipelines(['custom_pipeline']);
// Deletes all registered pipelines.
$elasticsearch->deletePipelines();
// Deletes only the given pipeline.
$elasticsearch->deletePipelines(['custom_pipeline']);
/** @var \Ellinaut\ElasticsearchConnector\ElasticsearchConnector $elasticsearch */
// Index the document with ID "document_1" into the index "custom_index" and use pipeline "custom_pipeline".
// Document is index via queue system which uses bulk requests internally. Recommended method to index documents.
$elasticsearch->indexDocument('custom_index','document_1', ['test'=>'Test'],'custom_pipeline');
// Index the document with ID "document_1" into the index "custom_index" and use pipeline "custom_pipeline".
// Document is indexed immediately to elasticsearch.
$elasticsearch->indexDocumentImmediately('custom_index','document_1', ['test'=>'Test'],'custom_pipeline');
// Retrieves the full document by ID from elasticsearch.
$elasticsearch->retrieveDocument('custom_index','document_1');
// Retrieves only the content of key "_source" from the elasticsearch document.
$elasticsearch->retrieveDocumentSource('custom_index','document_1');
// Deletes the document with ID "document_1" from index "custom_index".
// Document is deleted via queue system which uses bulk requests internally. Recommended method to delete documents.
$elasticsearch->deleteDocument('custom_index','document_1');
// Deletes the document with ID "document_1" from index "custom_index".
// Document is deleted immediately from elasticsearch.
$elasticsearch->deleteDocumentImmediately('custom_index','document_1');
/** @var \Ellinaut\ElasticsearchConnector\ElasticsearchConnector $elasticsearch */
// Executes all queued index or delete commands with a single bulk request.
$elasticsearch->executeQueueImmediately();
/** @var \Ellinaut\ElasticsearchConnector\ElasticsearchConnector $elasticsearch */
// Executes a search request over all indices.
$searchAllResult = $elasticsearch->executeSearch(
[
'body' => [
'query' => [
'match_all' => (object)[] // fix for elasticsearch, because an empty array doesn't work in the api
]
]
]
);
// Executes a search request only for the index "custom_index".
$searchCustomResult = $elasticsearch->executeSearch(
[
'body' => [
'query' => [
'match_all' => (object)[] // fix for elasticsearch, because an empty array doesn't work in the api
]
]
],
['custom_index']
);
// Executes a count request over all indices.
$numberOfAllResults = $elasticsearch->executeCount(
[
'body' => [
'query' => [
'match_all' => (object)[] // fix for elasticsearch, because an empty array doesn't work in the api
]
]
]
);
// Executes a count request only for the index "custom_index".
$numberOfCustomResults = $elasticsearch->executeCount(
[
'body' => [
'query' => [
'match_all' => (object)[] // fix for elasticsearch, because an empty array doesn't work in the api
]
]
],
['custom_index']
);
/** @var \Ellinaut\ElasticsearchConnector\ElasticsearchConnector $elasticsearch */
// Returns the configured instance of "Elasticsearch\Client" which is also used by the connector.
$connection = $elasticsearch->getConnection();
// Get the external index name for elasticsearch requests
$externalIndexName = $elasticsearch->getExternalIndexName('custom_index');
// Get the internal index name for internal mappings with the result of an elasticsearch request
$internalIndexName = $elasticsearch->getInternalIndexName('external_custom_index');
// Get the external pipeline name for elasticsearch requests
$externalPipelineName = $elasticsearch->getExternalPipelineName('custom_pipeline');
// Get the internal pipeline name for internal mappings with the result of an elasticsearch request
$internalPipelineName = $elasticsearch->getInternalPipelineName('external_custom_pipeline');
namespace App\PipelineManager;
use Ellinaut\ElasticsearchConnector\Connection\ResponseHandlerInterface;
class CustomResponseHandler implements ResponseHandlerInterface {
/**
* @param string $method
* @param array $response
*/
public function handleResponse(string $method,array $response) : void{
if($method === 'createIndex'){
var_dump($response);
}
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.