There are scenarios where a users search query can contain helpful information like gender, colour or brand that you want to use to further refine their search results.

By default if a user searches for mens t-shirts any products that contain both words will be considered highly relevant and will be given top ranking positions in the search result. However products just containing the word t-shirt are still relevant which means women's or children's t-shirts may also appear in the search result at a lower rank.

Another example is a search for white leather shoes, where lower ranking brown leather shoes, white leather jackets or white handbags may return in the result.

Adding a minimum index score threshold (see https://docs.search.io/documentation/fundamentals/search-settings/textual-relevance#minimum-index-score) can help here by ensuring only products above a certain relevance score are returned in a result set. However in circumstances like this you may want greater control and the ability to boost or filter products by their gender, type (shoes) or colour.

This guide describes how to implement a gender based query filter and/or boost.

Requirements:

For the filter or boost to work all of the products you want to display must be categorised so a filter or boost can be reliably applied. In the example below all of the products in the customers index have a field called gender which can have a value of womens, women, men or mens.

You will need to change the field names and values in the example below to suit your use case and data structure.

Step 1 - Extract the gender from the users search query

Login to the platform and open the Relevance screen then click </> Advanced which will open a pipeline code editor. Create a new line beneath where the pipeline editor says preSteps: and then copy and paste the code below.

- id: string-regexp-extract
  description: extract gender from user search query
  params:
    match:
      bind: genderExtract
    matchTemplate:
      constant: gender = ${genderValue} 
    pattern:
      constant: (?P<genderValue>(womens|women|men|mens)
    text:
      bind: q
CODE

This code checks to see if the users search query contains womens, women, men or mens. In our example the users search query is mens t-shirts so the code would match the word mens and assign the value to variable called genderValue. Its last action is to construct the string gender = 'mens' which it assigns to another variable called genderExtract.

Step 2a - Create a filter to filter on the gender field

If you want to perform a filter we need to pass genderExtract to an add-filter step. The code below can be cut and paste beneath the extract code from step 1. It has a condition to only execute if genderExtract is not empty.

- id: add-filter
  description: filter on gender
  params:
    filter:
      bind: genderExtract
  condition: genderExtract != ''
CODE

Now when a user searches for mens t-shirts only t-shirts that have the gender field set to mens will appear in the search result.

Step 2b - Create a boost to boost the gender field

If you want to perform a boost instead we need to pass genderExtract to a filter-boost step. The code below can be cut and pasted beneath the extract code from step 1. It has a condition to only execute if genderExtract is not empty.

- id: filter-boost
  description: boost on gender
  params:
    filter:
      bind: genderExtract
    score:
      constant: "0.35"
  condition: genderExtract != ''
CODE

Now when a user searches for mens t-shirts all t-shirts that have the gender field set to mens will receive a boost to their overall score. Use the Preview mode within the Relevance screen to test a few queries and determine which weight (currently 0.35) works best for you.

Other applications:

There is the potential to use this solution for multiple field types. Our example white leather shoes could have a filter or boost constructed for color=white, material=leather, type=shoes

The solution could also be used to filter or boost products by their SKU if a user were to type sku:A-2857489 or sku=A-2857489