Table of contents :

Combining Keyword Search and RAG

100-days-of-rag-for-woocommerce-028

Table of contents :

WooCommerce search and retrieval-augmented generation solve different parts of the same problem. Keyword search is precise, fast, and easy to constrain with product attributes. RAG is better at interpreting natural-language questions and supplying supporting context to an answer. Combining them produces a retrieval layer that can handle both exact shopping requests and conversational product research.

Why WooCommerce stores need both retrieval methods

A shopper may search for an exact SKU, such as TSHIRT-BLUE-M, or enter a descriptive request such as “a breathable blue shirt for summer under $40.” These queries should not be handled identically.

  • Keyword search performs well for SKUs, product names, brand names, model numbers, taxonomies, and exact attribute values.
  • Semantic retrieval performs well when the shopper uses synonyms, incomplete descriptions, use cases, or natural-language questions.
  • RAG generation can explain product differences, summarize specifications, and answer questions using retrieved catalog and policy content.

A keyword-only system may miss relevant products when the query uses unfamiliar wording. A vector-only system may return an approximate match for an exact SKU or ignore important constraints such as size, stock status, and price. A hybrid design uses each method where it is strongest.

A practical hybrid retrieval architecture

A WooCommerce implementation generally has five stages:

  1. Query analysis: identify the shopper’s intent, product terms, filters, and whether the query contains an exact identifier.
  2. Parallel retrieval: run a keyword query and a vector similarity query against the product index and supporting documents.
  3. Candidate merging: combine and deduplicate the result sets using a ranking strategy.
  4. Business filtering: apply authoritative constraints such as catalog visibility, stock status, permitted markets, price, and product availability.
  5. Context construction: provide the best product records and policy documents to the language model with clear source boundaries.

The product catalog should normally be indexed separately from longer documentation. Product records are transactional and change frequently. Shipping policies, sizing guides, care instructions, and buying guides are usually document-oriented and can use a different chunking and refresh schedule.

Indexing WooCommerce data for keyword and vector search

Each searchable product should have a structured representation for keyword search and a readable representation for embeddings. The structured fields support filters and exact matching, while the text representation captures meaning.

Product ID: 4821
SKU: TSHIRT-BLUE-M
Name: Organic Cotton Performance T-Shirt
Brand: Northwind Apparel
Categories: T-Shirts, Running Clothing
Attributes: Color=Blue; Size=M; Material=Organic cotton; Fit=Regular
Price: 34.00 USD
Stock status: instock
Search text: Lightweight organic cotton performance t-shirt in blue with a regular fit. Designed for running and warm-weather training.

Do not rely on the embedding alone for values that must be exact. Keep SKU, price, stock status, product ID, variation ID, category, and normalized attributes in filterable fields. The embedding can represent descriptive text, but it should not be the source of truth for commerce constraints.

For variable products, index variations when shoppers need to select a specific size, color, or other option. At minimum, retain a parent-product reference so the application can present a valid purchasable variation rather than only the parent product.

Choosing the retrieval path from the query

Query classification does not have to be complicated. A small set of deterministic signals can improve results before any model call:

  • SKU-like patterns, numeric model identifiers, or quoted phrases should receive strong keyword weighting.
  • Recognized attribute values such as “blue,” “size medium,” or “waterproof” should become structured filters where possible.
  • Use-case language such as “for commuting,” “for sensitive skin,” or “for a small apartment” should be sent to semantic retrieval.
  • Requests asking about returns, delivery, warranties, or care should retrieve policy and help-center documents in addition to products.

For example, the query “SKU TSHIRT-BLUE-M” should prioritize an exact keyword match. The query “blue breathable running shirt under $40” can use keyword search for the category, color, and price constraints while using vector retrieval to find products described as lightweight, moisture-wicking, or suitable for warm-weather training.

Merging keyword and semantic results

There are several valid ways to combine result sets. The right choice depends on the search engine and the amount of ranking control available.

Weighted score blending

