Symfony 2 FOQElasticaBundle search on several objects

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

If you have this configuration:

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: 

Then you can search indexes as follows:

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

But what if you want to search for multiple objects with a single function? Sort of...

Configuration:

 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 does not work, but I was wondering if there is a way to do this one search on multiple objects and get results based on their boost property?

+4
source share
2 answers

As I see, there are two ways to do what you want. You can create a parent for the user and client and add it as a type to your index. Just take a look at Doctrine Inheritance ; I am not sure, however, if and how the FOQ_ElasticaBundle processes them while storing these objects in the index. This is just a pointer in the direction, I'm not sure if this will work at all!

I would recommend the following approach: index search instead of type. You can use foq_elastica.index_manager to get the index (website) you need, and then create a query that uses a type filter to restrict the results to your Custom and client types.

+1
source

ANSWER FROM OP

Here is my solution ... I edited my configuration file to find in the root directory of my site:

 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 as follows ...

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

which will search in my User and Client object!

+4
source

Source: https://habr.com/ru/post/1437312/


All Articles