返回顶部
首页 > 资讯 > 精选 >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://lsjlt.com/news/350050.html(转载时请注明来源链接)

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

    猜你喜欢
    • 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
    • MySQL索引如何创建和使用索引
      创建MySQL索引可以通过以下两种方式: 使用CREATE INDEX语句创建索引: CREATE INDEX index_na...
      99+
      2024-03-06
      MySQL
    • es自动创建索引怎么实现
      在Elasticsearch中,可以通过以下几种方式自动创建索引: 动态映射(Dynamic Mapping):Elastics...
      99+
      2023-10-24
      es
    • 如何自动创建LOB索引段和重建索引
      这篇文章主要为大家展示了“如何自动创建LOB索引段和重建索引”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何自动创建LOB索引段和重建索引”这篇文章吧。LOB...
      99+
      2024-04-02
    • mysql如何创建索引
      使用CREATE INDEX创建索引语法:CREATE [UNIQUE] INDEX index_name ONtb_name (col_name [(length)] ...
      99+
      2024-04-02
    • mysql 如何创建索引
      本文将介绍mysql 如何创建索引,需要的朋友可以参考下 添加PRIMARY KEY(主键索引) mysql>ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` ) 2.添加UNIQUE...
      99+
      2023-09-01
      mysql 数据库 sql
    • oracle如何创建索引
      oracle 中创建索引需遵循以下步骤:确定表和索引列。使用 create index 语句,指定索引名称、表名称和列名称。指定索引类型(默认 b-tree),并可添加 unique、p...
      99+
      2024-06-12
      oracle
    • mysql索引如何创建
      mysql索引创建指南:确定要索引的列:常被搜索或排序的列。选择索引类型:b-tree索引(范围查询)、哈希索引(等值查询)、全文索引(文本搜索)、空间索引(地理空间搜索)。命名索引:指...
      99+
      2024-08-01
      mysql mysql索引
    • 如何使用Flask搭建ES搜索引擎
      本篇内容主要讲解“如何使用Flask搭建ES搜索引擎”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用Flask搭建ES搜索引擎”吧!1 配置文件Config.py#coding:utf-8...
      99+
      2023-06-16
    • Oracle 中如何创建和管理索引
      这篇文章将为大家详细讲解有关Oracle 中如何创建和管理索引,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。  在 Oracle 数据库中,存储的每一行数据...
      99+
      2024-04-02
    • Couchbase中如何创建和管理索引
      在Couchbase中,可以使用N1QL(Couchbase Query Language)来创建和管理索引。以下是创建和管理索引的...
      99+
      2024-04-09
      Couchbase
    • MySQL中如何创建和优化索引
      在MySQL中,可以使用CREATE INDEX语句来创建索引。例如,要在名为table_name的表中创建一个名为index_na...
      99+
      2024-04-09
      MySQL
    • 如何创建高效索引
      索引创建指南:1、频繁在where 从句中出现2、频繁在join关联字段中3、选择具有高选择性的键4、别在具有很少的不同值的键上使用B-tree索引。这类键或表达式经常具有较差选择性,所以不会是性...
      99+
      2024-04-02
    • mysql中如何创建索引
      这篇文章主要介绍mysql中如何创建索引,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!在执行CREATE TABLE语句时可以创建索引,也可以单独用CREATE INDEX或ALTER TABLE来为表增加索引。1、...
      99+
      2023-06-15
    • PostgreSQL中如何创建索引
      在 PostgreSQL 中,可以使用以下语法来创建索引: 1、创建一个基本索引: CREATE INDEX index_name ...
      99+
      2024-04-02
    • es索引多了如何解决
      当一个ES索引中的文档数量超过了ES集群的处理能力时,可以采取以下几种方法来解决: 垂直扩展:增加硬件资源,如增加更多的节点、更...
      99+
      2023-10-24
      es
    • MySQL如何管理创建CREATE表和索引
      小编给大家分享一下MySQL如何管理创建CREATE表和索引,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!SQL语句:   数据库   表 ...
      99+
      2024-04-02
    • 如何在MariaDB中创建和管理索引
      在MariaDB中,可以使用CREATE INDEX语句来创建索引,可以使用ALTER TABLE语句来添加、删除或修改索引。以下是...
      99+
      2024-03-15
      MariaDB
    • Oracle如何创建分区索引
      这篇文章主要介绍了Oracle如何创建分区索引,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。 分区索引总结: 一,分区索引分为2类: 1、...
      99+
      2024-04-02
    软考高级职称资格查询
    编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
    • 官方手机版

    • 微信公众号

    • 商务合作