Normalize both scores to comparable ranges, then calculate a combined score. A common starting point is:

combined_score = (0.60 * keyword_score) + (0.40 * vector_score)

Increase the keyword weight for exact product lookup and attribute-heavy searches. Increase the vector weight for broad discovery queries. Scores from different engines often have different distributions, so normalize them before blending rather than adding raw values.

Reciprocal rank fusion

When score normalization is unreliable, reciprocal rank fusion combines positions instead of raw scores:

fusion_score = 1 / (k + keyword_rank) + 1 / (k + vector_rank)

A value such as k = 60 is a common starting point, but it should be evaluated against real store queries. Reciprocal rank fusion is useful when keyword and vector systems are operated by different services.

Exact-match overrides

Exact identifiers should usually bypass general blending. If a normalized SKU matches one active product or variation, return that result first and use RAG only to answer follow-up questions about it. This prevents a semantically similar product from outranking the item the shopper explicitly named.

Apply commerce filters before generation

Retrieval scores should not override business rules. Apply filters before passing candidates to the language model, and verify the final product data against WooCommerce or the store’s authoritative commerce service.

  • Exclude products that are hidden from catalog or search.
  • Filter by current stock status when the request implies immediate purchase.
  • Apply price bounds using numeric fields, not text generated by the model.
  • Respect customer location, currency, catalog visibility, and membership rules.
  • Use variation-level inventory when size or color availability matters.
  • Recheck price and availability immediately before adding an item to the cart.

For WooCommerce, indexed data can be refreshed through product and variation hooks, scheduled synchronization, or an external event pipeline. The retrieval service should treat the index as a fast search projection, not as the final authority for price, stock, tax, or cart operations.

Constructing the RAG context

After merging and filtering, create a compact context that distinguishes products from informational documents. Include stable identifiers and source metadata so the application can render links and audit answers.

<products>
  <product id='4821' variation_id='4827' sku='TSHIRT-BLUE-M'>
    <name>Organic Cotton Performance T-Shirt</name>
    <price currency='USD'>34.00</price>
    <attributes>Blue, Medium, Regular fit</attributes>
    <availability>In stock</availability>
    <url>https://store.example.com/product/performance-t-shirt/</url>
  </product>
</products>
<documents>
  <document source='size-guide'>...retrieved sizing guidance...</document>
</documents>

Tell the generation layer to use retrieved content for factual claims, distinguish unavailable information from an actual negative answer, and avoid inventing specifications. Product names, prices, stock, and links should be inserted from structured application data where possible rather than generated as free text.

Example request flow

For a request such as “Find a waterproof commuter backpack under $150 with a laptop sleeve,” the application can:

  1. Extract waterproof, commuter, laptop sleeve, and a maximum price of 150.
  2. Apply the numeric price filter and catalog visibility constraints in the search engine.
  3. Run keyword retrieval for backpack, waterproof, laptop, and indexed attributes.
  4. Run vector retrieval against product descriptions and buying-guide content.
  5. Merge the candidates, boost exact attribute matches, and remove duplicates.
  6. Pass the top products and relevant buying guidance to the model.
  7. Render each result with a product link, current price, and verified availability from application data.

Evaluation for agency projects

Measure hybrid retrieval with real WooCommerce queries rather than relying only on language-model response quality. Build a test set containing SKUs, misspellings, synonyms, attribute combinations, price limits, out-of-stock products, and policy questions.

Useful retrieval metrics include recall at a fixed number of results, precision for exact product queries, and the percentage of responses containing a product that satisfies every explicit constraint. Also track operational measures such as search latency, index freshness, token usage, and the rate of unsupported product claims.

Review failures by category. If exact SKUs are missed, improve tokenization and keyword fields. If descriptive requests return irrelevant products, improve product text, synonyms, attribute normalization, or vector retrieval. If the right products are retrieved but answers contain stale prices, move those values out of generated context and validate them at response time.

Trending posts
You might also like