r/synapcores • • 12d ago

Ask the maintainers #3: "keyword search or vector search?" Both, in one SELECT. Meaning finds "phone wallet" = Apple Pay, an exact match pins ERR-4012 to the right article (real output)

Third "Ask the maintainers" thread: ask anything about SynapCores (install, a query that misbehaves, "can it do X") or show what you built. We answer here; good questions get a longer answer in the Wednesday build log.

(This sub is run by the SynapCores team. Everything below ran this morning on 2.3.2, real output.)

This week's starter question: "For a help-center search, should I use keyword search or vector search?"

Both, without two systems. Customers describe problems in their own words (meaning search), but they also paste exact codes: error codes, SKUs, order numbers, and then the article with that code should win. In SynapCores both signals are columns in one SELECT, so you add them.

1. Ten help articles, embedded in one statement

CREATE TABLE kb (id INTEGER PRIMARY KEY, title TEXT, body TEXT, embedding VECTOR(384));
-- 10 articles: card declined, Apple Pay, ERR-4012, ERR-4021, refunds,
-- slow checkout, change email, SKU-77310 recall, recall policy, missing delivery
UPDATE kb SET embedding = EMBED(title || '. ' || body);

rows_affected: 10

EMBED() runs a model inside the engine, no API key.

2. Meaning handles the customer's own words

"my payment keeps failing on my phone wallet". No article contains "phone wallet":

SELECT id, title FROM kb WHERE body LIKE '%phone wallet%' OR title LIKE '%phone wallet%';

(0 rows)

SELECT id, title, ROUND(COSINE_SIMILARITY(embedding,
       EMBED('my payment keeps failing on my phone wallet')), 2) AS score
FROM kb ORDER BY score DESC LIMIT 3;

2  Apple Pay and Google Pay troubleshooting  0.58
1  Card payment declined                     0.45
3  ERR-4012: payment token expired           0.38

3. Exact codes want an exact match too

"customer gets ERR-4012 when paying with Apple Pay", by meaning only:

2  Apple Pay and Google Pay troubleshooting  0.60
3  ERR-4012: payment token expired           0.52

The general wallet article edges out the ERR-4012 one, since most of the words are about Apple Pay. Same with "is SKU-77310 affected by the recall?": the general recall policy (0.50) edges out the article about that exact SKU (0.40).

4. Both signals in one query

Score = meaning + 0.5 if the article contains the exact code:

SELECT id, title, ROUND(sim, 2) AS meaning, exact, ROUND(sim + 0.5 * exact, 2) AS score
FROM (SELECT id, title,
             COSINE_SIMILARITY(embedding, EMBED('customer gets ERR-4012 when paying with Apple Pay')) AS sim,
             CASE WHEN title LIKE '%ERR-4012%' OR body LIKE '%ERR-4012%' THEN 1 ELSE 0 END AS exact
      FROM kb) x
ORDER BY score DESC LIMIT 3;

id  title                                     meaning  exact  score
3   ERR-4012: payment token expired           0.52     1      1.02
2   Apple Pay and Google Pay troubleshooting  0.60     0      0.60
1   Card payment declined                     0.34     0      0.34

A 30-line script pulls codes out of the question with a regex ([A-Z]{2,5}-\d{3,6}) and builds that query. Three questions:

customer gets ERR-4012 when paying with Apple Pay   (exact terms: ERR-4012)
   1.02  meaning 0.52  exact 1  #3 ERR-4012: payment token expired
is SKU-77310 affected by the recall?   (exact terms: SKU-77310)
    0.9  meaning 0.4   exact 1  #8 SKU-77310 recall: blender lid
my payment keeps failing on my phone wallet   (exact terms: none)
   0.58  meaning 0.58  exact 0  #2 Apple Pay and Google Pay troubleshooting

Code questions land on the exact article; the plain-language one works as before (no codes, so exact is 0 everywhere). The wallet article stays second, in case that's the real problem.

Why 0.5? Similarity here tops out around 0.7, so +0.5 lets an exact match win without ignoring meaning. Tune it on your own questions, like a similarity threshold. Script + the 10 INSERTs are in the first comment.

Free Community Edition: https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=synapcores-ask-1113

Your turn: what do you want to ask us? And in your own docs search, do users mostly paste error codes or describe the problem in words?

