Important
You are browsing the documentation for version 4.1 of OroCommerce, OroCRM and OroPlatform, which is no longer maintained. Read version 5.1 (the latest LTS version) of the Oro documentation to get up-to-date information.
See our Release Process documentation for more information on the currently supported and upcoming releases.
Perform Search¶
Search Engine¶
Search engine is an entry point in Oro application that is used to perform search request. Search engine implements Oro\Bundle\SearchBundle\Engine\EngineInterface interface with one method - search(Query $query, array $context = []).
This method accepts a low level query object (Oro\Bundle\SearchBundle\QueryQuery), sends a request to the search engine and returns result object (Oro\Bundle\SearchBundle\QueryResult). The $context variable may be used to pass additional parameters specific for the search engines. A default website search engine is accessible via the oro_website_search.engine service.
Every engine may have its own requests to modify the way request is sent. However, every engine should support the Oro\Bundle\WebsiteSearchBundle\Event\BeforeSearchEvent out of the box. This event is triggered before the engine queries the search index storage, so the developer can check request parameters and modify the query according to the business logics.
The event name - oro_website_search.before_search - can be used to add a new listener.
Here is an example of such listener definition:
1 services:
2 oro_product.product_visibility_restriction_listener:
3 class: Oro\Bundle\ProductBundle\EventListener\ProductVisibilityRestrictionListener
4 arguments:
5 - '@oro_product.product.manager'
6 - '@oro_website_search.provider.search_mapping'
7 tags:
8 - { name: kernel.event_listener, event: oro_website_search.before_search, method: process }
Search Query¶
Developer may start constructing a search query by adjusting the Oro\Bundle\SearchBundle\Query\Query example.
Let’s assume there is a product entity with the following index structure:
1 Oro\Bundle\ProductBundle\Entity\Product:
2 alias: oro_product
3 fields:
4 -
5 name: sku
6 type: text
7 -
8 name: name
9 type: text
10 -
11 name: price
12 type: decimal
13 -
14 name: all_text
15 type: text
and you want the following query to be executed:
SELECT
text.sku,
text.name,
decimal.price
FROM
oro_product
WHERE
text.all_text ~ product
ORDER_BY
decimal.price ASC
The following example illustrates how to build and execute such a query:
1 use Oro\Bundle\SearchBundle\Query\Query;
2 use Oro\Bundle\SearchBundle\Query\Result;
3 use Oro\Bundle\SearchBundle\Query\Criteria\Criteria;
4 use Oro\Bundle\SearchBundle\Engine\EngineInterface;
5
6 $query = new Query();
7 $query
8 ->addSelect(['text.sku', 'text.name', 'decimal.price'])
9 ->from('oro_product');
10 $query->getCriteria()
11 ->andWhere(Criteria::expr()->contains('text.all_text', 'product'))
12 ->orderBy(['decimal.price' => Query::ORDER_ASC]);
13
14 /** @var EngineInterface $engine */
15 $engine = $this->getContainer()->get('oro_website_search.engine');
16
17 /** @var Result $engine */
18 $result = $engine->search($query);
Where the $result variable is an Oro\Bundle\SearchBundle\Query\Result object that contains collection of Oro\Bundle\SearchBundle\Query\Result\Item objects. Every object in the collection contains information about an entity involved in a search query: entity class, entity ID and additional selected data.
This type of query is used when the search engine and its configuration is unknown, and you need an instance of an engine which implements Oro\Bundle\SearchBundle\Engine\EngineInterface.
As a universal, search-engine agnostic solution, you may use the Oro\Bundle\WebsiteSearchBundle\Query\WebsiteSearchQuery that implements the Oro\Bundle\SearchBundle\Query\SearchQueryInterface interface. This type does not require additional search-engine-specific parameters and can be used in components that should be able to work with any search engine. Basically, such query encapsulates previous type of query and the required parameters to perform search (in this case it is a search engine).
To create the latter type of query, you can use specific factory for website search, or use the generic one and specify the index you used:
1 $query = $this->container->get('oro_website_search.query_factory')->create();
2 // OR
3 $query = $this->container->get('oro_search.query_factory')->create(['search_index' => 'website']);
Let’s execute the request to the search index mentioned above using second type of query:
1 use Oro\Bundle\SearchBundle\Query\SearchQueryInterface;
2 use Oro\Bundle\SearchBundle\Query\Result;
3 use Oro\Bundle\SearchBundle\Query\Criteria\Criteria;
4
5 /** @var SearchQueryInterface $query */
6 $query = $this->container->get('oro_website_search.query_factory')->create();
7 $query
8 ->addSelect(['text.sku', 'text.name', 'decimal.price'])
9 ->setFrom('oro_product')
10 ->addWhere(Criteria::expr()->contains('text.all_text', 'product'))
11 ->setOrderBy('decimal.price', Query::ORDER_ASC);
12
13 /** @var Result $engine */
14 $result = $query->getResult();
As you can see, the interface is similar, but you are not aware about search engine and index type that is used. This information is resolved on the factory level, so you can override and decorate the service that represents the factory to customize search behaviour.
Note
There is a hard limit of 1000 on quantity of results which search query can return.
Search Repository¶
To store custom queries that are used to receive data from the search index, SearchBundle provides the search repository class – Oro\Bundle\SearchBundle\Query\SearchRepository – with default logic. The WebsiteSearchBundle extends this class and adds Oro\Bundle\WebsiteSearchBundle\Query\WebsiteSearchRepository. The only difference between these repositories is the type of index that is used. Basic repository uses default index from SearchBundle, and the website search repository uses website search index.
You may want to use website repository to create a specialized repository for an entity in the website search index. Similar to the Doctrine object repository, you can use this one to store the website search related methods.
Let’s have a look at the example of such repository:
1 namespace Oro\Bundle\ProductBundle\Search;
2
3 use Oro\Bundle\SearchBundle\Query\Criteria\Criteria;
4 use Oro\Bundle\SearchBundle\Query\Query;
5 use Oro\Bundle\SearchBundle\Query\Result;
6 use Oro\Bundle\SearchBundle\Query\SearchQueryInterface;
7 use Oro\Bundle\WebsiteSearchBundle\Query\WebsiteSearchRepository;
8
9 class ProductRepository extends WebsiteSearchRepository
10 {
11 /**
12 * @param string $string
13 * @return Result
14 */
15 public function findByAllText($string)
16 {
17 /** @var SearchQueryInterface $query */
18 $query = $this->createQuery();
19 $query
20 ->addSelect(['text.sku', 'text.name', 'decimal.price'])
21 ->addWhere(Criteria::expr()->contains('text.all_text', $string))
22 ->setOrderBy('decimal.price', Query::ORDER_ASC);
23
24 return $query->getResult();
25 }
26 }
This is exactly the same query described before encapsulated in the repository. No need to call the factory methods. Define this repository as a service and use it wherever you need it.
Below is an example of search repository service declaration:
1 services:
2 oro_product.website_search.repository.product:
3 parent: oro_website_search.repository.abstract
4 class: Oro\Bundle\ProductBundle\Search\ProductRepository
5 calls:
6 - [setEntityName, ['Oro\Bundle\ProductBundle\Entity\Product']]
Parent oro_website_search.repository.abstract service already contains all required constructor arguments. You may specify the entity name to automatically fill the FROM part of the query. This call is optional. If it is missing, an empty query will be created.