【问题标题】:How to avoid index explosion in ElasticSearch如何避免 ElasticSearch 中的索引爆炸
【发布时间】:2020-10-06 14:43:20
【问题描述】:

我有两个来自同一索引的文档,最初看起来像这样(此处仅显示 _source 值)

{
    "id" : "3",
    "name": "Foo",
    "property":{
        "schemaId":"guid_of_the_RGB_schema_defined_extenally",
        "value":{
            "R":255,
            "G":100,
            "B":20
        }
    }
}
{
    "id" : "2",
    "name": "Bar",
    "property":{
        "schemaId":"guid_of_the_HSL_schema_defined_extenally",
        "value":{
            "H":255,
            "S":100,
            "L":20
        }
    }
}

模式(用于验证value)存储在 ES 外部,因为它与索引无关。 如果我不定义映射,value 字段将被视为Object 映射。一旦有新的子字段,它的子字段就会增长。

目前,ElasticSearch 支持 Flattened mapping https://www.elastic.co/guide/en/elasticsearch/reference/current/flattened.html 以防止索引中的这种爆炸。但是由于其限制,它对搜索内部字段的支持有限:As with queries, there is no special support for numerics — all values in the JSON object are treated as keywords. When sorting, this implies that values are compared lexicographically.

我需要能够查询索引以找到与给定文档匹配的文档(例如 [10,30] 范围内的 B)

到目前为止,我想出了一个像这样构建我的文档的解决方案

{
    "id":4,
    "name":"Boo",
    "property":
    {
        "guid_of_the_normalized_RGB_schema_defined_extenally":
        {
           "R":0.1,
           "G":0.2,
           "B":0.5
        }
}

虽然它没有解决我的映射爆炸问题,但它缓解了其他一些问题。 我的映射现在看起来与字段 property 类似

"property": {
        "properties": {
          "guid_of_the_RGB_schema_defined_extenally": {
            "properties": {
              "B": {
                "type": "long"
              },
              "G": {
                "type": "long"
              },
              "R": {
                "type": "long"
              }
            }
          },
          "guid_of_the_normalized_RGB_schema_defined_extenally": {
            "properties": {
              "B": {
                "type": "float"
              },
              "G": {
                "type": "float"
              },
              "R": {
                "type": "float"
              }
            },
          "guid_of_the_HSL_schema_defined_extenally": {
            "properties": {
              "B": {
                "type": "float"
              },
              "G": {
                "type": "float"
              },
              "R": {
                "type": "float"
              }
            }
          }
        }
      }

这解决了字段名称相同但数据类型不同的情况。

有人可以建议我一个解决方案,可以解决索引爆炸的问题,而不会受到Flattened 在搜索中的限制吗?

【问题讨论】:

    标签: elasticsearch elasticsearch-7


    【解决方案1】:

    为避免映射爆炸,最好的解决方案是更好地规范化您的数据。 您可以在映射中设置“动态”:“严格”,如果文档包含映射中尚不存在的字段,则文档将被拒绝。 之后,您仍然可以添加新字段,但您必须先在映射中显式添加它们。

    您可以添加pipeline 以在摄取前清理和规范化您的数据。

    如果您不想要或无法重新索引:

    即使您不知道密钥的“中间”部分,为了使您的查询更容易,您可以使用带星号的多重匹配。

    GET myindex/_search
    {
      "query": {
        "multi_match": {
          "query": 0.5,
          "fields": ["property.*.B"]
        }
      }
    }
    

    但是您仍然无法按照自己的意愿对其进行排序。 要在不接触数据的情况下对多个“未知”字段名称进行排序,您可以使用脚本:https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-sort-context.html

    但也许您可以通过在索引中添加动态模板来简化整个过程。

    PUT test/_mapping
    {
      "dynamic_templates": [
        {
          "unified_red": {
            "path_match": "property.*.R",
            "mapping": {
              "type": "float",
              "copy_to": "unified_color.R"
            }
          }
        },
        {
          "unified_green": {
            "path_match": "property.*.G",
            "mapping": {
              "type": "float",
              "copy_to": "unified_color.G"
            }
          }
        },
        {
          "unified_blue": {
            "path_match": "property.*.B",
            "mapping": {
              "type": "float",
              "copy_to": "unified_color.B"
            }
          }
        }
      ],
      "properties": {
        "unified_color": {
          "properties": {
            "R": {
              "type": "float"
            },
            "G": {
              "type": "float"
            },
            "B": {
              "type": "float"
            }
          }
        }
      }
    }
    

    然后您将能够使用相同的查询查询任何值:

    GET test/_search
    {
      "query": {
        "range": {
          "unified_color.B": {
            "gte": 0.1,
            "lte": 0.6
          }
        }
      }
    }
    

    对于已经存在的字段,您必须自己在映射中添加 copy_to,然后运行 ​​_update_by_query 来填充它们。

    【讨论】:

      猜你喜欢
      • 2021-09-19
      • 2012-01-23
      • 2017-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      • 2011-07-07
      • 2013-12-25
      相关资源
      最近更新 更多