A PHP library for building Elasticsearch DSL queries and managing indices.
PHP Elasticsearch DSL 查询构建和索引管理库。
- PHP 8.1+
- Elasticsearch 8.x
composer require ykan/elastickit:^8use ElasticKit\Index\Index;
// Setup
$client = \Elastic\Elasticsearch\ClientBuilder::create()->setHosts(['http://localhost:9200'])->build();
Index::setClient($client);// Define index
class ProductIndex extends Index
{
protected $name = 'products';
protected $mappings = [
'properties' => [
'title' => ['type' => 'text'],
'price' => ['type' => 'float'],
'status' => ['type' => 'keyword'],
],
];
}// Search
$results = ProductIndex::query()
->match('title', 'elasticsearch')
->sort('price', 'asc')
->size(20)
->get();
$results->total(); // hit count
$results->docs(); // _source array
// CRUD
ProductIndex::doc(1)->create(['title' => 'New Product', 'price' => 29.99]);
ProductIndex::doc(1)->update(['price' => 39.99]);
ProductIndex::doc(1)->delete();MIT