Introduction to ElasticSearch mapping and templates

I. Introduction

There is a series of related articles in the past that introduce the basic concepts of ES and the use of various versions of SDK. ES has now been upgraded to version 8.5. Some concepts and SDK usage have changed greatly. Subsequent ES-related articles will be introduced based on version 8.3. Some concepts that need to be mastered for practical application and some practical examples.

2. Mapping

After the ES environment is set up, the first thing to consider is how to define mapping. Mapping is equivalent to the table structure. It is the process of defining how documents and the fields they contain are stored in the index library.

Mapping is divided into dynamic mapping and explicit mapping

1. Dynamic mapping

Using dynamic mapping does not require first creating an index and defining fields when writing index data. Fields and field types are automatically created based on rules.

Write index data directly in kinaba's dev tools

Then we use the command get /dy_idx/_mapping to check the index structure

Returning several fields means that it will automatically set the type for you according to the rules. Count is of long type, create_date is of date type, and memo is of text type.

Then we write another piece of data and change count into a string

An error will be reported at this time, because the field type has been set based on the first write, and the data written later will be constrained by the type.

The mapping rules are as follows, just have a general understanding:

The default is to use the "dynamic":true rule.

Note: You can also define date and number detection rules yourself or turn off automatic detection.

2. Explicit mapping

The mapping structure needs to be defined before writing data. After definition, the data written is subject to type constraints. Generally, it is simpler to define it in this way.

data input

Note: Because the birth field in the definition includes time, the time must also be included in the format when writing data.

3. ElasticSearch field type

Common types

     binary (store base64 result value or binary value), boolean, keyword (full-text search cannot be performed without word segmentation), numbers (long, double), date, alias (alias), text (word segmentation and indexing will be created during storage, suitable for full-text search) ).

Object and relationship types

    object (json object), nested (nested), array.

Other types

    range、rank_feature、token_count、ip、geo_point、geo_shape

3. Index template

Here we only introduce static templates. Dynamic templates are more complicated and will not be used for the time being.

Usage scenarios of index templates: For example, our inventory snapshot data needs to create index storage data on a daily basis, that is, one index file per day. If there is no index template, we need to create index mapping regularly every day, but with the index template, when you insert data It will check whether there is an index template matching this index name, and if so, create an index mapping based on the template and finally insert the data.

1. Create a template

Note: Here index_patters is set to regular matching.

2. Write data

November 15, 2022 an index file

November 16, 2022 an index file

Note: The following is the syntax for batch writing. The first store_id is "aa" and the writing will fail because it is constrained by the index template field type.

In kinaba's Index Manager, you can see that two index files are generated.

Supongo que te gusta

Origin blog.csdn.net/2301_76787421/article/details/133420357
Recomendado
Clasificación