Table of contents :

Filtering by Price, Brand and Category in Weaviate

100-days-of-weaviate-032

Table of contents :

WooCommerce product search often needs more than semantic relevance. A shopper may want running shoes under $150, from a particular brand, within a specific category. In Weaviate, these requirements are handled with property filters that can be combined with vector or hybrid search.

Model filterable WooCommerce properties

Filtering works best when price, brand, and category are stored as structured properties during product ingestion. A practical product object might include the following fields:

{
  "product_id": "wc_4821",
  "name": "Trail Runner Pro",
  "description": "Lightweight trail shoes with reinforced grip",
  "price": 129.99,
  "brand": "Acme Running",
  "categories": ["Shoes", "Running", "Trail"],
  "stock_status": "instock"
}

Store price as a numeric property rather than text. Store categories as an array when a product can belong to multiple WooCommerce categories. Normalize brand and category values before indexing so that values such as Acme Running, acme-running, and ACME RUNNING do not create inconsistent filter behavior.

Filtering by price

Price filters are useful for both hard budget limits and price ranges. The following Python example returns products priced below 150 while using semantic search to interpret the shopper’s query:

from weaviate.classes.query import Filter

products = client.collections.use("Product")

response = products.query.near_text(
    query="lightweight trail running shoes",
    filters=Filter.by_property("price").less_than(150),
    limit=12,
    return_properties=["product_id", "name", "price", "brand", "categories"]
)

A range can be expressed by combining two numeric conditions:

price_filter = (
    Filter.by_property("price").greater_or_equal(80)
    & Filter.by_property("price").less_or_equal(150)
)

response = products.query.near_text(
    query="waterproof running shoes",
    filters=price_filter,
    limit=12
)

Decide whether the indexed value represents the current selling price, regular price, or a calculated promotion price. For most storefront search experiences, indexing the effective selling price produces the least surprising result. If agencies need both sale and regular-price filtering, store both values explicitly.

Filtering by brand

Brand filtering is generally an equality condition. This is appropriate when the application has already resolved a shopper’s selected brand to a canonical value.

brand_filter = Filter.by_property("brand").equal("Acme Running")

response = products.query.hybrid(
    query="trail shoes with strong grip",
    filters=brand_filter,
    alpha=0.65,
    limit=12
)

Use a controlled list of brand values in the WooCommerce integration rather than passing arbitrary display text from the browser. A brand facet can return the selected term’s internal identifier, and the application can map that identifier to the normalized value stored in Weaviate.

If brand aliases must be supported, resolve them before querying or index an additional array such as brand_aliases. For example, a product from Acme Running could also contain Acme in that array. This keeps the primary brand field consistent while allowing controlled alternate names.

Filtering by category

WooCommerce products commonly have multiple categories, so categories are best represented as a text array. Use an array filter when a product should match any selected category:

category_filter = Filter.by_property("categories").contains_any(
    ["Running", "Trail"]
)

response = products.query.near_text(
    query="shoes for long-distance training",
    filters=category_filter,
    limit=12
)

Use contains_all when every category is required:

category_filter = Filter.by_property("categories").contains_all(
    ["Shoes", "Running"]
)

The choice between contains_any and contains_all should match the storefront behavior. A shopper selecting several categories often expects broader results, while an internal merchandising rule may require a product to belong to every specified category.

Combining price, brand, and category

Weaviate filters can be composed with Boolean operators. The following query finds Acme Running products in selected categories priced between 80 and 150:

combined_filter = (
    Filter.by_property("price").greater_or_equal(80)
    & Filter.by_property("price").less_or_equal(150)
    & Filter.by_property("brand").equal("Acme Running")
    & Filter.by_property("categories").contains_any(["Running", "Trail"])
)

response = products.query.hybrid(
    query="comfortable shoes for daily training",
    filters=combined_filter,
    alpha=0.6,
    limit=24,
    return_properties=[
        "product_id", "name", "price", "brand", "categories"
    ]
)

The filters narrow the eligible product set before the query results are returned, while hybrid or vector search ranks the matching products by relevance. This gives agencies a reliable way to enforce business constraints without expecting the embedding model to understand exact price or taxonomy requirements.

Building filters from WooCommerce requests

A storefront request might contain a search phrase, minimum and maximum prices, a brand slug, and one or more category IDs. Validate and translate these values on the server before constructing a Weaviate filter. Do not concatenate user input into a query string.

conditions = []

if min_price is not None:
    conditions.append(Filter.by_property("price").greater_or_equal(float(min_price)))

if max_price is not None:
    conditions.append(Filter.by_property("price").less_or_equal(float(max_price)))

if brand_name:
    conditions.append(Filter.by_property("brand").equal(brand_name))

if category_names:
    conditions.append(
        Filter.by_property("categories").contains_any(category_names)
    )

combined_filter = None
for condition in conditions:
    combined_filter = condition if combined_filter is None else combined_filter & condition

query_arguments = {
    "query": search_text,
    "limit": 24
}
if combined_filter is not None:
    query_arguments["filters"] = combined_filter

response = products.query.hybrid(**query_arguments)

In production code, validate numeric bounds, cap the maximum result count, reject unknown brand and category values, and define how an empty range should behave. Also decide whether category selection should include child WooCommerce categories. If it should, expand the selected category to a precomputed list before applying contains_any.

Operational considerations for agencies

  • Keep property types stable: changing a price field from number to text requires a schema and reindexing strategy.
  • Index canonical values: use stable brand names or IDs and keep presentation labels in the application layer.
  • Handle currency explicitly: do not compare values from different currencies in one numeric field. Use one index per currency or normalize prices before ingestion.
  • Reindex on catalog changes: price, stock, brand, and category updates should be sent to Weaviate promptly so search results agree with the WooCommerce catalog.
  • Test empty results: a strict combination of filters can legitimately return no products. The API should provide a clear response rather than silently removing constraints.
  • Measure filter selectivity: highly selective filters can improve relevance, while broad category filters leave semantic ranking with more work.

For product search, structured filters should enforce facts and vector or hybrid retrieval should handle language and intent. Separating those responsibilities makes price ranges, brand selections, and WooCommerce category navigation predictable as the catalog grows.

Trending posts
You might also like