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.
Search Relevance Weight¶
This article describes the purpose of search relevance weigh and describes the way to customize this relevance in the website search index.
Search relevance weight (also called search weight or search relevance) is a positive decimal number that affects the order of search results. It is used as a multiplier for the original relevance calculated by the search engine.
The original search relevance may differ depending on the search query and additional conditions. The multiplier enables a developer to change the original search relevance which in turn changes the order of the results.
From the code level perspective, search relevance weight is just another search index decimal field called relevance_weight. This name is stored in the Oro\Bundle\SearchBundle\Engine\IndexerInterface::WEIGHT_FIELD constant that enables a developer to fill this field with the data during the indexation and pass it to the search engine. Then the search engine will start processing the data.
The default search relevance weight is 1.0. If no custom value is specified for this multiplier, then the original search engine relevance is used.
Relevance Weight Customization¶
To customize search relevance weight, add an event listener to fill the relevance_weight field using the oro_website_search.event.index_entity event (or a similar event for a specific entity, such as oro_website_search.event.index_entity.product) and an associated Oro\Bundle\WebsiteSearchBundle\Event\IndexEntityEvent event class.
First, create an event listener class:
1 <?php
2
3 namespace Acme\Bundle\TestBundle\EventListener;
4
5 use Oro\Bundle\ProductBundle\Entity\Product;
6 use Oro\Bundle\SearchBundle\Engine\IndexerInterface;
7 use Oro\Bundle\WebsiteSearchBundle\Event\IndexEntityEvent;
8
9 class SetWebsiteSearchRelevanceWeightListener
10 {
11 /**
12 * @param IndexEntityEvent $event
13 */
14 public function onIndexEntity(IndexEntityEvent $event)
15 {
16 /** @var Product $product */
17 foreach ($event->getEntities() as $product) {
18 $relevanceWeight = $this->calculateRelevanceWeight($product);
19 $event->addField($product->getId(), IndexerInterface::WEIGHT_FIELD, $relevanceWeight);
20 }
21 }
22
23 /**
24 * @param Product $product
25 * @return float
26 */
27 private function calculateRelevanceWeight(Product $product)
28 {
29 switch ($product->getInventoryStatus()->getId()) {
30 case Product::INVENTORY_STATUS_IN_STOCK:
31 return 1.0;
32 case Product::INVENTORY_STATUS_OUT_OF_STOCK:
33 return 0.5;
34 default:
35 return 0.3;
36 }
37 }
38 }
Then, register this event listener in the DI container:
1 acme_test.event_listener.website_search.set_website_search_relevance_weight:
2 class: Acme\Bundle\TestBundle\EventListener\SetWebsiteSearchRelevanceWeightListener
3 tags:
4 - { name: kernel.event_listener, event: oro_website_search.event.index_entity.product, method: onIndexEntity }
Specify a mapping for the added relevance_weight field in the Resources/config/oro/website_search.yml file if it was not specified before:
1 Oro\Bundle\ProductBundle\Entity\Product:
2 fields:
3 -
4 name: relevance_weight
5 type: decimal
Finally, clear the cache using the bin/console cache:clear --env=prod
command and trigger reindexation of the required entity using the bin/console oro:website-search:reindex --class=OroProductBundle:Product --env=prod
command.