$client = Elasticsearch\ClientBuilder::create()->build(); $params = [ 'index' => 'my_index', 'body' => [ 'settings' => [ 'number_of_shards' => 3, 'number_of_replicas' => 2 ], 'mappings' => [ 'my_type' => [ '_source' => [ 'enabled' => true ], 'properties' => [ 'title' => [ 'type' => 'text' ], 'description' => [ 'type' => 'text' ], 'date' => [ 'type' => 'date' ] ] ] ] ] ]; $response = $client->indices()->create($params);
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => '1', 'body' => [ 'title' => 'Lorem Ipsum', 'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'date' => '2019-01-01' ] ]; $response = $client->index($params);
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'body' => [ 'query' => [ 'match' => [ 'description' => 'Lorem ipsum' ] ] ] ]; $response = $client->search($params);This code searches for documents in the "my_index" index that have a description containing the phrase "Lorem ipsum". Overall, Elasticsearch is a powerful library for search indexation in PHP. It provides a wide range of features and can handle complex search queries.