3 minutes to quickly understand the similarities of field types in mysql and es

Elasticsearch's and MySQL's field types are similar in many ways. These similarities are primarily reflected in their ability to represent primitive data types. Here are some similar field types in Elasticsearch and MySQL:

  1. text:

    • Elasticsearch:textkeyword
    • MySQL:VARCHAR, CHAR, TEXT, TINYTEXT, MEDIUMTEXT, LONGTEXT

    In Elasticsearch, texttypes are generally used for full-text searches, while keywordtypes are strings for exact values. In MySQL, the VARCHARand CHARtypes are used to store variable-length and fixed-length strings, while TEXTthe type and its variants are used to store larger strings.

  2. value:

    • Elasticsearch:integerlongshortbytefloatdoublehalf_floatscaled_float
    • MySQL:INT, BIGINT, SMALLINT, TINYINT, MEDIUMINT, FLOAT, DOUBLE, DECIMAL

    Both Elasticsearch and MySQL support a variety of integer and floating-point number types. These types have similar data representation range and precision between the two.

  3. date:

    • Elasticsearch:date
    • MySQL:DATE, DATETIME, TIMESTAMP

    Both Elasticsearch and MySQL support the date type for representing dates and times. They support different date formats, and can perform date and time queries and calculations.

  4. Boolean:

    • Elasticsearch:boolean
    • MySQL:BOOL, BOOLEAN

    Both Elasticsearch and MySQL support a Boolean type for representing trueor false.

Although Elasticsearch and MySQL share similarities in these basic data types, they differ significantly in the way they process and query data. Elasticsearch is a distributed search engine for full-text search and real-time analysis, while MySQL is a relational database management system, mainly used to store structured data. Therefore, they have different characteristics and advantages in data modeling, indexing, query and performance optimization.

Below is an example of Elasticsearch and MySQL showing how to use each to store and query a set of book data respectively.

Suppose we have the following book data:

  1. Title: "Alive", Author: "Yu Hua", Publication Date: "1992-01-01", Category: "Fiction"
  2. Title: "One Hundred Years of Solitude", Author: "García Márquez", Publication Date: "1967-05-30", Category: "Fiction"
  3. Book Title: "A Brief History of Humanity", Author: "Yuval Harari", Publication Date: "2011-01-01", Category: "History"

In Elasticsearch, we can do the following:

  1. Create an booksindex called :
curl -X PUT "localhost:9200/books?pretty" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "properties": {
      "title": { "type": "text" },
      "author": { "type": "keyword" },
      "publish_date": { "type": "date" },
      "category": { "type": "keyword" }
    }
  }
}'
  1. booksAdd book documents to the index:
# 添加《活着》
curl -X POST "localhost:9200/books/_doc?pretty" -H 'Content-Type: application/json' -d'
{
  "title": "活着",
  "author": "余华",
  "publish_date": "1992-01-01",
  "category": "小说"
}'

# 添加《百年孤独》
curl -X POST "localhost:9200/books/_doc?pretty" -H 'Content-Type: application/json' -d'
{
  "title": "百年孤独",
  "author": "加西亚·马尔克斯",
  "publish_date": "1967-05-30",
  "category": "小说"
}'

# 添加《人类简史》
curl -X POST "localhost:9200/books/_doc?pretty" -H 'Content-Type: application/json' -d'
{
  "title": "人类简史",
  "author": "尤瓦尔·赫拉利",
  "publish_date": "2011-01-01",
  "category": "历史"
}'
  1. Search for books with category "fiction":
curl -X GET "localhost:9200/books/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "term": {
      "category": "小说"
    }
  }
}'

In MySQL, we can do the following:

  1. Create a bookstable called :
CREATE TABLE books (
  id INT AUTO_INCREMENT PRIMARY KEY,
  title VARCHAR(255),
  author VARCHAR(255),
  publish_date DATE,
  category VARCHAR(255)
);
  1. booksInsert data into the table :
INSERT INTO books (title, author, publish_date, category)
VALUES
  ('活着', '余华', '1992-01-01', '小说'),
('百年孤独', '加西亚·马尔克斯', '1967-05-30', '小说'),
('人类简史', '尤瓦尔·赫拉利', '2011-01-01', '历史');
  1. Search for books with category "fiction":
SELECT * FROM books WHERE category = '小说';

This example shows how to store and query the same book data in Elasticsearch and MySQL. Although their field types and query syntax are different, they can all meet basic data storage and retrieval needs. It should be noted that Elasticsearch is better at handling full-text search and real-time analysis, while MySQL is a relational database, which is more suitable for storing structured data and processing complex relational queries.

Guess you like

Origin blog.csdn.net/u011197085/article/details/130450119