Skip to content

Normalize entity id before index or remove #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace MeiliSearch\Bundle;

use function count;
use MeiliSearch\Client;
use MeiliSearch\Exceptions\ApiException;

Expand Down Expand Up @@ -49,7 +48,7 @@ public function index($searchableEntities): array
$data[$indexUid] = [];
}

$data[$indexUid][] = $searchableArray + ['objectID' => $entity->getId()];
$data[$indexUid][] = $searchableArray + ['objectID' => $this->normalizeId($entity->getId())];
}

$result = [];
Expand Down Expand Up @@ -89,7 +88,7 @@ public function remove($searchableEntities): array
$data[$indexUid] = [];
}

$data[$indexUid][] = $entity->getId();
$data[$indexUid][] = $this->normalizeId($entity->getId());
}

$result = [];
Expand Down Expand Up @@ -143,4 +142,13 @@ public function count(string $query, string $indexName, array $searchParams): in
{
return $this->client->index($indexName)->search($query, $searchParams)->getHitsCount();
}

private function normalizeId($id)
{
if (is_object($id) && method_exists($id, '__toString')) {
return (string) $id;
}

return $id;
}
}
2 changes: 1 addition & 1 deletion src/Services/MeiliSearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function search(
}

$repo = $objectManager->getRepository($entityClass);
$entity = $repo->findOneBy(['id' => $id]);
$entity = $repo->find($id);

if (null !== $entity) {
$results[] = $entity;
Expand Down
15 changes: 15 additions & 0 deletions tests/BaseKernelTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use MeiliSearch\Bundle\Test\Entity\Comment;
use MeiliSearch\Bundle\Test\Entity\Image;
use MeiliSearch\Bundle\Test\Entity\Link;
use MeiliSearch\Bundle\Test\Entity\ObjectId\DummyObjectId;
use MeiliSearch\Bundle\Test\Entity\Page;
use MeiliSearch\Bundle\Test\Entity\Post;
use MeiliSearch\Bundle\Test\Entity\Tag;
use MeiliSearch\Exceptions\ApiException;
Expand Down Expand Up @@ -54,6 +56,19 @@ protected function createPost($id = null): Post
return $post;
}

protected function createPage(int $id): Page
{
$page = new Page();
$page->setTitle('Test Page');
$page->setContent('Test content page');
$page->setId(new DummyObjectId($id));

$this->entityManager->persist($page);
$this->entityManager->flush();

return $page;
}

protected function createSearchablePost(): SearchableEntity
{
$post = $this->createPost(random_int(100, 300));
Expand Down
20 changes: 20 additions & 0 deletions tests/Entity/ObjectId/DummyObjectId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace MeiliSearch\Bundle\Test\Entity\ObjectId;

class DummyObjectId
{
private int $id;

public function __construct(int $id)
{
$this->id = $id;
}

public function __toString()
{
return (string) $this->id;
}
}
70 changes: 70 additions & 0 deletions tests/Entity/Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace MeiliSearch\Bundle\Test\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
* @ORM\Entity
* @ORM\Table(name="pages")
*/
class Page
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
* @ORM\Column(type="object")
*/
private $id = null;

/**
* @ORM\Column(type="string", nullable=true)
* @Groups({"searchable"})
*/
private ?string $title = null;

/**
* @ORM\Column(type="text", nullable=true)
* @Groups({"searchable"})
*/
private ?string $content = null;

public function getId()
{
return $this->id;
}

public function setId($id): self
{
$this->id = $id;

return $this;
}

public function getTitle(): ?string
{
return $this->title;
}

public function setTitle(?string $title): self
{
$this->title = $title;

return $this;
}

public function getContent(): ?string
{
return $this->content;
}

public function setContent(?string $content): self
{
$this->content = $content;

return $this;
}
}
7 changes: 7 additions & 0 deletions tests/Integration/CommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function testSearchImportAndClearWithoutIndices(): void
$this->createPost();
}

for ($i = 0; $i <= 5; ++$i) {
$this->createPage($i);
}

