es创建索引和mapping的实例

 更新时间:2023年02月25日 16:55:16   作者:goxingman  
这篇文章主要介绍了es创建索引和mapping的实例,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

es创建索引和mapping

索引和type分开创建

1、创建index

http://127.0.0.1:9200/
negative/    put
{
  "settings": {
    "index": {
      "search": {
        "slowlog": {
          "threshold": {
            "fetch": {
              "debug": "5s"
            },
            "query": {
              "warn": "20s"
            }
          }
        }
      },
      "indexing": {
        "slowlog": {
          "threshold": {
            "index": {
              "info": "20s"
            }
          }
        }
      },
      "number_of_shards": "1",
      "number_of_replicas": "0"
    }
  }
}

2、创建mapping

http://127.0.0.1:9200/
negative/negative/_mapping  post
{"properties":{
  "id": {
    "type": "long"
  },
  "yjlb": {
    "type": "text",
    "fields": {
      "keyword": {
        "type": "keyword",
        "ignore_above": 256
      }
    }
  },
  "ejlb": {
    "type": "text",
    "fields": {
      "keyword": {
        "type": "keyword",
        "ignore_above": 256
      }
    }
  },
  "sjlb": {
    "type": "text",
    "fields": {
      "keyword": {
        "type": "keyword",
        "ignore_above": 256
      }
    }
  },
  "detail": {
    "type": "text",
    "fields": {
      "keyword": {
        "type": "keyword",
        "ignore_above": 256
      }
    }
  },
  "ssyj": {
    "type": "text",
    "fields": {
      "keyword": {
        "type": "keyword",
        "ignore_above": 256
      }
    }
  }
}}

索引和type一次创建

(注意:mapping下面一层的key值 是type名称)

http://192.168.0.213:9200/
announcement/    put
{
  "settings": {
    "index": {
      "search": {
        "slowlog": {
          "threshold": {
            "fetch": {
              "debug": "5s"
            },
            "query": {
              "warn": "20s"
            }
          }
        }
      },
      "indexing": {
        "slowlog": {
          "threshold": {
            "index": {
              "info": "20s"
            }
          }
        }
      },
      "number_of_shards": "1",
      "number_of_replicas": "0"
    }
  },
  "mappings": {
    "announcement": {
      "properties": {
        "id": {
          "type": "keyword"
        },
        "createtime": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        },
        "creatby": {
          "type": "keyword"
        },
        "updatetime": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        },
        "type": {
          "type": "keyword"
        },
        "status": {
          "type": "keyword"
        },
        "title": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "cont": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "files": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "keyword"
            },
            "filename": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            }
          }
        }
      }
    }
  }
}

更改elasticsearch中索引的mapping

昨天研发说在kibana中统计userid字段不出图,后来查到该字段显示冲突了,然后再查看了GET test/_mapping下该索引的mapping,发现userid是long类型的,而userid.keyword是string类型的,出现这种情况的根本原因是日志中这个字段存的是数值类型的值,改成字符串类型即可,由于急着用,我司上线一般是下午6点30上线,所以临时修改了下该字段的类型,步骤如下:

查看旧索引的mapping

  • GET test/_mapping 

找到userid这个字段,修改类型为keyword,如下:

