1. Create
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
// Specify the Elasticsearch server details
$hosts = [
'your-elasticsearch-server:9200', // Replace with your Elasticsearch server details
];
// Create an Elasticsearch client
$client = ClientBuilder::create()->setHosts($hosts)->build();
// Define the index name
$indexName = 'your_new_index';
//data
$document= ['field1' => 'value1', 'field2' => 'value2'];
$params = [
'index' => $indexName,
'type' =>'_doc',
'body' => $document,
];
$response = $client->index($params);
2. Search
$searchParams = [
'index' => $indexName,
'body' => [
'query' => [
'bool' => [
'must' => [
['match' => ['field1' => 'value1']], // Add more conditions as needed
['match' => ['field2' => 'value2']],
],
],
],
],
];
// Perform the search
$searchResponse = $client->search($searchParams);
3. Delete
$deleteQuery = [
'bool' => [
'must' => [
['term' => ['field1' => 'value1']],
['term' => ['field2' => 'value2']],
],
],
];
// Define the delete parameters
$deleteParams = [
'index' => $indexName,
'body' => [
'query' => $deleteQuery,
],
];
// Execute the delete query
$response = $client->deleteByQuery($deleteParams);