A PHP library that converts Elasticsearch query DSL into strongly-typed PHP objects. Instead of building queries as arrays, use type-safe classes that mirror the Elasticsearch documentation.
- Type-safe queries - Full-text, term-level, compound, geo, and nested queries
- Aggregations - Metric (min, max, avg) and bucket (terms, histogram, range, filter) aggregations
- Response mapping - Automatic mapping of Elasticsearch responses to typed objects
- Index mapping - Define index settings, analyzers, tokenizers, and filters
- Function scoring - Custom scoring with field value factors, weights, and random scores
- Highlighting - Search result highlighting support
- Pagination & sorting - Options for size, offset, scroll, and geo-distance sorting
- PHP 8.2 or higher
Install via Composer:
composer require spameri/elastic-queryuse Spameri\ElasticQuery\ElasticQuery;
use Spameri\ElasticQuery\Query\ElasticMatch;
use Spameri\ElasticQuery\Query\Term;
// Create a query
$query = new ElasticQuery();
// Add a must query (AND condition)
$query->addMustQuery(new ElasticMatch('title', 'Elasticsearch'));
// Add a filter (cached, no scoring)
$query->addFilter(new Term('status', 'published'));
// Set pagination
$query->options()->changeSize(10);
$query->options()->changeFrom(0);
// Convert to array for Elasticsearch client
$body = $query->toArray();Learn more in the documentation:
- Usage - Integration examples
- Query Objects - All query types
- Aggregation Objects - Aggregation types
- Result Objects - Response mapping
- Options & Sorting - Pagination, sorting, scroll
- Highlight & Function Score - Highlighting and custom scoring
- Mapping & Settings - Index configuration
MIT