# Search

**Setări → Căutare** (Settings → Search) chooses the engine behind product search, in the shop and in the admin.
Changing it needs *Manages settings*, and the page asks for your password again, because it holds the Typesense API
key.

- **PostgreSQL** needs nothing extra: it is the shop's own database. It suits a catalogue of a few thousand products.
- **Typesense** is a separate search server, or a cluster of three. It is faster on large catalogues, forgives typos
  and finds products from their first letters. PostgreSQL stays up to date next to it: whenever Typesense does not
  answer, search moves to PostgreSQL by itself and back again once Typesense recovers.

Either way, an exact SKU, barcode or manufacturer code comes first in the results.

## Using Typesense

1. Run Typesense on your own server or a hosted cluster. One node is enough to start; three keep working when one
   fails.
2. Create an API key that may create collections and aliases, write documents and search. Do not use the server's
   bootstrap key.
3. Fill in the form:
   - **Nodurile** (nodes): one address per line, for example `https://search-1.magazin.ro:8108`.
   - **Numele indexului** (index name): the alias the shop searches through. Shops sharing one cluster each need
     their own.
   - **Timp de așteptare** (timeout): after this many seconds, search answers from PostgreSQL. 2 seconds usually
     suits.
   - **Cheia API** (API key). It is stored encrypted and never shown again; leave the field empty to keep it. If you
     change the nodes, type the key again: the saved key is only ever sent to the saved nodes.
4. **Testează conexiunea** (test the connection) checks that every node answers and that the key is accepted. It then
   creates a small test index, writes to it, searches it, points an alias at it and deletes both. A node that is down
   while the others answer is a warning: the cluster still works without it. **Salvează** (save) runs the same test
   and saves only if it passes.

After saving, the shop builds the Typesense index in the background. Until the index is ready, search keeps answering
from PostgreSQL, so customers never see empty results. The **Starea** (status) card shows which engine answers now:

| Status | Meaning |
|---|---|
| Căutarea folosește PostgreSQL | Typesense is not chosen. |
| Indexul Typesense nu e construit încă | The index for this cluster is being built, or the build failed. **Construiește indexul** starts it again. |
| Căutarea folosește Typesense | Typesense answers, with PostgreSQL as the fallback. |
| Typesense nu a răspuns de câteva ori la rând | PostgreSQL answers for a few seconds, then Typesense is tried again. |

A new cluster or a new index name builds a new index. A new API key does not.

## Rebuilding the index

**Reconstruiește indexul** (rebuild the index) rebuilds it in the background; search answers from the old index until
the new one is ready. From the command line: `php artisan search:rebuild`. The nightly *Check the search index* task
(see [Scheduled tasks](scheduled-tasks.md)) catches anything that fell behind, so a rebuild is rarely needed.

## Going back to PostgreSQL

Choose PostgreSQL and save. Typesense is no longer asked anything. Choosing Typesense again later builds a fresh
index, because it missed every change in between.

## Semantic search

With AI switched on (Settings → AI), the shop's search understands searches in plain words and, with Typesense,
searches by meaning as well as by words.

- **Înțelegerea căutării** (search understanding) reads a search that looks like a sentence. That means three words or
  more, or one with a budget, a recipient or a purpose ("sub 200 lei", "cadou", "pentru"). The model turns it into:
  - the words naming the products;
  - a price range;
  - a brand.

  „cadou pentru un pasionat de grădinărit sub 200 lei" becomes *grădinărit*, up to 200 lei.

  The results page says what was understood and links to **Caută exact** (search exactly as typed). If what was
  understood finds nothing, the shop searches as typed. Product names and codes are never sent: they go straight to the
  index. The price range works with PostgreSQL and Typesense alike, on each product's lowest retail price.
- **Căutare semantică** (semantic search) needs **Typesense**. Every 15 minutes, the shop makes a vector for each
  product on sale whose name, brand, categories or descriptions changed. Vectors are kept in the database, so a rebuild
  does not pay for them again. Typesense searches by words and meaning together, so „ceva pentru grădină" finds the
  gardening tools too. With PostgreSQL, no vectors are made or paid for.
- **What leaves the shop:** only the search text, after e-mail addresses and long runs of digits (phone numbers, CNP,
  IBAN, cards) are taken out, and the product texts for vectors. Nothing about the shopper is sent.
- **Cost:** each feature needs a **daily budget** before it can be switched on, because shoppers set it off. A search's answer is kept for a day, so a popular search is paid for once. One visitor gets at most 10 searches a minute read by AI; past that, their searches go as typed. Shoppers' calls never wait on one another for the budget; calls already under way when the budget runs out may pass it by a little.
- **Failures:** with AI off, out of budget or not answering, the search simply goes as typed.

