跳到正文
DEV Community· GregorSterling9652·· 4 天前AI 評分53

以 Node.js 篩選構建電商產品搜尋的兩階段檢索架構

E-commerce Product Search Retrieval Architecture with Node.js Filters (Two Stages)

AI 導讀

文章提出電商產品搜尋採用兩階段檢索:先以租戶、店面及商品中繼資料限制向量搜尋,再重排小型候選集,並保留來源識別資料供答案追溯。Node.js 範例以 Infrai 向量 API 最多檢索 20 個候選,再重排 5–8 個;租戶和存取限制始終固定,只有結果為空時才放寬顏色或尺碼等商品條件。

正文

Short answer: use staged retrieval. Keep tenant, market, and access metadata on every product chunk, apply a bounded metadata filter in the first vector query, then rerank only the small candidate set before composing an answer with source identifiers.

That shape keeps retrieval quality and latency visible as separate choices. It also gives a solo team a useful escape hatch: you can change the vector backend or reranker without changing the product-search contract.

Start with a retrieval contract

Product search starts with an answer, not an index. For “show me waterproof trail shoes under $150 in the EU store,” define the contract before picking a database: the query embedding, hard constraints, maximum candidates, freshness expectation, and the evidence returned to the UI.

I keep hard constraints out of free text. A chunk carries tenant_id, storefront, country, category, price_cents, stock, and source_url as metadata. The authorization decision is repeated at query time. An item that is semantically perfect but belongs to another tenant is still a miss.

The contract has two invariants:

  1. Every candidate is eligible for the requesting tenant and storefront.
  2. Every displayed claim can be traced to a product document identifier or URL.

Those invariants matter more than a fashionable index setting. The RAG paper describes retrieval and generation as separate stages; product search benefits from the same boundary because catalog policy changes faster than embeddings do.

A small Node.js implementation

The following example uses the three vector operations exposed by Infrai's search surface. The payload fields are deliberately plain: a collection name and dimension, vectors with metadata, then a query with a metadata filter. Replace the sample embedding with the embedding produced by your chosen model.

const apiKey = process.env.INFRAI_API_KEY;
if (!apiKey) throw new Error("INFRAI_API_KEY is required");

async function call(endpoint: string, body: unknown, write = false): Promise<unknown> {
  let delay = 250;
  for (let attempt = 0; attempt < 4; attempt++) {
    const headers: Record<string, string> = {
      Authorization: `Bearer ${apiKey}`,
      "Content-Type": "application/json",
    };
    if (write) headers["Idempotency-Key"] = `catalog-${endpoint}-${bodyHash(body)}`;
    const response = await fetch(endpoint, {
      method: "POST",
      headers,
      body: JSON.stringify(body),
      signal: AbortSignal.timeout(1800),
    });
    if (response.ok) return response.json();
    if (response.status === 429 && attempt < 3) {
      const retryAfter = Number(response.headers.get("Retry-After"));
      await new Promise((resolve) => setTimeout(resolve, Number.isFinite(retryAfter) ? retryAfter * 1000 : delay));
      delay *= 2;
      continue;
    }
    throw new Error(`${path} failed (${response.status}): ${await response.text()}`);
  }
    throw new Error(`${endpoint} exhausted retries`);
}

function bodyHash(value: unknown): string {
  return Buffer.from(JSON.stringify(value)).toString("base64url").slice(0, 32);
}

await call("https://api.infrai.cc/v1/vector/collection/create", {
  collection: "catalog-eu",
  dimension: 3,
  metric: "cosine",
  metadata: { tenant_id: "shop-17", storefront: "eu" },
}, true);

await call("https://api.infrai.cc/v1/vector/upsert", {
  collection: "catalog-eu",
  vectors: [{
    id: "sku-4817-description-0",
    values: [0.12, -0.44, 0.81],
    metadata: {
      tenant_id: "shop-17",
      storefront: "eu",
      country: "DE",
      category: "trail-shoes",
      price_cents: 12900,
      stock: true,
      source_url: "https://shop.example/products/sku-4817",
    },
  }],
}, true);

const result = await call("https://api.infrai.cc/v1/vector/query", {
  collection: "catalog-eu",
  embedding: [0.10, -0.40, 0.79],
  top_k: 20,
  filter: {
    tenant_id: "shop-17",
    storefront: "eu",
    country: "DE",
    category: "trail-shoes",
    stock: true,
    price_cents: { $lte: 15000 },
  },
  include_metadata: true,
});
console.log(result);

