|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +ElasticQuery is a PHP library that converts Elasticsearch query DSL into typed PHP objects. Instead of building queries as arrays, developers can use strongly-typed classes that mirror the Elasticsearch documentation. |
| 8 | + |
| 9 | +## Development Commands |
| 10 | + |
| 11 | +```bash |
| 12 | +# Install/update dependencies |
| 13 | +make composer |
| 14 | + |
| 15 | +# Run static analysis (PHPStan level 7) |
| 16 | +make phpstan |
| 17 | + |
| 18 | +# Run code style checks (Slevomat coding standard) |
| 19 | +make cs |
| 20 | + |
| 21 | +# Auto-fix code style issues |
| 22 | +make cbf |
| 23 | + |
| 24 | +# Run tests (Nette Tester) |
| 25 | +make tests |
| 26 | + |
| 27 | +# Run a single test file |
| 28 | +vendor/bin/tester -s -p php --colors 1 -C tests/SpameriTests/ElasticQuery/Path/To/Test.phpt |
| 29 | +``` |
| 30 | + |
| 31 | +## Architecture |
| 32 | + |
| 33 | +### Core Query Building |
| 34 | + |
| 35 | +**ElasticQuery** (`src/ElasticQuery.php`) - Main entry point for building queries. Composes: |
| 36 | +- `QueryCollection` - Boolean query with must/should/mustNot collections |
| 37 | +- `FilterCollection` - Filter context queries |
| 38 | +- `AggregationCollection` - Aggregation definitions |
| 39 | +- `Options` - Pagination, sorting, scroll settings |
| 40 | +- `Highlight` - Search result highlighting |
| 41 | +- `FunctionScore` - Custom scoring functions |
| 42 | + |
| 43 | +All query/aggregation objects implement `toArray()` to serialize to Elasticsearch-compatible format. |
| 44 | + |
| 45 | +### Query Objects (`src/Query/`) |
| 46 | + |
| 47 | +Leaf queries implement `LeafQueryInterface`: |
| 48 | +- `ElasticMatch`, `MatchPhrase`, `MultiMatch`, `PhrasePrefix` - Full-text queries |
| 49 | +- `Term`, `Terms`, `Range`, `Exists`, `WildCard` - Term-level queries |
| 50 | +- `Nested`, `GeoDistance`, `Fuzzy` - Specialized queries |
| 51 | + |
| 52 | +Collection queries (`MustCollection`, `ShouldCollection`, `MustNotCollection`) also implement `LeafQueryInterface`, enabling nested boolean logic. |
| 53 | + |
| 54 | +### Aggregations (`src/Aggregation/`) |
| 55 | + |
| 56 | +- `LeafAggregationCollection` wraps aggregation definitions with nested sub-aggregations |
| 57 | +- Metric aggregations: `Min`, `Max`, `Avg`, `TopHits` |
| 58 | +- Bucket aggregations: `Term`, `Range`, `Histogram`, `Nested`, `Filter` |
| 59 | + |
| 60 | +### Response Mapping (`src/Response/`) |
| 61 | + |
| 62 | +`ResultMapper` converts Elasticsearch array responses to typed objects: |
| 63 | +- `ResultSearch` - Standard search results with hits and aggregations |
| 64 | +- `ResultSingle` - Single document retrieval |
| 65 | +- `ResultBulk` - Bulk operation results |
| 66 | +- `ResultVersion` - Cluster version info |
| 67 | + |
| 68 | +### Index Mapping (`src/Mapping/`) |
| 69 | + |
| 70 | +Classes for defining Elasticsearch index mappings: |
| 71 | +- Analyzers: Standard, custom dictionary analyzers for multiple languages |
| 72 | +- Filters: Stemming, synonyms, stop words, edge n-grams |
| 73 | +- Tokenizers: Pattern, whitespace, etc. |
| 74 | +- `Settings` - Index settings configuration |
| 75 | + |
| 76 | +### Document Handling (`src/Document.php`, `src/Document/`) |
| 77 | + |
| 78 | +`Document` wraps index name, body, and optional ID for Elasticsearch client calls. |
| 79 | + |
| 80 | +### Options & Sorting (`src/Options.php`, `src/Options/`) |
| 81 | + |
| 82 | +- `Options` - Pagination (size, from), scroll support, min_score, version inclusion |
| 83 | +- `Sort` - Field sorting with ASC/DESC and missing value handling |
| 84 | +- `GeoDistanceSort` - Geo-spatial sorting by distance from a point |
| 85 | +- `SortCollection` - Manages multiple sort criteria |
| 86 | + |
| 87 | +### Function Score (`src/FunctionScore.php`, `src/FunctionScore/`) |
| 88 | + |
| 89 | +Custom scoring with multiple score functions: |
| 90 | +- `FieldValueFactor` - Score based on numeric field values with modifiers (log, sqrt, etc.) |
| 91 | +- `Weight` - Constant weight multiplier when filter matches |
| 92 | +- `RandomScore` - Randomized scoring with optional seed for consistency |
| 93 | + |
| 94 | +Score modes: multiply, sum, avg, first, max, min |
| 95 | + |
| 96 | +## Coding Standards |
| 97 | + |
| 98 | +- PHP 8.2+ with strict types |
| 99 | +- Fully qualified class names in annotations |
| 100 | +- Fully qualified global functions and constants |
| 101 | +- Tab indentation (no spaces) |
| 102 | +- Trailing commas in arrays, function calls, and declarations |
| 103 | +- Constructor property promotion where applicable |
| 104 | +- No Yoda comparisons |
| 105 | +- Strict equality operators only (`===`, `!==`) |
0 commit comments