Skip to content

Commit d9a52ff

Browse files
committed
docs: Update documentation and dependencies
1 parent bb7c732 commit d9a52ff

File tree

2 files changed

+166
-4
lines changed

2 files changed

+166
-4
lines changed

README.md

+156
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,162 @@ $results = User::search('John')
125125
->get();
126126
```
127127

128+
### Advanced Elasticsearch Features
129+
130+
#### Vector Search
131+
132+
```php
133+
// Define a model with vector fields
134+
#[ElasticModel(index: 'documents')]
135+
class Document extends Model
136+
{
137+
#[ElasticField(type: 'dense_vector', options: ['dims' => 768])]
138+
public array $embedding;
139+
140+
#[ElasticField(type: 'text')]
141+
public string $content;
142+
}
143+
144+
// Perform KNN vector search
145+
$results = Document::query()
146+
->vectorSearch('embedding', $queryVector, k: 10)
147+
->get();
148+
```
149+
150+
#### Semantic Search
151+
152+
```php
153+
// Using embedding services (OpenAI, HuggingFace, etc.)
154+
$results = Document::query()
155+
->semanticSearch(
156+
"What is machine learning?",
157+
'embedding',
158+
'openai'
159+
)
160+
->get();
161+
162+
// Sparse vector search with ELSER
163+
$results = Document::query()
164+
->sparseVectorSearch("How does AI work?", 'content_embedding')
165+
->get();
166+
```
167+
168+
#### Hybrid Search & Semantic Reranking
169+
170+
```php
171+
// Hybrid search combining text and vector scores
172+
$results = Document::query()
173+
->hybridSearch(
174+
"machine learning applications",
175+
['content'],
176+
'embedding',
177+
$queryVector,
178+
textWeight: 0.3,
179+
vectorWeight: 0.7
180+
)
181+
->get();
182+
183+
// Semantic reranking to improve relevance
184+
$results = Document::query()
185+
->search("artificial intelligence")
186+
->semanticRerank("How do neural networks work?")
187+
->get();
188+
```
189+
190+
#### ES|QL Support
191+
192+
```php
193+
// Using Elasticsearch's SQL-like query language
194+
$results = Document::query()
195+
->esql("FROM documents WHERE match(content, 'artificial intelligence') | LIMIT 10")
196+
->get();
197+
```
198+
199+
#### Performance Optimizations
200+
201+
```php
202+
// Field selection and exclusion
203+
$results = Document::query()
204+
->select(['content', 'title']) // Only return these fields
205+
->exclude(['embedding']) // Exclude large vector fields
206+
->trackTotalHits(false) // Disable exact hit counting for better performance
207+
->search("machine learning")
208+
->get();
209+
```
210+
211+
### Data Transfer Objects & Pagination
212+
213+
Laravel Elastoquent integrates with [spatie/laravel-data](https://github.com/spatie/laravel-data) to provide structured DTOs for your Elasticsearch results:
214+
215+
```php
216+
// Using the built-in ElasticDocument DTO
217+
$results = Document::query()
218+
->where('category', 'technology')
219+
->asDocument() // Return results as ElasticDocument DTOs
220+
->get();
221+
222+
// Using a custom DTO that extends ElasticDocument
223+
class ProductDocument extends ElasticDocument
224+
{
225+
public function getFormattedPrice(): string
226+
{
227+
return '$' . number_format($this->field('price', 0), 2);
228+
}
229+
}
230+
231+
$results = Product::query()
232+
->asData(ProductDocument::class) // Use custom DTOs
233+
->get();
234+
235+
// Enhanced pagination with DTOs
236+
$paginatedResults = Product::query()
237+
->where('active', true)
238+
->asDocument()
239+
->paginate(15);
240+
241+
// Accessing pagination metadata
242+
echo "Showing {$paginatedResults->count()} of {$paginatedResults->total()} results";
243+
echo "Page {$paginatedResults->currentPage()} of {$paginatedResults->lastPage()}";
244+
245+
// Converting to Spatie Data collection
246+
use Spatie\LaravelData\Data;
247+
248+
class ProductData extends Data
249+
{
250+
public function __construct(
251+
public string $id,
252+
public string $name,
253+
public float $price,
254+
) {}
255+
256+
// Transform from ElasticDocument to Spatie Data
257+
public static function fromElasticDocument(ElasticDocument $doc): self
258+
{
259+
return new self(
260+
id: $doc->id,
261+
name: $doc->field('name'),
262+
price: (float)$doc->field('price', 0)
263+
);
264+
}
265+
}
266+
267+
// Map the results to Spatie Data objects
268+
$dataCollection = $results->map(fn($doc) => ProductData::fromElasticDocument($doc));
269+
```
270+
271+
### Retriever API Support
272+
273+
```php
274+
// Using retrievers for advanced semantic search
275+
$results = Document::query()
276+
->retriever(
277+
"How does machine learning work?",
278+
['content'],
279+
'hybrid' // Use hybrid retrieval (text + semantic)
280+
)
281+
->get();
282+
```
283+
128284
### Relationships
129285

130286
```php

composer.json

+10-4
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@
2222
"require": {
2323
"php": "^8.1",
2424
"laravel/framework": "^10.0",
25-
"elasticsearch/elasticsearch": "^8.0"
25+
"elasticsearch/elasticsearch": "^8.0",
26+
"guzzlehttp/guzzle": "^7.0",
27+
"illuminate/pagination": "^10.0",
28+
"illuminate/support": "^10.0",
29+
"spatie/laravel-data": "^3.0"
2630
},
2731
"require-dev": {
32+
"laravel/pint": "^1.0",
33+
"orchestra/testbench": "^8.35",
2834
"pestphp/pest": "^2.0",
2935
"pestphp/pest-plugin-laravel": "^2.0",
30-
"phpstan/phpstan": "^1.10",
31-
"laravel/pint": "^1.0"
36+
"phpstan/phpstan": "^1.10"
3237
},
3338
"extra": {
3439
"laravel": {
@@ -42,7 +47,8 @@
4247
"config": {
4348
"sort-packages": true,
4449
"allow-plugins": {
45-
"pestphp/pest-plugin": true
50+
"pestphp/pest-plugin": true,
51+
"php-http/discovery": true
4652
}
4753
},
4854
"scripts": {

0 commit comments

Comments
 (0)