Filtering facets

You can filter at the facet level, that is, filter the values within a facet. To do this, add a search.facet.types.filter object. You can optionally add a widget.items.search.facet.types.filtering_options object.

For the data model of these objects, see the Search and Recommendation API reference.

Filtering a term facet

In this example, you want to filter a facet called product. You want to:

  • Get items from either of two facet values, facet value A or facet value B. For this, use the filter object and pass or as the type and pass the IDs of the two facet values as the values.

    Note

    To get the facet ID values, make an unfiltered request to the endpoint. The response contains the facet IDs. For a sample call, see Get all available facets

  • Apply hard filters, that is, previously-decided filters that apply to the request as a whole. For this, pass hard_filters as a value for the filtering_options object.

  • Apply filters to the current facet, the object facet. This means that when a visitor selects, say, facet value A, facet value B disappears. For this, pass own_values as a value for the filtering_options object.

  • Apply filters from other facets to the current facet. This means that when a visitor selects facet values in other facets, the facet values in the current facet change dynamically based on whether the filter still applies to them. For this, pass other_facet_values as a value for the filtering_options object.

The following code sample shows how to create a request:

RequestResponse
{
    "widget": {
        "items": [
            {
                "rfk_id": "rfkid_7",
                "entity": "product",
                "search": {
                    "facet": {
                        "types": [
                            {
                                "name": "product",
                                "max": 5,
                                "filtering_options": [
                                    "own_values",
                                    "hard_filters",
                                    "other_facet_values"
                                ],
                                "filter": {
                                    "type": "or",
                                    "values": [
                                        "facetid_A",
                                        "facetid_A"
                                    ]
                                }
                            }
                        ]
                    }
                }
            }
        ]
    },
    "context": {
        "locale": {
            "country": "us",
            "language": "en"
        }
    }
}

Filtering a dynamic attributes facet

Filtering a dynamic attributes facet is similar to filtering a term facet or a histogram facet. The main difference is that when you work with a dynamic attributes facet, the facet.types.name is the name of the dynamic attribute.

In this example, you want to filter the attribute_tags.ID01 dynamic attributes facet. You want items from either of two facet values, facet value A or facet value B. For this, use the widget.item.search.facet.types.filter object and pass or as the type and pass the IDs of the two facet values as the values.

Note

To get facet ID values, make an unfiltered request to the endpoint. You get facet IDs in the response. For a sample call, see Get the top facets from a dynamic attributes collection

The following code sample shows how to create a request:

RequestResponse
{
    "widget": {
        "items": [
            {
                "rfk_id": "rfkid_7",
                "entity": "product",
                "search": {
                    "facet": {
                        "types": [
                            {
                                "name": "attribute_tags.ID9",
                                "filter": {
                                    "type": "or",
                                    "values": [
                                        "facetid_A",
                                        "facetid_B"
                                    ]
                                }
                            }
                        ]
                    }
                }
            }
        ]
    },
    "context": {
        "locale": {
            "country": "us",
            "language": "en"
        }
    }
}

Using complex facet filters

To create a complex facet filter with nested objects, use more than one operator within the facet.types.filter object.

Using complex filters with a range facet

A common use case for complex facet filters is when you have a double slider on the GUI for facets with a range of values like Review rating , Price, or Newness. In these cases, you have to be able to display things that are both less than an amount and greater than an amount.

For example, you have a Newness facet that helps users narrow down results based on when an item was added. The facet values range from 0 to 30, meaning that content items were added from zero days ago to 30 days ago. Assume a visitor wants to see items added between 10 and 20 days ago and moves the slider accordingly.

The following sample request shows you how to handle Newness filtered by values between 10 (inclusive) and 20:

RequestResponse
{
  "widget": {
    "items": [
      {
        "rfk_id": "rfkid_7",
        "entity": "content",
        "search": {
          "facet": {
            "types": [
              {
                "name": "newness",
                "filter": {
                  "type": "and",
                  "values": [
                    {
                      "type": "gte",
                      "value": 5
                    },
                    {
                      "type": "lt",
                      "value": 10
                    }
                  ]
                }
              }
            ]
          }
        }
      }
    ]
  },
  "context": {
    "locale": {
      "country": "us",
      "language": "en"
    }
  }
}

Using complex filters with a term facet

You can also use complex filters with term facets.

For example, you have a bookstore website and a Genre facet. This facet is based on a string attribute. Each content item (that is, each book) can have multiple genres. For example, a book might have the genre values of Young adult, Fantasy, Mystery, Dystopian, and Fiction. A visitor wants to see books that are either Mystery or Fiction, but they do not want to see Young adult books.

The following sample request shows you how to filter for items that are mystery or fiction , but not young adult:

RequestResponse
{
  "widget": {
    "items": [
      {
        "rfk_id": "rfkid_7",
        "entity": "product",
        "search": {
          "facet": {
            "types": [
              {
                "name": "genre",
                "filter": {
                  "type": "and",
                  "values": [
                    {
                      "type": "not",
                      "filter": {
                        "type": "eq",
                        "value": "Young adult"
                      }
                    },
                    {
                      "type": "or",
                      "filters": [
                        {
                          "type": "eq",
                          "value": "Fiction"
                        },
                        {
                          "type": "eq",
                          "value": "Mystery"
                        }
                      ]
                    }
                  ]
                }
              }
            ]
          }
        }
      }
    ]
  },
  "context": {
    "locale": {
      "country": "us",
      "language": "en"
    }
  }
}

Do you have some feedback for us?

If you have suggestions for improving this article,