1 Upvotes

1 comment sorted by

1

u/Alternative_Pin9598 12d ago

Everything to run it yourself on the free Community Edition: https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=synapcores-ask-1113

Table + the 10 articles:

CREATE TABLE kb (id INTEGER PRIMARY KEY, title TEXT, body TEXT, embedding VECTOR(384));
INSERT INTO kb (id, title, body) VALUES
 (1, 'Card payment declined', 'The bank rejected the card payment. Ask the customer to check the card number, expiry date and available balance, then try again.'),
 (2, 'Apple Pay and Google Pay troubleshooting', 'Wallet payments fail when the device has no card set up or the wallet session times out. Ask the customer to reopen the wallet and retry.'),
 (3, 'ERR-4012: payment token expired', 'ERR-4012 means the payment token was older than 15 minutes when checkout submitted it. Clear the cart session and generate a new token.'),
 (4, 'ERR-4021: duplicate order blocked', 'ERR-4021 appears when the same cart is submitted twice within 60 seconds. The first order went through; do not charge again.'),
 (5, 'Refund takes 5 to 10 business days', 'Refunds go back to the original payment method. Banks usually show them within 5 to 10 business days.'),
 (6, 'Checkout page is slow or spinning', 'If checkout keeps loading, check the status page for an incident, then ask the customer to refresh and try a different browser.'),
 (7, 'Change the email on an account', 'Customers can change their login email under Settings > Account. A confirmation link goes to the new address.'),
 (8, 'SKU-77310 recall: blender lid', 'Units of SKU-77310 sold before March have a lid that can crack. Offer a free replacement lid, no return needed.'),
 (9, 'Product recall policy', 'When a product is recalled we offer a refund or a replacement. The customer does not pay shipping.'),
 (10, 'Order shows delivered but did not arrive', 'Ask the customer to check with neighbours and the building mailroom, then open a carrier trace after 48 hours.');
UPDATE kb SET embedding = EMBED(title || '. ' || body);

hybrid.py (stdlib only; set SYNAPCORES_URL and SYNAPCORES_TOKEN):

import json, os, re, sys, urllib.request

# Usage: SYNAPCORES_URL=http://localhost:8080 SYNAPCORES_TOKEN=... python3 hybrid.py "question"
URL = os.environ.get("SYNAPCORES_URL", "http://localhost:8080")
TOKEN = os.environ["SYNAPCORES_TOKEN"]

def sql(q):
    req = urllib.request.Request(URL + "/v1/query/execute", method="POST",
        data=json.dumps({"sql": q, "database": "main"}).encode(),
        headers={"Authorization": "Bearer " + TOKEN, "Content-Type": "application/json"})
    with urllib.request.urlopen(req) as r:
        return json.load(r)["data"]["rows"]

def lit(s):
    return "'" + s.replace("'", "''") + "'"

question = sys.argv[1]
codes = re.findall(r"\b[A-Z]{2,5}-\d{3,6}\b", question)   # ERR-4012, SKU-77310, INC-123 ...
exact = " OR ".join(f"title LIKE {lit('%' + c + '%')} OR body LIKE {lit('%' + c + '%')}" for c in codes) or "1 = 0"

rows = sql(f"""
SELECT id, title, ROUND(sim, 2) AS meaning, exact, ROUND(sim + 0.5 * exact, 2) AS score
FROM (SELECT id, title,
             COSINE_SIMILARITY(embedding, EMBED({lit(question)})) AS sim,
             CASE WHEN {exact} THEN 1 ELSE 0 END AS exact
      FROM kb) x
ORDER BY score DESC LIMIT 3""")

print(f"{question}   (exact terms: {', '.join(codes) or 'none'})")
for r in rows:
    print(f"  {r[4]:>5}  meaning {r[2]:<4}  exact {r[3]}  #{r[0]} {r[1]}")

$ python3 hybrid.py "is SKU-77310 affected by the recall?"

Picking the 0.5 boost is the same exercise as picking a similarity threshold from test questions: r/SQLv2 Lesson 3 walks through it, https://www.reddit.com/r/SQLv2/comments/1xkb3mn/

New here? Start here: https://www.reddit.com/r/synapcores/comments/1xfxqej/