乐闻世界logo
搜索文章和话题

What is an Elasticsearch index mapping?

1个答案

1

Elasticsearch index mapping is the process of defining how fields within an index are stored and indexed. In short, it functions similarly to a table structure definition in a database, specifying the data types of each field (such as integers, strings, or boolean values) and the precise rules for indexing (for example, whether tokenization should occur or if the field should be stored).

In Elasticsearch, mappings can be explicitly defined or implicitly inferred. When you explicitly define a mapping, you gain control over the behavior of each field in the index, which can significantly enhance search and storage efficiency. For instance, you might have a field named date_of_birth, and you can specify it as the date type in the mapping while defining a specific date format, ensuring Elasticsearch processes and indexes this field correctly.

Example

Suppose we are working with an Elasticsearch index containing user information. One of the fields is user_id, and we want to ensure this field is correctly indexed as a keyword type that does not undergo tokenization (i.e., keyword), enabling precise queries.

The mapping definition might appear as follows:

json
{ "mappings": { "properties": { "user_id": { "type": "keyword" }, "name": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "email": { "type": "keyword", "ignore_above": 256 }, "date_of_birth": { "type": "date", "format": "yyyy-MM-dd" } } } }

In this mapping, the user_id and email fields are defined as keyword type, meaning they are not processed by the tokenizer and can be used for exact match queries. The name field is defined as text type with a sub-field keyword, allowing it to support both full-text search and exact search.

By defining mappings in this manner, Elasticsearch can store and index data more efficiently, providing robust support for queries, thereby improving performance and ensuring the accuracy of results.

2024年8月13日 21:34 回复

你的答案