Elasticsearch / Basic: document and query contracts / Lesson 7
Choose full-text or exact-value search
Compare a match query on analyzed text with a term query on a keyword identifier.
On this page
Lesson goals
- Explain analyzed text and exact-value field contracts.
- Read a synthetic mapping and two query examples.
- Verify analysis, visibility, and expected matches.
Start with the user’s question
A museum visitor searching words in a title asks a different question from an application looking up an exact catalog code. Elasticsearch maps fields according to their intended use: a text field is analyzed for full-text search, while a keyword field is suited to exact values. Define case, language, punctuation, and identifier rules before choosing a query. The original source value alone does not reveal every indexed term.
Create a disposable mapping and document
The following requests are illustrative for a new isolated Elasticsearch 8.19 lab. They create an explicit mapping and a synthetic artifact, then wait for the write to become searchable. Do not use the index name against a configured cluster. The actual response, access permissions, and refresh timing depend on the environment; the public course does not execute these requests.
PUT /lab-artifacts
{
"mappings": { "properties": {
"title": { "type": "text" },
"catalog_code": { "type": "keyword" }
} }
}
PUT /lab-artifacts/_doc/1?refresh=wait_for
{ "title": "Blue ceramic bowl", "catalog_code": "ART-101" }
The title is mapped as text and catalog_code as keyword. The document contains a synthetic title and exact identifier. refresh=wait_for requests search visibility before the indexing response completes.
Search each field deliberately
The first request uses match for words in the title. The second uses term for the exact catalog code. Both are intended for the preceding fixture after it becomes searchable. A term query on analyzed text does not analyze input like a full-text query, so a complete title string can fail to match the indexed terms. Confirm the actual analyzer and mapping before diagnosing a missing result.
GET /lab-artifacts/_search
{ "query": { "match": { "title": "ceramic bowl" } } }
GET /lab-artifacts/_search
{ "query": { "term": { "catalog_code": "ART-101" } } }
With the declared mapping and fixture, each query is intended to find the artifact. Actual response details and scores depend on the environment. The examples demonstrate distinct search contracts, not a universal relevance ranking.
Inspect analysis and visibility
If a title search misses a document, verify the target index, individual write response, refresh visibility, mapping, and analysis in that order as evidence permits. Use the documented analyze API on representative synthetic text to inspect tokens. Test Arabic and accented labels under the actual analyzer. A successful document GET does not necessarily establish immediate search visibility, and a visible source field does not prove the expected indexed representation.
Build a small acceptance set
Create a set of expected matches and nonmatches for a full title, individual words, a differently cased code, and a nonexistent code. Run it through the same client, mapping, and authorization context used by the application. Record whether exact codes are case sensitive under the chosen mapping. Test both result identity and ordering where relevant; a plausible hit alone does not prove the complete search contract.
Common errors and fixes
A complete title does not match a term query.
- Why it happens
- The text field was analyzed into terms while term input was expected to match an indexed term.
- How to fix it
- Use an appropriate full-text query or an exact-value field for the intended question.
- Prevent it next time
- Document field mappings and test representative search inputs.
A newly indexed artifact is missing from search.
- Why it happens
- Search visibility or the target index was assumed without evidence.
- How to fix it
- Inspect the write result, target, and refresh boundary.
- Prevent it next time
- Test the application’s visibility requirement.
A differently cased catalog code does not match.
- Why it happens
- The exact field’s normalization and case contract was assumed.
- How to fix it
- Use the intended exact value or a deliberately configured normalization strategy.
- Prevent it next time
- Include case variants in acceptance tests.
2 practice exercises
1. Which field is intended for an exact catalog-code filter in the fixture?
Show worked solution
catalog_code is mapped as keyword, so the term query for ART-101 expresses the exact identifier lookup. Confirm any normalization and case rules in the actual mapping.
2. Why inspect analysis when a title search behaves unexpectedly?
Show worked solution
The indexed and query text may be transformed into terms. Inspecting the configured analysis helps explain whether the expected words are present and comparable.
Key takeaways
- Choose queries from the field mapping and user’s question.
- Analyzed text and exact identifiers have different contracts.
- Verify target, visibility, and analysis with representative examples.