Skip to content

TID Relevance Sorting

Introduction

The Relevance Sorting Plugin adds a new sorting option to your Shopware product listings: Sorting by Relevance.
This feature works like any other built-in sorting (e.g., Topseller, Lowest Price) but uses a configurable relevance score.

How It Works

The plugin calculates a relevance score for each product using a built-in algorithm.
As the administrator, you can influence how this score is calculated:

  • Adjust the weight of different score factors
  • Use Boosts to artificially increase the score for each Product individually.
  • Define custom rulesets using product custom fields.

Once configured, customers can use “Relevance” as a sorting method in any product listing.

Configuration Options

Weight Adjustments

This is the formula used to calculate the relevance score for each product:

plaintext
Score =
  (Recent Sales × 4)    +
  (Margin ÷ 10)         +
  Product Age           +
  All-Time Sales        +
  (Seasonal Sales × 4)  +
  Stock                 ± 
  Custom-Field Boost    ±
  On Sale Boost         ±
  Manual Boost

Using the number-inputs inside the plugin configuration under the adjustments tab, you can change the way the algorithm weighs the relevance score factors.

SettingDescription
Recent SalesWeight for products sold in the configured sales period.
Sales MarginWeight for the profit margin of sold products.
On Sale BoostBoost for products currently on sale.
Product AgeWeight for how long the product has been available.
Total SalesWeight for the total number of sales.
Season SalesWeight for products with high sales during the same season in previous years.
StockWeight for the amount of stock available.

Custom Boost Rules (YAML)

You can define complex boosting logic using the text-editor field in the plugin configuration written in YAML syntax.
This allows you to create custom rulesets based on product custom fields.

WARNING

In the Custom Boost Rules field, make sure you are in the Code Editor view when editing the custom rules at all times. You can switch to the Code Editor by clicking the </> icon in the top-right corner of the text field, which should then show a small line-number 1. This allows you to enter YAML without formatting issues.

After saving the configuration, the editor will exit the Code Editor view and display the YAML weirdly formatted, but it will still work correctly. If you wish to edit it, enable the Editor view again by clicking the </> icon.

The YAML should be structured so that each custom field contains a list of values that its packed with:

A custom field has the type single if there is only one value that can be entered into the Field.
A custom field has the type multi if there are multiple values that can be entered into the Field.

ElementDescription
field_typeDefines the type of custom field. Available types are: "single" or "multi"
operatorThe comparison operator used to evaluate the custom field value. Here, ">" meaning the custom field value must be greater than the defined comparison value as in: custom field value > comparison value. The operators =, !=, <, >, <=, >= can all be used.
comparison_valueThe threshold value to compare against. In this example, the product’s custom field value must be greater than the comparison valu to qualify for the boost.
boostThe value added to the product’s relevance score when the condition is met. Here, a boost of 5 is applied.

These elements are used to build a ruleset, a collection of self-defined rules for each custom field

Here's how the type single may be used in YAML:

yaml
custom_field_technical_name:
    field_type: "single"
    ruleset:
        example_rule:
            operator: ">"
            comparison_value: "10" 
            boost: 5
        example_rule2:
            operator: "="
            comparison_value: "42"
            boost: 2000

The example_rule adds a boost of 5 to products where the custom field value is greater than 10.
The example_rule2 adds a boost of 2000 to products where the custom field value is exactly 42.

For a multi type, the YAML structure is slightly different.

ElementDescription
match (none, any, all)"none" means the rule applies if none of the comparison values match the product’s custom field value(s). The other options are "any", which applies the rule if at least one value matches, and "all", which applies the rule only if every value matches.
comparison_value (multi)A list (array) of acceptable values for comparison. In this example: ["option_3", "option_4"].

WARNING

Ensure that even if there is only one comparison value, it is still formatted as a list (inside []) in the YAML.

Here's an example of how to define a multi type custom field in YAML:

yaml
custom_field_technical_name:
    field_type: "multi"
    ruleset:
        example_rule:
            match: "any"
            comparison_value: ["test_option_1", "test_option_2"]
            boost: 3
        example_rule2:
            match: "none"
            comparison_value: ["test_option_3"]
            boost: 5

The example_rule applies a boost of 3 to products where the custom field values match any of the specified options (test_option_3 or test_option_4).
The example_rule2 applies a boost of 5 to products where the comparison values are not in the custom field.

This is how a complete YAML configuration might look like, combining both single and multi types:

yaml
rating_custom_field:
    field_type: "single"
    ruleset:
        example_rule:
            operator: ">"
            comparison_value: "3.89" 
            boost: "5"

custom_product_material_composition:
    field_type: "single"
    ruleset:
        example_rule:
            operator: "="
            comparison_value: "100% Cotton"
            boost: "12"
    
tids_product_customfields_testing:
    field_type: "multi"
    ruleset:
        example_rule:
            match: "none"
            comparison_value: ["test_option_3", "test_option_4"]
            boost: "3"

Use Case

Imagine you want to:

  1. Boost products that have been sold recently.
  2. Increase the score for items currently on sale.
  3. Apply a custom rule that boosts products with a specific custom field value.

You would:

  • Set a high weight for Recent Sales and On Sale Boost in the plugin configuration
  • Define a custom rule in YAML to boost products with that specific custom field

When customers choose “Sort by Relevance,” the products matching your criteria will appear higher in the listing.