Hi Typesense community, Title: How to implement di...
# community-help
a
Hi Typesense community, Title: How to implement distance slabs (≤10 km, >10 km, not found) and choose cheapest supplier plan in Typesense? Context / Use case We run a B2B auto spare parts marketplace. A customer requests multiple parts (`rootPartNumber`s). We want to return availability stats and also suggest a supplier plan that minimizes last-mile delivery (LMD) cost. Key needs: • Slabs: ◦ _Slab A_: within 10 km ◦ _Slab B_: outside 10 km (e.g., >10 km) ◦ _Slab C_: not foundEdge case: Sometimes there are 4 suppliers within 10 km but in opposite directions, while 1 supplier at 15 km can fulfill everything. Operationally, the single farther supplier may be cheaper/faster than multiple pickups. So we need to evaluate plans, not just distance. Sample document
Copy code
{
  "familyQuantity": 2,
  "id": "680cde5de2443bb2a1bf70c1::990J0M53M00-010",
  "location": [27.596642, 75.183688],   // geopoint
  "rootPartNumber": "990J0M53M00-010",
  "supplierId": "680cde5de2443bb2a1bf70c1",
  "supplierInfo": "supplierName::680cde5de2443bb2a1bf70c1::27.596642,75.183688"
}
Schema (relevant fields)
Copy code
{
  "name": "supplierinventories_rpn",
  "fields": [
    {
      "name": "rootPartNumber",
      "type": "string",
      "facet": true
    },
    {
      "name": "familyQuantity",
      "type": "int32"
    },
    {
      "name": "location",
      "type": "geopoint"
    },
    {
      "name": "supplierId",
      "type": "string",
      "facet": true
    },
    {
      "name": "supplierInfo",
      "type": "string",
      "facet": true
    }
  ]
}
Current query (De-URL-encoded for readability; API key redacted)
Copy code
curl --location --globoff \
'https://<typesense-host>/collections/supplierinventories_rpn/documents/search
  ?q=*
  &query_by=rootPartNumber
  &facet_by=supplierInfo
  &filter_by=(
      (rootPartNumber:=[69510M68P00] && familyQuantity:>=2) ||
      (rootPartNumber:=[69100M75T40] && familyQuantity:>=1)
    )
    && location:(27.1598238,75.7364393,100 km)
  &max_facet_values=1000
  &per_page=1000' \
-H 'x-typesense-api-key: <REDACTED>'
What I’m trying to achieve 1. Distance slabs for each requested part: ◦
≤ 10 km
(Slab A) ◦
> 10 km
(Slab B) ◦ not found (Slab C) 2. Ability to compute/compare supplier plans: ◦ Plan A: multiple suppliers within 10 km (possibly opposite directions) ◦ Plan B: one supplier outside 10 km who covers all parts ◦ Choose the lowest LMD cost plan (e.g., cost model using base fee + km + multi-stop penalty). Questions for the community 1. Outside radius filter: Is there a built-in way in
filter_by
to express “outside 10 km” (NOT within a circle)? Or is the recommended approach to query within MAX radius and then bin client-side using the distance value returned from
sort_by=location(lat,lng):asc
? 2. Ring query (R1–R2): Can I express a ring (e.g., between 10 km and 100 km) natively, or should I do two queries (≤100 km) minus (≤10 km)? 3. Per-hit distance: Is using
&sort_by=location(lat,lng):asc
the canonical way to get a per-hit distance in the response for binning (Slab A vs Slab B)? Any SDK-specific tips here? 4. Faceting + geo: Any best practices when combining
facet_by
(e.g.,
supplierId
or
supplierInfo
) with a geo filter for large result sets (performance, limits, pagination)? 5. Alternative geo shapes: Would using polygon filters be preferable for some cities/areas versus a simple radius (e.g., matching municipal boundaries)? And still, there’s no “NOT polygon” support, correct? 6. Sorting helpers: I noticed
exclude_radius
in geo sort helps prioritize outside-radius hits, but it doesn’t filter them out. Is that the intended use (ranking only), and filtering still needs to be done via separate queries / client logic? What we’ll do with the results • Produce an API response with counts by slab, the “not found” list, and a recommended plan (either multi-supplier ≤10 km or single farther supplier) based on our cost model. • For now, we’ll keep the cost optimization on our server; Typesense handles the filtering/sorting. Any guidance, examples, or gotchas would be super helpful. Thanks! cc @Sahil Rally @Atishay Jain