JSON 层次结构中的弹性搜索动态字段

衔尾蛇

我将有一个 JSON 文档,其中会有嵌套。例子是:

{
    "userid_int" : <integer>
    "shoes" : {
         "size_int" : <integer>,
         "brand_str" : <string>,
         "addeddate_dt" : <date>
    },
    "shirt" : {
         "size_int" : <integer>,
         "brand_str" : <string>,
         "addeddate_dt" : <date>
         "color_str" : <string>
    },
    ...
}

嵌套字段可以是什么没有限制。例如,我可能想要一个特定文档的新键“睡衣”。但这在创建索引时是未知的。

我只想知道动态字段映射是否适用于整个 JSON,包括嵌套在任何级别,或者不是?

此映射是否适用于嵌套字段内的所有_int/_str /etc 字段?

PUT my_index
{
  "mappings": {
    "dynamic_templates": [
      {
        "_int_as_integers": {
          "match":   "*_int",
          "mapping": {
            "type": "integer"
          }
        },
        "_str_as_strings": {
          "match":   "*_str",
          "mapping": {
            "type": "string"
          }
        },
        ...
      }
    ]
  }
}
乔·索罗辛

这取决于你的意思nested有一种我称之为琐碎嵌套性的东西,即在对象中有对象,并且有句法嵌套性,它具有专用类型和某些固有属性,其中最重要的是能够将嵌套子对象视为完全独立的. 需要注意的是,查询需要遵守特定的语法

两点说明:

  • 您的动态模板几乎是正确的——只需确保各个模板是单独的对象定义(见下文)
  • 该类型string已被弃用一段时间,以支持text

  1. 琐碎的嵌套
PUT trivial_nestedness
{
  "mappings": {
    "dynamic_templates": [
      {
        "int_as_integers": {
          "path_match": "*.*_int",
          "mapping": {
            "type": "integer"
          }
        }
      },
      {
        "str_as_strings": {
          "path_match": "*.*_str",
          "mapping": {
            "type": "text"
          }
        }
      }
    ]
  }
}

POST trivial_nestedness/_doc
{
    "userid_int" : 123,
    "shoes" : {
         "size_int" : 456,
         "brand_str" : "str",
         "addeddate_dt" : "2020/04/25 00:00:00"
    },
    "shirt" : {
         "size_int" : 123939,
         "brand_str" : "str",
         "addeddate_dt" : "2020/04/25 00:00:00",
         "color_str" : "red"
    }
}

然后

GET trivial_nestedness/_mapping

屈服

{
  "trivial_nestedness" : {
    "mappings" : {
      "dynamic_templates" : [
       ...
      ],
      "properties" : {
        "shirt" : {
          "properties" : {
            "addeddate_dt" : {
              "type" : "date",
              "format" : "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
            },
            "brand_str" : {
              "type" : "text"
            },
            "color_str" : {
              "type" : "text"
            },
            "size_int" : {
              "type" : "integer"
            }
          }
        },
        "shoes" : {
          "properties" : {
            "addeddate_dt" : {
              "type" : "date",
              "format" : "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
            },
            "brand_str" : {
              "type" : "text"
            },
            "size_int" : {
              "type" : "integer"
            }
          }
        },
        "userid_int" : {
          "type" : "long"
        }
      }
    }
  }
}

  1. 句法嵌套
PUT syntactic_nestedness
{
  "mappings": {
    "dynamic_templates": [
      {
        "possibly_nested_obj": {
          "match": "*_nested",
          "mapping": {
            "type": "nested"
          }
        }
      },
      {
        "int_as_integers": {
          "path_match": "*_nested.*_int",
          "mapping": {
            "type": "integer"
          }
        }
      },
      {
        "str_as_strings": {
          "path_match": "*_nested.*_str",
          "mapping": {
            "type": "text"
          }
        }
      }
    ]
  }
}

在这种情况下,每个shoes, shirt, ... 对象都有一个_nested后缀来清楚地表明其类型,更重要的是,它是一个可能加载的被认为是独立实体的项目的数组。

POST syntactic_nestedness/_doc
{
  "userid_int": 123,
  "shoes_nested": [
    {
      "size_int": 456,
      "brand_str": "str",
      "addeddate_dt": "2020/04/25 00:00:00"
    }
  ],
  "shirt_nested": [
    {
      "size_int": 123939,
      "brand_str": "str",
      "addeddate_dt": "2020/04/25 00:00:00",
      "color_str": "red"
    }
  ]
}

然后

GET syntactic_nestedness/_mapping

验证我们有真正的嵌套对象

{
  "syntactic_nestedness" : {
    "mappings" : {
      "dynamic_templates" : [
        ...
      ],
      "properties" : {
        "shirt_nested" : {
          "type" : "nested",
          "properties" : {
            "addeddate_dt" : {
              "type" : "date",
              "format" : "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
            },
            "brand_str" : {
              "type" : "text"
            },
            "color_str" : {
              "type" : "text"
            },
            "size_int" : {
              "type" : "integer"
            }
          }
        },
        "shoes_nested" : {
          "type" : "nested",
          "properties" : {
            "addeddate_dt" : {
              "type" : "date",
              "format" : "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
            },
            "brand_str" : {
              "type" : "text"
            },
            "size_int" : {
              "type" : "integer"
            }
          }
        },
        "userid_int" : {
          "type" : "long"
        }
      }
    }
  }
}

最后,虽然我选择_nested后缀主要是为了清楚起见,但我也想避免使用像userid_int. 不过,这可以通过您的密钥优雅地解决match patterns

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章