Giter VIP home page Giter VIP logo

Comments (4)

curquiza avatar curquiza commented on May 19, 2024 1

I let this issue opened since there is still some objectID in the code, but you did most of the job, thank you very much 😁

from meilisearch-symfony.

codedge avatar codedge commented on May 19, 2024 1

Yes, for the objectId I would create a new branch & PR to work on.

from meilisearch-symfony.

skylord123 avatar skylord123 commented on May 19, 2024

Why are we removing the objectID usage? When objects get placed into Meilisearch the objectID param is set to the primary keys of the entity (or just the single value if there aren't multiple primary keys). When we pull objects down out of MeiliSearch we can just parse the objectID field to figure out how to query for the entity.

I sort of like how it works now because it keeps the config to a minimum (don't have to define the primary key in config) and also doesn't require running two http requests for every search (to figure out the primary key). It would be cool to override the objectID field with your own way if you really want but how it is currently setup is easiest for users to get started with IMO.

I just updated the MeiliSearchService::search function to the following and it works really well (handles multiple primary keys and entities that don't use id as their primary key):

    public function search(
        ObjectManager $objectManager,
        string $className,
        string $query = '',
        array $requestOptions = []
    ): array {
        $this->assertIsSearchable($className);

        $ids = $this->engine->search($query, $this->searchableAs($className), $requestOptions);
        $results = [];

        // Check if the engine returns results in "hits" key
        if (!isset($ids['hits'])) {
            throw new SearchHitsNotFoundException('There is no "hits" key in the search results.');
        }

        foreach ($ids['hits'] as $objectID) {
            if (in_array($className, $this->aggregators, true)) {
                $entityClass = $className::getEntityClassFromObjectID($objectID);
                $id = $className::getEntityIdFromObjectID($objectID);
            } else {
                $multipleIds = explode('__', $objectID['objectID']);
                $entityClass = $className;

                if (0 === count($ids)) {
                    throw new Exception('Entity has no primary key');
                } else if(count($multipleIds) === 1) {
                    $id = $multipleIds[0];
                    $idFieldName = $objectManager->getClassMetadata($entityClass)->getSingleIdentifierFieldName();
                    $repo = $objectManager->getRepository($entityClass);
                    $entity = $repo->findOneBy([$idFieldName => $id]);
                } else {
                    $idFieldNames = $objectManager->getClassMetadata($entityClass)->getIdentifierFieldNames();
                    $searchIdFields = [];
                    foreach($multipleIds as $idPart) {
                        list($idFieldName, $idFieldValue) = explode("-", $idPart);
                        if(!in_array($idFieldName, $idFieldNames)) {
                            continue;
                        }
                        $searchIdFields[$idFieldName] = $idFieldValue;
                    }
                    $repo = $objectManager->getRepository($entityClass);
                    $entity = $repo->findOneBy($searchIdFields);
                }
            }

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

            if (null !== $entity) {
                $results[] = $entity;
            }
        }

        return $results;
    }
    ```

from meilisearch-symfony.

codedge avatar codedge commented on May 19, 2024

So the second tasks is fixed as well (#70). The objectId stuff is done in #66 - I would also start taking care of that but first let's crush #76 (little reminder @curquiza ) 😉

from meilisearch-symfony.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.