CedricD
CedricD

Reputation: 93

Symfony 2 FOQElasticaBundle search on multiple Entities

I started messing with the Elastic Search Bundle with Symfony 2 and have a question about the search function with entities.

If you have a config like this :

foq_elastica:
clients:
    default: { host: localhost, port: 9200 }
indexes:
    website:
        client: default
        types:
            user:
                mappings:
                    username: { boost: 5 }
                    firstName: { boost: 3 }
                persistence:
                    driver: orm # orm, mongodb, propel are available
                    model: Application\UserBundle\Entity\User
                    provider:

You can then search indexes like this :

$userType = $this->container->get('foq_elastica.index.website.user');

$resultSet = $userType->search('bob');

But what if you want to search multiple entities with a single function? Something like...

Config :

foq_elastica:
clients:
    default: { host: localhost, port: 9200 }
indexes:
    website:
        client: default
        types:
            user:
                mappings:
                    username: { boost: 5 }
                    firstName: { boost: 3 }
                persistence:
                    driver: orm
                    model: Application\UserBundle\Entity\User
                    provider:
            client:
                mappings:
                    clientname: { boost: 5 }
                persistence:
                    driver: orm 
                    model: Application\UserBundle\Entity\Client
                    provider:

Search function :

$Type = $this->container->get(['foq_elastica.index.website.user', 'foq_elastica.index.website.client']);

$resultSet = $Type->search('bob');

The code above doesn't work but I was wondering if there was a way like that to do a single search on multiple entities and get results based on their boost property?

Upvotes: 4

Views: 2848

Answers (2)

Robert Harvey
Robert Harvey

Reputation: 180787

ANSWER FROM OP

Here's my solution...I edited my config file to have the finder at the root of my website like this :

foq_elastica:
clients:
    default: { host: localhost, port: 9200 }
indexes:
    website:
        client: default
        finder:
        types:
            user:
                mappings:
                    username: { boost: 5 }
                    firstName: { boost: 3 }
                persistence:
                    driver: orm
                    model: Application\UserBundle\Entity\User
                    provider:
            client:
                mappings:
                    clientname: { boost: 5 }
                persistence:
                    driver: orm 
                    model: Application\UserBundle\Entity\Client
                    provider:

And I call my search like this...

$finder = $this->container->get('foq_elastica.finder.website');

$results = $finder->find('bob');

Which will search in my User and Client entity!

Upvotes: 4

dbrumann
dbrumann

Reputation: 17166

As I see it, there are two ways to do what you want. You could create a parent entity for User and Client and add it as a type to your index. Just take a look at Doctrine's Inheritance Mapping; I am not sure however if and how FOQ_ElasticaBundle handles these when persisting these entities in the index. This is just a pointer in the direction, I am not sure if this would work at all!

I would recommend the following approach: Searching the index instead of the type. You can use foq_elastica.index_manager to retrieve the index you want (website) and then build a query which uses the Type-filter to limit the results to your User and Client-Type.

Upvotes: 1

Related Questions