• 谷粒商城 119、全文检索ElasticSearch映射mapping


    Mapping(映射)是用来定义一个文档(document),以及它所包含的属性(field)是如何存储和索引的。比如:使用maping来定义:

    哪些字符串属性应该被看做全文本属性(full text fields);
    哪些属性包含数字,日期或地理位置;
    文档中的所有属性是否都能被索引(all 配置);
    日期的格式;
    自定义映射规则来执行动态添加属性;
    ——

    查看映射信息 GET bank/_mapping

    {
      "bank" : {
        "mappings" : {
          "properties" : {
            "account_number" : {
              "type" : "long"
            },
            "address" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "age" : {
              "type" : "long"
            },
            "balance" : {
              "type" : "long"
            },
            "city" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "email" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "employer" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "firstname" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "gender" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "lastname" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "state" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        }
      }
    }

    创建索引并指定映射

    PUT /my_index
    {
      "mappings": {
        "properties": {
          "age": {
            "type": "integer"
          },
          "email": {
            "type": "keyword"
          },
          "name": {
            "type": "text"
          }
        }
      }
    }

    GET /my_index

    {
      "my_index" : {
        "aliases" : { },
        "mappings" : {
          "properties" : {
            "age" : {
              "type" : "integer"
            },
            "email" : {
              "type" : "keyword"
            },
            "name" : {
              "type" : "text"
            }
          }
        },
        "settings" : {
          "index" : {
            "creation_date" : "1657784938885",
            "number_of_shards" : "1",
            "number_of_replicas" : "1",
            "uuid" : "y-42en76TGuPiXvRHBWJFQ",
            "version" : {
              "created" : "7090299"
            },
            "provided_name" : "my_index"
          }
        }
      }
    }

    如果需要给my_index索引添加一个新的字段employee-id,采用下面的方式会报错

    PUT /my_index
    {
      "mappings": {
        "properties": {
          "age": {
            "type": "integer"
          },
          "email": {
            "type": "keyword"
          },
          "name": {
            "type": "text"
          },
          
            "employee-id": {
          "type": "keyword",
          "index": false
        
          }
        }
      }
    }

    会报错下面的错误,我们不能直接在原理的基础上面进行修改

    {
      "error" : {
        "root_cause" : [
          {
            "type" : "resource_already_exists_exception",
            "reason" : "index [my_index/y-42en76TGuPiXvRHBWJFQ] already exists",
            "index_uuid" : "y-42en76TGuPiXvRHBWJFQ",
            "index" : "my_index"
          }
        ],
        "type" : "resource_already_exists_exception",
        "reason" : "index [my_index/y-42en76TGuPiXvRHBWJFQ] already exists",
        "index_uuid" : "y-42en76TGuPiXvRHBWJFQ",
        "index" : "my_index"
      },
      "status" : 400
    }

    添加新的字段映射

    PUT /my_index/_mapping
    {
      "properties": {
        "employee-id": {
          "type": "keyword",
          "index": false
        }
      }
    }

    添加成功之后,我们再来看索引

    {
      "my_index" : {
        "aliases" : { },
        "mappings" : {
          "properties" : {
            "age" : {
              "type" : "integer"
            },
            "email" : {
              "type" : "keyword"
            },
            "employee-id" : {
              "type" : "keyword",
              "index" : false
            },
            "name" : {
              "type" : "text"
            }
          }
        },
        "settings" : {
          "index" : {
            "creation_date" : "1657784938885",
            "number_of_shards" : "1",
            "number_of_replicas" : "1",
            "uuid" : "y-42en76TGuPiXvRHBWJFQ",
            "version" : {
              "created" : "7090299"
            },
            "provided_name" : "my_index"
          }
        }
      }
    }

    上面这个"index" : false指定了index为false,表示这个字段是不能被搜索的,这里要注意,默认的字段都是能够被检索的

    更新映射

    对于已经存在的现场映射,我们不能更新。必须进行更新创建新的索引,数据迁移。

    数据迁移

    先创建 new_twitter 的正确映射。然后使用如下方式进行数据迁移。

     

    更多详情见:https://www.elastic.co/guide/en/elasticsearch/reference/7.6/docs-reindex.html

     上面默认的bank索引未long类型,如果要需该log类型为整形,只能对于已经存在的现场映射,我们不能更新。必须进行更新创建新的索引,数据迁移。

    我们先创建一个新的索引,只能age类型为整形

    将年龄修改为整数

    PUT /newbank
    {
      "mappings": {
        "properties": {
          "account_number": {
            "type": "long"
          },
          "address": {
            "type": "text"
          },
          "age": {
            "type": "integer"
          },
          "balance": {
            "type": "long"
          },
          "city": {
            "type": "keyword"
          },
          "email": {
            "type": "keyword"
          },
          "employer": {
            "type": "keyword"
          },
          "firstname": {
            "type": "text"
          },
          "gender": {
            "type": "keyword"
          },
          "lastname": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "state": {
            "type": "keyword"
          }
        }
      }
    }

    查看“newbank”的映射:

    获取 /newbank/_mapping

    可以看到年龄的映射类型被修改为整数。

    将银行中的数据迁移到新银行中

    POST _reindex
    {
      "source": {
        "index": "bank",
        "type": "account"
      },
      "dest": {
        "index": "newbank"
      }
    }

    执行效果如下

    查看newbank中的数据

    type类型

    在原理的back索引中,我们创建了bank索引的时候,我们指定了类型为account,银行账户

     在7.0以后创新索引,不需要指定type类型,取消了索引的type类型,默认一个index只有一个type

    索引文档时,使用  _doc来代替type

     为啥了:比如之前一个bank索引下面存在银行account的类型,也存在shool的类型,两个类型下面都有相同name这个字段,这样在索引中会存在冲突,索引es7以后,取消的索引的type类型,默认的类型为_doc类型

    elasticsearch 是基于 Lucene 开发的搜索引擎,而 ES 中不同类型下名称相同的归档最终在 Lucene 中的处理方式是一样的

    • 两个不同类型下的两个用户名,在 ES 同目录下实际上被认为是同一个文件,你在两个不同类型中定义相同的文件必须提交一个。或者,不同类型中的字段名称必须相同在处理中出现冲突的情况,导致Lucene处理效率下降。

    所以上面这幅图中国的类型就被去掉了

    POST _reindex{  "source": {    "index": "bank",    "type": "account"  },  "dest": {    "index": "newbank"  }}
  • 相关阅读:
    1.2 软件测试的分类和职业生涯
    1.1:软件测试的发展
    1,select查询详解
    7、网页
    6、开学典礼
    5、边框属性和display
    4、盒子模型和margin、padding
    3、字体、背景、和文本的属性
    2、Css中的样式选择器
    16. C# 对象初始化器
  • 原文地址:https://www.cnblogs.com/kebibuluan/p/16478118.html
Copyright © 2020-2023  润新知