Using search filters

Filters are specific selections from available facets that reduce the number of results returned. You must configure filters in the Customer Engagement Console (CEC) before using them in the Sitecore Discover Search and Recommendation API. Filters ensure that item attributes exactly match the specified values. Unlike queries, filters do not consider other attributes.

Consider the following two products:

RequestResponse
Product 1: {"name": "red flag shirt", "color": "white"}
Product 2: {"name": "shirt", "color": "red"}

Querying with the keyphrase red gives you both products because the query looks at product fields. Filtering with the color red gives you only Product 2 because the filter only looks at the color field.

Filters for different facet types

By default, filters for different facet types use the AND operation.

For example, if you apply filters for stone types and stone colors, the response uses an AND operation. In the following image and code snippet, you select two filters to get both Aquamarine stone types AND Blue stone colors.

Image showing aquamarine applied for stone types filter and blue applied for stone colors filter

Filter 1:

RequestResponse
{"stone_types": "Aquamarine"}

Filter 2:

RequestResponse
{"stone_colors": "Blue"}

Filters within the same facet type

By default, filters within the same facet type use the OR operation.

For example, if you apply filters for the same facet type such as stone colors, the response uses an OR operation. In the following image and code snippet, you select two filters to get either Blue OR Clear stone colors.

Image showing two different filters, blue and clear, applied for Stone Colors

Filter 1 and 2:

RequestResponse
{"stone_colors": ["Blue", "Clear"]}

Applying a term filter

The following shows code shows a request for a term filter. In this example, you use the keyphrase wedding and apply a filter for primary_stone_type.

RequestResponse
{
  "query": {
    "keyphrase": ["wedding"]
  },
  "filter": {
    "primary_stone_type": {
      "value": ["stone_id_diamond"]
    }
  },
  "content": {}
}

The following code shows the corresponding response:

RequestResponse
{
  "ts": 1480977544,
  "rid": "response_id_ff1",
  "url": "/searchpage?keyphrase=wedding&primary_stone_type=stone_id_diamond",
  "query2id": {
    "keyphrase": "keyphrase_id_wedding"
  },
  "filter": {
    "primary_stone_type": {
      "value": ["stone_id_diamond"]
    }
  },
  "content": {
    "product": {
      "value": [
        {"name": "wedding ring 1", "price": 2500, "image_url": "",
         "rating": 5, "primary_stone_type": "diamond"},
        {"name": "wedding ring 2", "price": 1000, "rating": 1,
         "primary_stone_type": "diamond"},
        {"name": "wedding jewelry 3", "price": 5000,
         "primary_stone_type": "diamond"},
        
      ]
    }
  },
  "n_item": 10,
  "total_item": 150,
  "page_number": 1,
  "total_page": 15
}

Applying a range filter

The following shows code shows a request for a range filter. In this example, you use the keyphrase wedding and apply a price range filter between 1000 and 2500.

RequestResponse
{
  "query": {
    "keyphrase": ["wedding"]
  },
  "filter": {
    "price": {
      "value": [{"min": 1000, "max": 2500}]
    }
  },
  "content": {}
}

The following code shows the corresponding response:

RequestResponse
{
  "ts": 1480977544,
  "rid": "response_id_ff2",
  "url": "/searchpage?keyphrase=wedding&price-min=1000&price-max=2500",
  "query2id": {
    "keyphrase": "keyphrase_id_wedding"
  },
  "filter": {
    "price": {
      "value": [{"min": 1000, "max": 2500}]
    }
  },
  "content": {
    "product": {
      "value": [
        {"name": "wedding ring 1", "price": 2500, "image_url": ""},
        {"name": "wedding ring 2", "price": 1000, "rating": 1},
        {"name": "wedding jewelry 4", "price": 1999},
        
      ]
    }
  },
  "n_item": 10,
  "total_item": 150,
  "page_number": 1,
  "total_page": 15
}

Applying a dynamic attribute filter

The following shows code shows a request for a dynamic attribute. In this example, you apply a Pants filter for a dynamic attribute for red products.

The dyn_attrs in the request refers to a dynamic attribute enabled for filtering.

RequestResponse
{
  "query": {
    "keyphrase": ["red"]
  },
  "filter": {
    "dyn_attrs.clothing_type": {
      "value": ["Pants"]
    }
  },
  "content": {}
}

The following code shows the corresponding response:

RequestResponse
{
  "ts": 1480977544,
  "rid": "response_id_37",
  "query2id": {
    "keyphrase": "keyphrase_id_red"
  },
  "content": {
    "product": {
      "value": [
        {"name": "Pants 1", "price": 25, "color": "red", "image_url": "", "dyn_attrs": {"clothing_type": "Pants", ...}},
        {"name": "New Arrival Pants 2", "color": "red", "price": 100, "rating": 1, "dyn_attrs": {"clothing_type": "Pants", ...}},
        {"name": "Red Pants 4", "price": 19, "dyn_attrs": {"clothing_type": "Pants", ...}},
        
      ]
    }

  },
  "n_item": 10,
  "total_item": 297,
  "page_number": 1,
  "total_page": 30
}

Do you have some feedback for us?

If you have suggestions for improving this article,