Substring matching on OpenSearch
On OpenSearch and Elasticsearch, contains and not contains look for the text inside the value, exactly as a SQL substring match does. There is no tokenising, stemming or relevance scoring. A field can opt in to full-text matching explicitly if a host wants that instead.
Rule. On OpenSearch/Elasticsearch, contains/notContains ($defs/binaryOperator) compile to substring-matching query types — wildcard against a keyword field, or a per-field ngram- analyzed sub-field strategy (schema-configured) — never to a tokenized match query. This pins contains's behavior to exactly what SQL's LIKE '%pattern%' does: substring presence, no tokenization, no stemming, no stopword removal, no relevance scoring. A per-field, explicit opt-in searchMode: analyzed is the documented escape hatch for a host that deliberately wants relevance-style full-text matching instead of substring semantics for that field.
Rationale. ES/OpenSearch's idiomatic "search for text" mechanism is match against an analyzed field, and a naive compiler reaches for it because the operator is literally named contains and match reads as "does it contain this". That is the tokenized-match trap: match splits both the indexed field and the query string into analyzer tokens (lowercased, stemmed, stopword-filtered) and matches when any token overlaps — it does not check for a literal substring. contains: "art" against the indexed value "Smart Watch" returns no hit under match (the analyzer produces whole tokens "smart" and "watch", neither of which is "art"), while the identical filter compiled to SQL (LIKE '%art%') or Mongo ($regex) matches that row. The same IR silently diverges across backends — precisely the parity failure G3 exists to prevent, and the TRD's problem statement (§1) calls out ES/OpenSearch support as the one thing "nothing exists" for in any prior art, largely because this exact trap is unsolved elsewhere. Pinning substring semantics trades away ES's own relevance ranking for contains; searchMode: analyzed exists so a host can make that trade deliberately, per field, instead of getting it by accident.
Worked example — contains: "art" against name, where a document has name = "Smart Watch":
- Substring semantics (the default this rule pins) —
wildcardagainst thekeywordsub-field matches, because the literal substring"art"is present, exactly mirroringLIKE '%art%':{ "wildcard": { "name.keyword": { "value": "*art*", "case_insensitive": true } } }A per-field ngram-indexed sub-field (e.g.
name.ngram, matched with a plainmatchagainst that field) is the higher-index-cost/lower-query-cost alternative mechanism SC-3 lets a field choose instead — same illustrative substring outcome, different index-time/query-time trade-off (PERF-4). - The tokenized trap being explicitly ruled out — shown here only as the anti-pattern the rule exists to forbid, never emitted by the compiler for
contains:{ "match": { "name": "art" } }— no hit against
"Smart Watch", which is exactly the silent divergence from SQL/Mongo this rule prevents. searchMode: analyzedopt-in — when a field is explicitly configured this way,containscompiles to the tokenized form instead, as a documented, deliberate divergence:{ "match": { "name": { "query": "art", "operator": "and" } } }- For comparison, the same logical outcome on the other backend families, achieved without an opt-in since neither needs one: SQL
LIKE-based substring matching, with the pattern's own%/_wildcard characters escaped per SEC-4 and an explicitESCAPEclause naming the escape character used — required because, unlike PostgreSQL (whose defaultLIKEescape character is\unless overridden), SQL Server, SQLite and Oracle have no default escape character at all and MySQL's depends on theNO_BACKSLASH_ESCAPESsession mode; without a compiler-suppliedESCAPEclause, SEC-4's escaping is silently a no-op on four of the five SQL dialects, and a user-supplied literal%/_still behaves as a wildcard:WHERE name LIKE '%' || :escapedPattern || '%' ESCAPE '\'MongoDB has no native non-regex substring operator, so
containsthere is necessarily a$regexbuilt from an already-escaped literal — never compiled from a client-supplied pattern, consistent with SEC-5's exclusion of user-controlled regex (ReDoS surface). The escaping here is a distinct character class from SEC-4: SEC-4 covers SQLLIKEwildcards (%,_,\) and ES wildcard syntax (*,?) — it says nothing about regex metacharacters. Building$regexfrom a SEC-4-escaped-only pattern still leaves.,+,*,(,),[,],{,},|,^,$live in the pattern, which is both a match-scope-widening bug (the pattern matches more than the literal substring the user typed) and, combined with attacker-influenced repetition/alternation, a ReDoS vector — the exact surface SEC-5 excludes user-controlled regex operators to avoid, reopened here unless this document pins its own escape set. The value MUST be escaped against every regex metacharacter (a full backslash-escape of every character outside[A-Za-z0-9], not just SEC-4's four LIKE/wildcard characters) before insertion:{ "name": { "$regex": "escaped-pattern-here", "$options": "i" } }
Must NOT.
- Must NOT compile
contains/notContainsto amatchquery as the default — that is the tokenized-match trap this rule exists to rule out. - Must NOT emit a SQL
LIKEpattern without a matchingESCAPEclause — SEC-4's wildcard-escaping is a no-op on any dialect without a default escape character (SQL Server, SQLite) or with backslash-escaping disabled (MySQL'sNO_BACKSLASH_ESCAPES). Read this together with SQL-5 in the canonical-rendering addendum, which is where the per-dialect answer lives: the operator, theESCAPEclause's spelling, and the escape alphabet all vary. Taken literally as a universal rule, this bullet is wrong on two of the four v1 SQL dialects — SQLite's case-sensitive path isGLOB, which has noESCAPEclause at all, and SQL Server's alphabet includes[on top of SEC-4's three. - Must NOT build the MongoDB
$regexvalue by applying only SEC-4's LIKE/wildcard escape set — regex metacharacters are a distinct, wider class (see the worked example above) and must be escaped in full before insertion; SEC-4 alone is not sufficient here and must NOT be treated as though it were, never compiled from a client-supplied regex (SEC-5). - Must NOT let
searchMode: analyzedbecome a silent or implicit default for any field — it is an explicit per-field opt-in (SC-3); its absence always means substring semantics. - Must NOT assume a
wildcardquery is index-friendly without akeyword(or normalizer-equipped) field/sub-field behind it — runningwildcardagainst an analyzed text field undermines the point of pinning substring semantics and belongs, if used, in the per-backend index-guidance docs (PERF-4) as an explicitly documented trade-off, not assumed silently here.
Cross-links. $defs/binaryOperator (contains, notContains), $defs/stringValue (the value being substring-matched), $defs/fieldName.