索引库就类似数据库表,mapping映射就类似表的结构。
我们要向es中存储数据,必须先创建“库”和“表”

在Kibana里面的Dev Tools里面写的那些东西具体怎么写的,有什么含义,可以往下看

mapping映射属性

mapping是对索引库中文档的约束,常见的mapping属性包括:

  • type:字段数据类型,常见的简单类型有:
    • 字符串:text(可分词的文本)、keyword(精确值,例如:品牌、国家、ip地址)
    • 数值:longintegershortbytedoublefloat
    • 布尔:boolean
    • 日期:date
    • 对象:object
  • index:是否创建索引,默认为true
  • analyzer:使用哪种分词器
  • properties:该字段的子字段

    创建索引库和映射

  • 请求方式:PUT

  • 请求路径:/索引库名,可以自定义
  • 请求参数:mapping映射
    1. PUT /索引库名称
    2. {
    3. "mappings": {
    4. "properties": {
    5. "字段名":{
    6. "type": "text",
    7. "analyzer": "ik_smart"
    8. },
    9. "字段名2":{
    10. "type": "keyword",
    11. "index": "false"
    12. },
    13. "字段名3":{
    14. "properties": {
    15. "子字段": {
    16. "type": "keyword"
    17. }
    18. }
    19. },
    20. // ...略
    21. }
    22. }
    23. }
    image.png

    查询索引库

    1. GET /索引库名
    image.png

    修改索引库

    倒排索引结构虽然不复杂,但是一旦数据结构改变(比如改变了分词器),就需要重新创建倒排索引,这简直是灾难。因此索引库一旦创建,无法修改mapping
    虽然无法修改mapping中已有的字段,但是却允许添加新的字段到mapping中,因为不会对倒排索引产生影响。
    1. PUT /索引库名/_mapping
    2. {
    3. "properties": {
    4. "新字段名": {
    5. "type": "integer"
    6. }
    7. }
    8. }
    image.png

    删除索引库

    1. DELETE /索引库名
    image.png