如何创建一个模拟我的 mysql 数据库的弹性搜索索引

how to create an elastic search index which emulates my mysql db

我是 elasticsearch 的新手,我很难从 mysql 切换到 elasticsearch

我的 Mysql 我的 table 看起来像这样


    table :  test_request 
    +---------+-------------+--------------+-----------+------------+-----------+
    | test_id | device_name | ip_address   | user_name | time_stamp | show_flag |
    +---------+-------------+--------------+-----------+------------+-----------+
    |   1     |     d1      |   0.0.0.0    |   admin   |            |     Y     |
    +---------+-------------+--------------+-----------+------------+-----------+

    
table: test_results +----+---------+-----+-----------------------+-------------------------+----------------------------------+-----------+ | id | test_id | cli | xml | json | another json | show_flag | +----+---------+-----+-----------------------+-------------------------+----------------------------------+-----------+ | 1 | 1 | c1 | some xml format data | {"some":"json here"} | {"some":" another json here"} | Y | +----+---------+-----+-----------------------+-------------------------+----------------------------------+-----------+ | 2 | 1 | c2 | some xml format data | {"some":"json here"} | {"some":" another json here"} | Y | +----+---------+-----+-----------------------+-------------------------+----------------------------------+-----------+ | 3 | 1 | c2 | some xml format data | {"some":"json here"} | {"some":" another json here"} | Y | +----+---------+-----+-----------------------+-------------------------+----------------------------------+-----------+

test_requesttable中的test_id字段和test_resultstable中的id字段是auto increment .
jsonanother json 字段的数据类型为 JSON.

我正在尝试使用 elasticsearch_dsl 创建索引及其映射。我正在通过 docs 了解如何实现这一目标,但我无法弄清楚三件事

  1. 如何将 test_id 变为 auto increment
  2. 如何创建 JSON 数据类型的字段
  3. 在两者之间建立关系的最佳方式(我部分理解嵌套在这里可能有所帮助)但正在寻找正确的方式来做到这一点

auto increment id 列在 SQL 表中遵循以下规则:

  1. 它们是行的唯一标识符
  2. 它们允许在表之间 link 行

要在 elasticsearch 中实现此目的,您不需要 auto increment 字段。您可以将文档添加到elasticsearch索引中,elasticsearch会为其添加一个唯一的id。

对于 JSON 字段,只需使用 object datatype.

设置关系的选项很少,例如 SQL join:

  1. 您可以将 test_results 作为 nested 对象放入 test_request 文档
  2. 您可以使用 join datatype 字段将 link 文档与 test_request 文档放在同一索引内
  3. 您可以将每个 test_result 与其 test_request 一起反规范化并存储到单个文档中。没关系,test_request会被多次存储。无论如何,Elasticsearch 主要用于搜索。

选择哪个版本由您决定。这取决于你将如何使用你的数据,你将进行什么样的查询。您可以将所有 test_resultstest_request 一起收集并通过单个调用存储,还是需要存储 test_request 并连续添加 test_results?

连续更新嵌套字段意味着每次都重新索引整个文档。 Join datatype 查询成本高。

反规范化增加了 space 用法,但如果每个请求的 test_results 数量不大,那么它可能是最好的选择。