for ($i = 0; $i <= 5; ++$i) {
$this->createTag(['id' => $i]);
}
Expand All @@ -78,6 +82,8 @@ public function testSearchImportAndClearWithoutIndices(): void
Indexed 6 / 6 MeiliSearch\Bundle\Test\Entity\Tag entities into sf_phpunit__tags index
Indexed 6 / 6 MeiliSearch\Bundle\Test\Entity\Tag entities into sf_phpunit__aggregated index
Importing for index MeiliSearch\Bundle\Test\Entity\Link
Importing for index MeiliSearch\Bundle\Test\Entity\Page
Indexed 6 / 6 MeiliSearch\Bundle\Test\Entity\Page entities into sf_phpunit__pages index
Importing for index MeiliSearch\Bundle\Test\Entity\Post
Indexed 6 / 6 MeiliSearch\Bundle\Test\Entity\Post entities into sf_phpunit__posts index
Indexed 6 / 6 MeiliSearch\Bundle\Test\Entity\Post entities into sf_phpunit__aggregated index
Expand All @@ -100,6 +106,7 @@ public function testSearchImportAndClearWithoutIndices(): void
Cleared sf_phpunit__aggregated index of MeiliSearch\Bundle\Test\Entity\ContentAggregator
Cleared sf_phpunit__tags index of MeiliSearch\Bundle\Test\Entity\Tag
Cleared sf_phpunit__tags index of MeiliSearch\Bundle\Test\Entity\Link
Cleared sf_phpunit__pages index of MeiliSearch\Bundle\Test\Entity\Page
Done!

EOD, $clearOutput);
Expand Down
58 changes: 58 additions & 0 deletions tests/Integration/EventListener/DoctrineEventSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\ORM\Event\LifecycleEventArgs;
use MeiliSearch\Bundle\EventListener\DoctrineEventSubscriber;
use MeiliSearch\Bundle\Test\BaseKernelTestCase;
use MeiliSearch\Bundle\Test\Entity\Page;
use MeiliSearch\Bundle\Test\Entity\Post;

class DoctrineEventSubscriberTest extends BaseKernelTestCase
Expand All @@ -30,6 +31,23 @@ public function testPostPersist(): void
$this->assertSame(2, $result[0]->getId());
}

public function testPostPersistWithObjectId(): void
{
$this->createPage(1);
$page = $this->createPage(2);

$eventArgs = new LifecycleEventArgs($page, $this->entityManager);

$subscriber = new DoctrineEventSubscriber($this->searchService, []);
$subscriber->postPersist($eventArgs);
sleep(1);

$result = $this->searchService->search($this->entityManager, Page::class, $page->getTitle());

$this->assertCount(1, $result);
$this->assertSame((string) $page->getId(), (string) $result[0]->getId());
}

/**
* This tests creates two posts in the database, but only one is triggered via an event to MS.
*/
Expand All @@ -49,6 +67,23 @@ public function testPostUpdate(): void
$this->assertSame(2, $result[0]->getId());
}

public function testPostUpdateWithObjectId(): void
{
$this->createPage(1);
$page = $this->createPage(2);

$eventArgs = new LifecycleEventArgs($page, $this->entityManager);

$subscriber = new DoctrineEventSubscriber($this->searchService, []);
$subscriber->postUpdate($eventArgs);
sleep(1);

$result = $this->searchService->search($this->entityManager, Page::class, $page->getTitle());

$this->assertCount(1, $result);
$this->assertSame((string) $page->getId(), (string) $result[0]->getId());
}

/**
* This tests creates posts in the database, send it to MS via a trigger. Afterwards Doctrines 'preRemove' event
* is going to remove that entity from MS.
Expand Down Expand Up @@ -79,4 +114,27 @@ public function testPreRemove(): void

$this->assertCount(0, $result);
}

public function testPreRemoveWithObjectId(): void
{
$page = $this->createPage(1);

$eventArgs = new LifecycleEventArgs($page, $this->entityManager);

$subscriber = new DoctrineEventSubscriber($this->searchService, []);
$subscriber->postPersist($eventArgs);
sleep(1);

$result = $this->searchService->search($this->entityManager, Page::class, $page->getTitle());

$this->assertCount(1, $result);
$this->assertSame((string) $page->getId(), (string) $result[0]->getId());

$subscriber->preRemove($eventArgs);
sleep(1);

$result = $this->searchService->search($this->entityManager, Page::class, $page->getTitle());

$this->assertCount(0, $result);
}
}
3 changes: 3 additions & 0 deletions tests/config/meili_search.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ meili_search:
- name: tags
class: 'MeiliSearch\Bundle\Test\Entity\Link'
index_if: isSponsored
- name: pages
class: 'MeiliSearch\Bundle\Test\Entity\Page'
enable_serializer_groups: true