Skip to content

Commit c12ae0e

Browse files
dabrtmikadamczyk
andauthored
Apply suggestions from code review
Co-authored-by: Mikolaj Adamczyk <mikadamczyk@gmail.com>
1 parent 51570c0 commit c12ae0e

File tree

1 file changed

+45
-17
lines changed

1 file changed

+45
-17
lines changed

docs/search/search_api.md

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The service should be [injected into the constructor of your command or controll
1919

2020
`SearchService` is also used in the back office of [[= product_name =]], in components such as Universal Discovery Widget or Sub-items List.
2121

22-
### Perform a search
22+
### Perform search
2323

2424
To search through content you need to create a [`LocationQuery`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-Content-LocationQuery.html) and provide your Search Criteria as a series of Criterion objects.
2525

@@ -372,7 +372,7 @@ Instead of comparing keywords, the system compares vectors that represent the se
372372
!!! note "Taxonomy suggestions"
373373

374374
Embedding queries have been introduced primarily to support the [Taxonomy suggestions](taxonomy.md#taxonomy-suggestions) feature but you can use them in other scenarios.
375-
375+
376376
Searching with embeddings can be combined with filtering, which allows the semantic search results to be constrained by content type, location, permissions, or other criteria.
377377

378378
An embedding query is represented by the `Ibexa\Contracts\Core\Repository\Values\Content\EmbeddingQuery` value object.
@@ -401,29 +401,57 @@ Embedding queries can also be combined with filters to narrow down results, such
401401

402402
``` php
403403
use Ibexa\Contracts\Core\Repository\Values\Content\EmbeddingQueryBuilder;
404-
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Embedding;
404+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\ContentTypeIdentifier;
405+
use Ibexa\Contracts\Taxonomy\Search\Query\Value\TaxonomyEmbedding;
405406

406-
// Example embedding vector (float[])
407407
$vector = [
408-
0.0123,
409-
-0.9876,
410-
0.4567,
411-
// ...
408+
0.0123,
409+
-0.9876,
410+
0.4567,
411+
...
412412
];
413413

414-
// Create an Embedding instance with a float[] vector
415-
$embedding = new Embedding($vector);
414+
$embedding = new TaxonomyEmbedding($vector);
416415

417-
// Build the embedding query with the fluent builder
418416
$embeddingQuery = EmbeddingQueryBuilder::create()
419-
->withEmbedding($embedding)
420-
->setLimit(10) // maximum number of results
421-
->setOffset(0) // result offset for pagination
422-
->setPerformCount(true) // optionally count total matching items
423-
->build();
417+
->withEmbedding($embedding)
418+
->setFilter(new ContentTypeIdentifier('article'))
419+
->setLimit(10)
420+
->setOffset(0)
421+
->setPerformCount(true)
422+
->build();
424423

425424
// Execute the query via the repository
426-
$results = $repository->findContent($embeddingQuery);
425+
use Ibexa\Contracts\Core\Repository\Repository;
426+
use Ibexa\Contracts\Core\Repository\SearchService;
427+
use Ibexa\Contracts\Core\Repository\Values\Content\EmbeddingQueryBuilder;
428+
use Ibexa\Contracts\Taxonomy\Search\Query\Value\TaxonomyEmbedding;
429+
430+
final class ExampleService
431+
{
432+
private SearchService $searchService;
433+
434+
public function __construct(Repository $repository)
435+
{
436+
$this->searchService = $repository->getSearchService();
437+
}
438+
439+
public function searchByEmbedding(array $vector): void
440+
{
441+
$query = EmbeddingQueryBuilder::create()
442+
->withEmbedding(new TaxonomyEmbedding($vector))
443+
->setLimit(10)
444+
->setOffset(0)
445+
->build();
446+
447+
$result = $this->searchService->findContent($query);
448+
449+
foreach ($result->searchHits as $hit) {
450+
// ...
451+
}
452+
}
453+
}
454+
427455
```
428456

429457
The `EmbeddingQueryBuilder` ensures that the query is correctly configured before execution.

0 commit comments

Comments
 (0)