Elasticsearch field types¶
In Elasticsearch, data is stored as JSON-like documents, analogous to rows in a relational database, while fields correspond to columns.^[600-developer__Elasticsearch.md] The definition of these fields and their types is referred to as the Mapping^[600-developer__Elasticsearch.md].
Field types determine how data is indexed and queried. The two primary types discussed in the context of basic search configurations are keyword and text^[600-developer__Elasticsearch.md].
Common Field Types¶
text¶
The text type is designed for full-text search.^[600-developer__Elasticsearch.md]
* Analysis: Fields of this type are analyzed, meaning the text is broken down into tokens (a process called tokenization) by an analyzer.
* Inverted Index: These tokens are stored in an inverted index^[600-developer__Elasticsearch.md].
* Use Case: Ideal for searching within long content, such as the body of an email or a product description, where finding matches for parts of the text is required.
keyword¶
The keyword type is used for exact value matching.^[600-developer__Elasticsearch.md]
* No Analysis: Unlike text, keyword fields are not analyzed or split into tokens.^[600-developer__Elasticsearch.md]
* Matching: They are used for filtering, sorting, and aggregations. Queries against keyword fields typically treat the entire string as a single entity.
* Use Case: Suitable for fields like IDs, email addresses, tags, or status codes where the exact value must match.
Search Behavior and Queries¶
The field type dictates which query types are effective.^[600-developer__Elasticsearch.md]
matchquery: Typically used ontextfields. It analyzes the input search string and breaks it down to match against the indexed tokens.termquery: Typically used onkeywordfields or for exact lookups. It searches for the exact, un-analyzed term in the inverted index^[600-developer__Elasticsearch.md]. For example, searching for "iPhone 手機" using atermquery would look for that exact phrase rather than the individual words "iPhone" or "手機"^[600-developer__Elasticsearch.md].
Mapping Configuration¶
Mappings can be defined manually or configured to be dynamic^[600-developer__Elasticsearch.md].
Sources¶
- 600-developer__Elasticsearch.md