广告
返回顶部
首页 > 资讯 > 精选 >es如何创建索引和mapping
  • 829
分享到

es如何创建索引和mapping

2023-07-05 06:07:27 829人浏览 薄情痞子
摘要

本文小编为大家详细介绍“es如何创建索引和mapping”,内容详细,步骤清晰,细节处理妥当,希望这篇“es如何创建索引和mapping”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。es创建索引和mapping索

本文小编为大家详细介绍“es如何创建索引和mapping”,内容详细,步骤清晰,细节处理妥当,希望这篇“es如何创建索引和mapping”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

    es创建索引和mapping

    索引和type分开创建

    创建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"    }  }}

    创建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类型了

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

    读到这里,这篇“es如何创建索引和mapping”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注编程网精选频道。

    --结束END--

    本文标题: es如何创建索引和mapping

    本文链接: https://www.lsjlt.com/news/350050.html(转载时请注明来源链接)

    有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

    本篇文章演示代码以及资料文档资料下载

    下载Word文档到电脑,方便收藏和打印~

    下载Word文档
    猜你喜欢
    • es如何创建索引和mapping
      本文小编为大家详细介绍“es如何创建索引和mapping”,内容详细,步骤清晰,细节处理妥当,希望这篇“es如何创建索引和mapping”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。es创建索引和mapping索...
      99+
      2023-07-05
    • es创建索引和mapping的实例
      目录es创建索引和mapping索引和type分开创建索引和type一次创建更改elasticsearch中索引的mapping查看旧索引的mapping创建一个自定义mapping...
      99+
      2023-02-25
      es创建索引 es创建mapping es创建索引和mapping
    • es自动创建索引怎么实现
      在Elasticsearch中,可以通过以下几种方式自动创建索引: 动态映射(Dynamic Mapping):Elastics...
      99+
      2023-10-24
      es
    • 如何自动创建LOB索引段和重建索引
      这篇文章主要为大家展示了“如何自动创建LOB索引段和重建索引”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何自动创建LOB索引段和重建索引”这篇文章吧。LOB...
      99+
      2022-10-18
    • 如何使用Flask搭建ES搜索引擎
      本篇内容主要讲解“如何使用Flask搭建ES搜索引擎”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用Flask搭建ES搜索引擎”吧!1 配置文件Config.py#coding:utf-8...
      99+
      2023-06-16
    • MySql索引和索引创建策略
      目录1、B+树索引1.1、聚集索引/聚簇索引1.2、辅助索引/二级索引1.3、联合索引/复合索引1.3.1、什么是复合索引1.3.2、最左原则1.3.3、联合索引的查询优化2、哈希索引2.1、查看哈希索引的命中率等信息3...
      99+
      2022-08-22
    • mysql如何创建索引
      使用CREATE INDEX创建索引语法:CREATE [UNIQUE] INDEX index_name ONtb_name (col_name [(length)] ...
      99+
      2022-10-13
    • mysql 如何创建索引
      本文将介绍mysql 如何创建索引,需要的朋友可以参考下 添加PRIMARY KEY(主键索引) mysql>ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` ) 2.添加UNIQUE...
      99+
      2023-09-01
      mysql 数据库 sql
    • es索引多了如何解决
      当一个ES索引中的文档数量超过了ES集群的处理能力时,可以采取以下几种方法来解决: 垂直扩展:增加硬件资源,如增加更多的节点、更...
      99+
      2023-10-24
      es
    • 如何创建高效索引
      索引创建指南:1、频繁在where 从句中出现2、频繁在join关联字段中3、选择具有高选择性的键4、别在具有很少的不同值的键上使用B-tree索引。这类键或表达式经常具有较差选择性,所以不会是性...
      99+
      2022-10-18
    • mysql中如何创建索引
      这篇文章主要介绍mysql中如何创建索引,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!在执行CREATE TABLE语句时可以创建索引,也可以单独用CREATE INDEX或ALTER TABLE来为表增加索引。1、...
      99+
      2023-06-15
    • Oracle 中如何创建和管理索引
      这篇文章将为大家详细讲解有关Oracle 中如何创建和管理索引,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。  在 Oracle 数据库中,存储的每一行数据...
      99+
      2022-10-18
    • MySQL如何管理创建CREATE表和索引
      小编给大家分享一下MySQL如何管理创建CREATE表和索引,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!SQL语句:   数据库   表 ...
      99+
      2022-10-19
    • MySQL唯一索引如何创建
      要创建一个MySQL唯一索引,可以使用以下语法: ALTER TABLE table_name ADD UNIQUE INDEX...
      99+
      2023-10-27
      MySQL
    • 如何在MySQL中创建索引
      本篇文章为大家展示了如何在MySQL中创建索引,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。什么是索引?MySQL官方对索引的定义为:索引(Index)是帮助MyS...
      99+
      2022-10-18
    • Oracle如何创建分区索引
      这篇文章主要介绍了Oracle如何创建分区索引,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。 分区索引总结: 一,分区索引分为2类: 1、...
      99+
      2022-10-18
    • PHP如何创建索引数组
      这篇文章主要介绍了PHP如何创建索引数组,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。PHP 索引数组有两种创建索引数组的方法:索引是自动分...
      99+
      2022-10-19
    • 数据库如何创建索引
      数据库创建索引的方法打开需要操作的数据表。通过create index命令来添加索引。语法:CREATE [ UNIQUE ] [ CLUSTERED| NONCLUSTERED ] INDEX index_name ON { table ...
      99+
      2022-10-13
    • mysql复合索引如何创建
      在MySQL中,可以使用CREATE INDEX语句来创建复合索引。复合索引是基于多个列的索引,可以提高查询性能。 创建复合索引的语...
      99+
      2023-10-28
      mysql
    • python入门——DataFrame创建和索引
      DataFrame是Pandas中常用的数据结构,即表示矩阵的数据表,包含已排序的列集合,既有行索引又有列索引。使用前需先导入pandas(import pandas as pd)。 一、DataFrame的创建 1、利用包含等长度列表活N...
      99+
      2023-09-02
      python pandas
    软考高级职称资格查询
    编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
    • 官方手机版

    • 微信公众号

    • 商务合作