mapping类似于数据库中的schema,作用如下:

  1. 定义索引中的字段类型;
    2. 定义字段的数据类型,例如:布尔、字符串、数字、日期…..
    3. 字段倒排索引的设置

mapping定义设置

  1. ##获取索引
  2. GET /_cat/indices
  3. ##设置索引mapping
  4. PUT users
  5. {
  6. "mappings": {
  7. // define your mappings here
  8. }
  9. }

定义mapping的建议方式: 写入一个样本文档到临时索引中,ES会自动生成mapping信息,通过访问
mapping信息的api查询mapping的定义,修改自动生成的mapping成为我们需要方式,创建索引,删
除临时索引,简而言之就是 “卸磨杀驴”
mapping索引创建后不能修改,所以设置后,如果有变化,通常都是删除从新创建,
设置时,一定要设置好mapping,尽量不要对mapping进行改变
例如:
image.pngimage.png

{
  "mappings" : {
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "height" : {
          "type" : "long"
        },
        "isRich" : {
          "type" : "boolean"
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
}

keyword
字符串, Keyword的意思是字符串的内容不会被分词处理,输入是什么内容,存储在ES中就是什么内容。Text类型ES会自动的添加一个Keyword类型的子字段

#设置索引mapping
PUT /commodity/?pretty
{
  "settings": {
    "number_of_shards": 5,
    "number_of_replicas": 2
  },
  "mappings": {
    "properties": {
    "brandid": {
      "type": "long"
    },
    "brandname": {
      "type": "text",
      "analyzer": "ik_smart",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "commentNum": {
      "type": "long"
    },
    "creationtime": {
      "type": "date",
       "format": "yyyy-MM-dd HH:mm:ss"
    },
    "discount": {
      "type": "long"
    },
    "filteritem": {
      "type": "text",
      "analyzer": "ik_max_word",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "id": {
      "type": "long"
    },
    "image": {
      "type": "keyword",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "imagelist": {
      "type": "keyword",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "introduces": {
      "type": "keyword",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "names": {
      "type": "text",
      "analyzer": "ik_smart",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "nums": {
      "type": "long"
    },
    "oneid": {
      "type": "long"
    },
    "prices": {
      "type": "long"
    },
    "recommend": {
      "type": "long"
    },
    "specifications": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "status": {
      "type": "long"
    },
    "supplierid": {
      "type": "long"
    },
    "supplieridname": {
      "type": "text",
      "analyzer": "ik_smart",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "tFilteritems": {
      "properties": {
        "id": {
          "type": "long"
        },
        "names": {
          "type": "text",
          "analyzer": "ik_smart",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "params": {
          "type": "object"
        },
        "pid": {
          "type": "long"
        },
        "status": {
          "type": "integer"
        },
        "tKeywordList": {
          "properties": {
            "id": {
              "type": "long"
            },
            "names": {
              "type": "text",
              "analyzer": "ik_smart",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "params": {
              "type": "object"
            },
            "pid": {
              "type": "long"
            }
          }
        }
      }
    },
    "tSpecifications": {
      "properties": {
        "creationtime": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss"
        },
        "id": {
          "type": "long"
        },
        "names": {
          "type": "text",
          "analyzer": "ik_smart",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "number": {
          "type": "long"
        },
        "prices": {
          "type": "long"
        },
        "scribingprice": {
          "type": "long"
        },
        "stocknum": {
          "type": "long"
        }
      }
    },
    "threeid": {
      "type": "long"
    },
    "transactionnum": {
      "type": "long"
    },
    "twoid": {
      "type": "long"
    },
    "types": {
      "type": "integer"
    }
  }
  }
}

如果原始索引内数据很多,需要修改原索引mapping,可以采用新建mapping,然后将原数据对拷至新索引内,脚本样例:


#es 数据索引对拷
POST _reindex
{
  "source": {
    "index": "recommended-20220112"
  },
  "dest": {
    "index":"recommended-20220117"
  }
}