{
    "mappings": {
        "doc": {
            "properties": {
                "@timestamp": {
                    "type": "date"
                },
                "@version": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "beat": {
                    "properties": {
                        "hostname": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "name": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "version": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                },
                "code": {
                    "type": "long"
                },
                "dip": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "fields": {
                    "properties": {
                        "log_topic": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                },
                "host": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "message": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "method": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "offset": {
                    "type": "long"
                },
                "referer": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "sip": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "source": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "tags": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "time": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "url": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "userid": {
                    "type": "keyword"   #修改此处
                }
            }
        }
    }
}

创建一个自定义mapping的新索引

PUT test-new
{
    "mappings": {
        "doc": {
            "properties": {
                "@timestamp": {
                    "type": "date"
                },
                "@version": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "beat": {
                    "properties": {
                        "hostname": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "name": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "version": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                },
                "code": {
                    "type": "long"
                },
                "dip": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "fields": {
                    "properties": {
                        "log_topic": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                },
                "host": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "message": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "method": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "offset": {
                    "type": "long"
                },
                "referer": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "sip": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "source": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "tags": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "time": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "url": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "userid": {
                    "type": "keyword"
                }
            }
        }
    }
}

把旧索引的数据reindex到新索引上

注意,旧索引先停止新数据的写入

POST _reindex
{
  "source": {
    "index": "test"
  },
  "dest": {
    "index": "test-new"
  }
}

删除旧索引

DELETE test

按照步骤2创建test索引

PUT test
{
    "mappings": {
        "doc": {
            "properties": {
                "@timestamp": {
                    "type": "date"
                },
                "@version": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "beat": {
                    "properties": {
                        "hostname": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "name": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "version": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                },
                "code": {
                    "type": "long"
                },
                "dip": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "fields": {
                    "properties": {
                        "log_topic": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                },
                "host": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "message": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "method": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "offset": {
                    "type": "long"
                },
                "referer": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "sip": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "source": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "tags": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "time": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "url": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "userid": {
                    "type": "keyword"
                }
            }
        }
    }
}

把test-new索引的数据reindex到test索引上

POST _reindex
{
  "source": {
    "index": "test-new"
  },
  "dest": {
    "index": "test"
  }
}

查看test索引的mapping

GET test/_mapping,执行命令后,可以看到userid的字段类型为keyword类型了

然后再打开该索引接收新数据的开关

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Spring Security的持久化用户和授权实现方式

    Spring Security的持久化用户和授权实现方式

    文章介绍了如何使用JdbcUserDetailsManager实现数据库读取用户,并展示了如何配置SpringSecurity进行授权管理,通过创建数据库表、配置数据库连接和修改SecurityConfig,实现了用户权限的控制
    2025-02-02
  • vue 使用vuex在页面跳转的实现方式

    vue 使用vuex在页面跳转的实现方式

    这篇文章主要介绍了vue 使用vuex在页面跳转的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • MyBatis-Plus 主键生成策略的几种实现方式

    MyBatis-Plus 主键生成策略的几种实现方式

    主键生成策略是指在数据库中为每条记录生成唯一标识符的方法,本文就来介绍一下MyBatis-Plus 主键生成策略的实现,具有一定的参考价值,感兴趣的可以了解一下
    2024-05-05
  • 详解Java的面向对象和面向过程

    详解Java的面向对象和面向过程

    我们知道,Java是面向对象的编程语言,但是至今仍有不少同学还不太懂”面向对象“的确切含义,今天这篇文章就带大家真正地认识面向对象,感兴趣的同学可以跟着小编一起来学习
    2023-05-05
  • MyBatisPlus报错:Failed to process,please exclude the tableName or statementId问题

    MyBatisPlus报错:Failed to process,please exclud

    这篇文章主要介绍了MyBatisPlus报错:Failed to process,please exclude the tableName or statementId问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • Java异常(Exception)处理以及常见异常总结

    Java异常(Exception)处理以及常见异常总结

    在《Java编程思想》中这样定义异常,阻止当前方法或作用域继续执行的问题,虽然java中有异常处理机制,但是要明确一点,决不应该用"正常"的态度来看待异常,这篇文章主要给大家介绍了关于Java异常(Exception)处理以及常见异常的相关资料,需要的朋友可以参考下
    2021-10-10
  • InputStreamReader和BufferedReader用法及实例讲解

    InputStreamReader和BufferedReader用法及实例讲解

    这篇文章主要介绍了InputStreamReader和BufferedReader用法及实例讲解的相关资料,需要的朋友可以参考下
    2015-12-12
  • springboot+mybatis-plus实现内置的CRUD使用详解

    springboot+mybatis-plus实现内置的CRUD使用详解

    这篇文章主要介绍了springboot+mybatis-plus实现内置的CRUD使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • java 字符串相减(很简单的一个方法)

    java 字符串相减(很简单的一个方法)

    本篇文章是对java中关于字符串相减的一个简单的方法进行了介绍,需要的朋友参考下
    2013-07-07
  • java如何用正则表达式匹配与提取字符串

    java如何用正则表达式匹配与提取字符串

    一位以前的同事在群里面突然发了个需求,要通过正则表达式来取值,下面这篇文章主要给大家介绍了关于java如何用正则表达式匹配与提取字符串的相关资料,需要的朋友可以参考下
    2022-06-06

最新评论