Преглед на файлове

docs: the regex filter is case-sensitive, byte-oriented, and silent on a bad pattern

A consumer measured eight regex filters on prod and found three traps, all
confirmed against the database's evaluation arm and all reproduced locally
against its exact code shape:

- matching is case-sensitive and (?i) is not valid in the grammar, so it
  throws and is swallowed - it reads as "no matches", not as an error
- the engine addresses BYTES, so a character class over non-ASCII is wrong
  in both directions: sz[aA]mfejt (accented) can never match, while [oO]
  (umlauted) shares a lead byte with every accented vowel and matches
  almost every row. Control: it matches text containing neither character
- an invalid pattern is caught and reported as no-match, so a well-formed
  spec carrying a broken pattern answers 200 with an empty list. This is a
  different layer from the 400 bad_filter that catches a malformed spec

Our own line recommending regex as the field-scoped replacement for search
was silent on all three, which is the silence-propagates failure sitting in
the sentence that steers callers onto the operator.

Also warns off nested quantifiers on caller-supplied input: measured, a
six-character pattern of that shape takes 11 s at 26 bytes and over 25 s at
28, cleanly exponential.

The evaluation is the database's layer, not ours, so the findings are routed
there rather than fixed here; a move to RE2 is in progress upstream. The
paragraph is therefore stamped with the version it was measured on and says
which parts the move will change, and both the doc and the UI hint lead with
the advice that is correct under either engine: spell alternatives out as
alternation, and escape anything a user typed.
fszontagh преди 1 месец
родител
ревизия
28cc131aa9
променени са 2 файла, в които са добавени 18 реда и са изтрити 1 реда
  1. 5 1
      api/llms.txt
  2. 13 0
      webui/src/pages/Documents.tsx

+ 5 - 1
api/llms.txt

@@ -123,7 +123,11 @@ Supported ops: `eq`, `ne`, `lt`, `lte`, `gt`, `gte`, `in`, `contains`, `exists`,
 - `in` - value is a JSON array, e.g. `colour:in:["red","yellow"]`.
 - `contains` - **array membership**, not substring: it matches when the field is an array holding the value. `colour:contains:yellow` does NOT match the string `"yellow"`.
 - `search` - **IGNORES THE FIELD NAME.** A case-insensitive substring over the WHOLE document: its id, its keys, and every string and number in it. So `colour:search:ell` matches `"yellow"` in `colour` AND a document whose `note` says "excellent", AND one whose id contains `ell`. The field is syntactically required and semantically discarded - `nosuchfield:search:ell` matches exactly what `colour:search:ell` does. **For a substring within ONE field, use `regex` on that field.** Worth care if a `search` filter is doing scoping work: it admits rows you may believe it excludes, and in the same direction as a filter that was dropped entirely.
-- `regex` - regular-expression match, and the field IS honoured. This is the field-scoped substring: `colour:regex:ell`.
+- `regex` - regular-expression match, and the field IS honoured. This is the field-scoped substring: `colour:regex:ell`. **Three things about it are measurable and none of them announce themselves, so read this before building a search box on it.** Everything in the four bullets below was measured against **database 2.17.2**, which is what the deployed server runs today. **A move to RE2 is in progress upstream and will change three of the four**, so treat these as the behaviour of the current server rather than as a standing contract: under RE2 a character class becomes character-oriented (so `[áÁ]` starts working), `(?i)` becomes valid, an invalid pattern becomes a real error instead of an empty result, and the exponential backtracking goes away - while **backreferences and lookaround stop being supported**, since RE2 has neither. **The one piece of advice that is correct under BOTH engines is to spell alternatives out as alternation** (`sz(?:á|Á)mfejt`) and to escape anything a user typed; a pattern written that way needs no change when the server moves. This paragraph will be updated when it ships.
+  - **Matching is CASE-SENSITIVE and there is no flag to ask otherwise.** `text:regex:dolgoz` and `text:regex:Dolgoz` are different queries. **`(?i)` does NOT work** - it is not valid in this grammar, and an invalid pattern behaves as described two bullets down, so `(?i)Dolgoz` reads as "no matches" rather than as an error. Case-fold caller-side: spell the alternatives out, e.g. `text:regex:(?:d|D)olgoz`.
+  - **The engine is BYTE-oriented, not UTF-8 aware, so a CHARACTER CLASS over non-ASCII text is wrong in BOTH directions.** A character class matches one BYTE, and any non-ASCII character is several. So `sz[áÁ]mfejt` can never match `számfejt` - it reads as "no matches" - while `[öÖ]` matches almost every Hungarian sentence, because `ö` and `Ö` share the lead byte `0xC3` with `á é í ó ú ü ő ű` and the class degenerates into match-nearly-anything. One failure reads as an empty corpus and the other as a broken search; neither reports a fault. **Use ALTERNATION of the whole sequences instead of a class**: `sz(?:á|Á)mfejt` is correct and was measured correct. The same caution applies to `.`, `\w` and any quantifier applied to a non-ASCII character, since each addresses bytes rather than characters.
+  - **AN INVALID PATTERN MATCHES NOTHING AND ANSWERS 200 - it is NOT an error.** `text:regex:dolgoz(` cannot compile, and the response is a clean empty result list, indistinguishable from a valid pattern that genuinely matched no rows. Note this is a different layer from the **400** `bad_filter` below, which catches a malformed `field:op:value` SPEC: a well-formed spec carrying a broken PATTERN is not currently refused anywhere. **So never pass end-user text through to `regex` unescaped** - every unbalanced `(`, `[` or trailing `\` a customer types in a search box will look to them like an empty knowledge base. Escape the input, or validate the pattern before sending it.
+  - **Avoid nested quantifiers on caller-supplied input** (`(a+)+`, `(.*)*` and similar). The engine backtracks exponentially: measured in isolation, a 6-character pattern of that shape against a 26-byte subject took 11 seconds and did not finish within 25 at 28 bytes. A pattern you construct yourself is fine; one assembled from user input is a way to make your own queries time out.
 - `exists` - `field:exists:true` / `field:exists:false`.
 
 The value is parsed as JSON when it parses (numbers, booleans, arrays), otherwise it is taken as a string. Everything after the second `:` is the value, so a value may itself contain `:`. A malformed spec or unknown operator is rejected with **400** `bad_filter`.

+ 13 - 0
webui/src/pages/Documents.tsx

@@ -217,6 +217,19 @@ function FilterBar({
         its keys. For a substring within one field use <strong>regex</strong>.
       </p>
     )}
+    {filterOp === 'regex' && (
+      <p className="text-xs text-slate-400" data-testid="filter-op-hint">
+        <strong>regex</strong> is evaluated by the database, so its exact dialect
+        follows the server version. Write accented alternatives as alternation
+        rather than a character class -{' '}
+        <code>sz(?:&aacute;|&Aacute;)mfejt</code>, not{' '}
+        <code>sz[&aacute;&Aacute;]mfejt</code> - which is correct on every
+        version. On the current server matching is case-sensitive, a class
+        matches single BYTES (so <code>[&ouml;&Ouml;]</code> matches almost every
+        row and <code>sz[&aacute;&Aacute;]mfejt</code> matches none), and an
+        invalid pattern returns no rows instead of an error.
+      </p>
+    )}
     {filterOp === 'contains' && (
       <p className="text-xs text-slate-400" data-testid="filter-op-hint">
         <strong>contains</strong> is array membership, not a substring: it matches