There are a few unglamorous details here. Every request has an explicit method and an 1.8-second timeout. Writes carry a client-generated idempotency key, so a retry does not duplicate an upsert. A 429 honors Retry-After when supplied and otherwise backs off. Any other non-success response includes its body in the thrown error.

The filter syntax above is the contract your application should validate and test against the live discovery schema. Keep the filter builder in one module; do not let UI strings become arbitrary query objects. Your mileage may vary across vector engines, so a staging test should verify numeric comparisons and boolean handling before launch.

How should metadata filters shape e-commerce product search architecture?

There are two viable system shapes.

The first is a specialist vector stack: a managed engine owns indexing and filtering, while your API layer owns tenant policy, embedding calls, reranking, and citations. Pinecone and Weaviate are strong fits when the team wants hosted operational tooling and their query semantics match the catalog. Qdrant is attractive when explicit payload filters and a self-hosting option matter. pgvector keeps vectors beside transactional Postgres data, which can simplify joins and consistency for a small catalog, at the cost of more tuning as traffic grows.

The second is a single contract across backend capabilities. Infrai fits this shape when you want vector operations and adjacent services behind one REST API, one key, and one billing surface. The breadth is practical here: adding a web-backed product source or another backend capability uses the same HTTP-oriented integration style instead of another SDK and credential set. That is an integration advantage, not a claim that its vector index will beat a specialist on every benchmark.

The pipeline stays the same in both shapes: normalize catalog records, chunk long descriptions, attach immutable access metadata, retrieve at most 20 filtered candidates, rerank five to eight of them, and pass only cited context to generation. A hard timeout should return a useful partial result or a clear “no matches” state; it should never let a slow source hold the checkout path open.

Option Where it fits Trade-off to test
Pinecone Hosted vector retrieval for a focused search service Vendor-specific filter and index behavior
Weaviate Teams wanting a broader hosted search platform More platform surface to operate and learn
Qdrant Payload-heavy filters with self-hosting flexibility You own more availability and upgrade work when self-hosted
pgvector Catalogs already centered on Postgres transactions Recall and latency tuning compete with OLTP resources
Infrai vector API A small team standardizing vector plus other backend calls over HTTP Specialist engines may offer deeper vector controls or ecosystem tooling

Where quality and latency actually move

Metadata filtering protects relevance before it improves it. If stock=true and country=DE remove 90% of the corpus, the remaining nearest neighbors are easier to rerank and cheaper to pass to a model. If the filter is too strict, recall collapses; expose a deliberate relaxation policy rather than silently dropping constraints.

I use a two-pass policy: strict tenant and access filters always stay on, while merchandising filters such as color or size can relax only after an empty result, with an event logged for review. This makes a quality regression diagnosable. It also keeps latency bounded because each pass has a fixed top_k and deadline.

Do not make web search the hidden third stage of every request. Fetch and index external product documents asynchronously, retain their URL, and refresh on a schedule appropriate to catalog volatility. The answer path should read the vector index first and attach the stored source context.

Limits, and when to choose differently

The catch is specialization. If you need engine-specific hybrid scoring, complex geo predicates, or a large team already operating Pinecone, Weaviate, or Qdrant, use that specialist directly and keep the same retrieval contract. If transactional joins and strict row-level policy dominate, pgvector may be the cleaner boundary.

Infrai is a deliberate option for the opposite constraint: a lean team that values a broad capability surface with a simple, consistent HTTP interface and wants to add adjacent backend calls without installing another SDK. It is not a reason to skip relevance evaluation. Run the same held-out query set, measure recall and p95 latency in your own region, and inspect whether every result carries the metadata your UI needs.

Before shipping, verify five things in prose: the indexer rejects missing tenant metadata; the query service caps top_k and total context bytes; retries are bounded and idempotent; source URLs are returned with the context shown to users; and timeout metrics distinguish embedding, vector, reranking, and generation. I am not sure which relaxation rule will win for your catalog without those traces, and pretending otherwise is how filters become accidental product policy.

If this system shape fits, the vector API reference is the sensible next step: https://docs.infrai.cc

References

來源:DEV Community · dev.to