Download the PHP package baraja-core/doctrine-fulltext-search without Composer
On this page you can find all versions of the php package baraja-core/doctrine-fulltext-search. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download baraja-core/doctrine-fulltext-search
More information about baraja-core/doctrine-fulltext-search
Files in baraja-core/doctrine-fulltext-search
Package doctrine-fulltext-search
Short Description Entity search engine, extremely easy to use.
License
Homepage https://github.com/baraja-core/doctrine-fulltext-search
Informations about the package doctrine-fulltext-search
Doctrine Fulltext Search
A powerful, easy-to-use fulltext search engine for Doctrine entities with automatic relevance scoring, query normalization, and machine learning-powered suggestions.
- Define entity and column mappings with simple configuration
- Automatic relevance scoring and result sorting
- Built-in "Did you mean?" suggestions using analytics
- Query normalization with stopword filtering
- Support for entity relationships and custom getters
- Nette Framework integration via DIC extension
π― Core Principles
- Zero Configuration Start: Define your entity map and start searching immediately
- Intelligent Scoring: Results are automatically scored and sorted by relevance (0-512 points)
- Query Normalization: Automatic stopword removal, duplicate filtering, and query sanitization
- Relationship Support: Search across related entities using dot notation
- Analytics-Powered: Machine learning suggestions based on search history
- Extensible Architecture: Override query normalizer and score calculator via interfaces
- Performance Optimized: PARTIAL selection for efficient database queries with configurable timeout
ποΈ Architecture Overview
The package follows a modular architecture with clear separation of concerns:
π§ Main Components
| Component | Purpose |
|---|---|
| Search | Main entry point, orchestrates the search process |
| SelectorBuilder | Fluent API for building search queries with type validation |
| Container | Service container holding all dependencies (PSR-11 compatible) |
| Core | Internal search logic, processes candidate results |
| QueryBuilder | Builds DQL queries with JOIN support for relations |
| Analytics | Stores search statistics, powers "Did you mean?" feature |
| QueryNormalizer | Normalizes queries, removes stopwords |
| ScoreCalculator | Calculates relevance scores with year boost |
| SearchResult | Collection of results implementing Iterator |
| SearchItem | Single search result with entity, title, snippet, and score |
π¦ Installation
It's best to use Composer for installation, and you can also find the package on Packagist and GitHub.
To install, simply use the command:
Requirements
- PHP 8.0 or higher
- ext-mbstring
- Doctrine ORM 2.9+
Nette Framework Integration
Register the DIC extension in your NEON configuration:
The extension automatically registers:
SearchserviceQueryNormalizerserviceScoreCalculatorserviceSearchAccessoraccessorQueryBuilderservice
Manual Instantiation
You can create an instance of Search manually:
With custom normalizer and score calculator:
π Basic Usage
Simple Array-Based Query
The simplest way to perform a search is by defining an entity map:
Fluent SelectorBuilder API
For better type safety and IDE autocompletion, use the SelectorBuilder:
Adding WHERE Conditions
Filter results with custom conditions:
π οΈ Column Modifiers
Column names support special prefixes that control how they're used in search:
| Modifier | Syntax | Description |
|---|---|---|
| Title | :column |
Used as result caption, displayed even without match |
| Search Only | !column |
Searched but excluded from snippet output |
| Select Only | _column |
Loaded but not searched or included in snippet |
| Normal | column |
Searched and included in snippet |
Examples
Using SelectorBuilder:
π Entity Relationships
Search across related entities using dot notation:
Custom Getters
When the getter method differs from the column name:
π Advanced Query Features
Exact Match
Wrap phrases in quotes for exact matching:
Negative Match
Exclude words with minus prefix:
Number Intervals
Search for number ranges:
π Working with Results
SearchResult Entity
The search() method returns a SearchResult entity implementing Iterator:
Getting Results
SearchItem Methods
Each result is a SearchItem with these methods:
| Method | Return Type | Description |
|---|---|---|
getId() |
string\|int |
Entity identifier |
getEntity() |
object |
Original Doctrine entity (PARTIAL loaded) |
getTitle() |
?string |
Normalized title |
getTitleHighlighted() |
?string |
Title with <i class="highlight"> tags |
getSnippet() |
string |
Best matching text snippet |
getSnippetHighlighted() |
string |
Snippet with highlighted words |
getScore() |
int |
Relevance score (0-512) |
entityToArray() |
array |
Entity as normalized array |
Quick HTML Rendering
For rapid prototyping, SearchResult implements __toString():
This outputs styled HTML with:
- Result count and search time
- "Did you mean?" suggestion (if available)
- Results with highlighted titles and snippets
Add ?debugMode=1 to URL to see scores in output.
β "Did You Mean?" Feature
When search returns few or no results, the engine can suggest alternative queries:
How It Works
- Every search query and result count is stored in the
search__search_querytable - Queries are scored based on frequency and result count
- When needed, the system finds similar queries using Levenshtein distance
- The best match is suggested based on combined scoring
Disable analytics for specific searches:
π Scoring System
Results are scored on a scale of 0-512 points based on multiple factors:
Score Calculation
| Factor | Points | Description |
|---|---|---|
| Exact match | +32 | Haystack equals query exactly |
| Contains query | +4 | Query found as substring |
| Substring count | +1-3 | Bonus per occurrence (max 3) |
| Word match | +1-4 | Per word occurrence (max 4) |
| Empty content | -16 | Penalty for empty fields |
| Search-only column | -4 | Reduced weight for ! columns |
| Title column | x6-10 | Multiplier for : columns |
| Year boost | x1-6 | Bonus for current/recent years |
Year Boost
The score calculator automatically boosts results containing recent years:
- Current year and adjacent years receive higher scores
- Particularly relevant for news, events, and time-sensitive content
Custom Score Calculator
Implement IScoreCalculator for custom scoring:
Register in Nette DI:
The container will automatically use your implementation.
π Query Normalization
Queries are automatically normalized before processing:
Default Normalizer Features
- Whitespace normalization: Multiple spaces reduced to single
- Length limit: Truncated to 255 characters
- Stopword removal: Common words filtered (in, it, a, the, of, or, etc.)
- Duplicate removal: Repeated words kept only once
- Special character handling:
%,_,{,}converted or removed - Hash removal:
#123becomes123
Custom Query Normalizer
Implement IQueryNormalizer for project-specific normalization:
βοΈ Configuration Options
Search Timeout
Configure maximum search time (default: 2500ms):
Exact Search Mode
Disable "Did you mean?" suggestions:
User Conditions
Add WHERE conditions to all entity queries:
π Database Entity
The package creates one database table for analytics:
SearchQuery Entity
Table: search__search_query
| Column | Type | Description |
|---|---|---|
| id | UUID | Primary key |
| query | string | Normalized search query (unique) |
| frequency | int | Number of times searched |
| results | int | Last result count |
| score | int | Calculated relevance (0-100) |
| insertedDate | datetime | First search time |
| updatedDate | datetime | Last search time |
The table is automatically created when using Doctrine migrations with the package's entity mappings.
π¨ Styling Highlighted Results
The default highlighter wraps matched words in:
Add CSS for styling:
Custom Highlight Pattern
Use Helpers::highlightFoundWords() with custom pattern:
π Internationalization
The search engine handles accented characters intelligently:
- ASCII conversion: Queries are converted for matching (
cafΓ©matchescafe) - Accent-aware highlighting: Original text preserved with proper highlighting
- Character mapping: Supports Czech, Slovak, Polish, and other Central European languages
Supported character mappings:
amatchesΓ‘,Γ€cmatchesΔematchesΓ¨,Γͺ,Γ©,ΔnmatchesΕrmatchesΕ,ΕsmatchesΕ‘,ΕzmatchesΕΎ,ΕΊ- And more...
π§ Troubleshooting
Column Not Found
The package validates column names against entity metadata. Check your entity properties or use the suggested alternative.
Empty Results
- Verify entity has data in the database
- Check if columns contain searchable text
- Try disabling query normalization for debugging
- Verify WHERE conditions aren't too restrictive
Performance Issues
- Add database indexes on searched columns
- Reduce the number of entities/columns in search
- Lower the search timeout
- Use
!modifier for large text columns - Consider
_modifier for columns only needed in results
π€ Author
Jan BarΓ‘Ε‘ek
- Website: https://baraja.cz
- GitHub: @janbarasek
π License
baraja-core/doctrine-fulltext-search is licensed under the MIT license. See the LICENSE file for more details.
All versions of doctrine-fulltext-search with dependencies
ext-mbstring Version *
baraja-core/lock Version ^v1.0
doctrine/orm Version ^2.9
psr/log Version ^2.0 || ^3.0
ramsey/uuid Version ^4.1
ramsey/uuid-doctrine Version ^1.7
voku/portable-ascii Version ^2.0