当前位置:   article > 正文

ElasticSearch索引操作入门_es创建索引命令

es创建索引命令

目录

一、索引创建

二、查看索引

1、查看所有索引

2、查看单个索引

三、删除索引

四、映射关系

1、先创建一个索引

2、创建映射

2.1、创建映射

2.2、创建映射设置分片,不设置会默认一个主分片一个备份分片

2.3、ignore_above限定字符长度

2.4、doc_values 属性

2.5、fielddata属性

3、settings中定义索引库的各种属性

3.1、refresh_interval

3.2、max_result_window

3.3、动态映射

五、增加数据

六、简单查询

6.1、查找name含有”小“数据

6.2、查找sex含有”男“数据:

6.3、查询电话


一、索引创建

PUT /shopping

二、查看索引

1、查看所有索引

GET /_cat/indices?v

 

  1. 返回结果:
  2. health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
  3. green open .geoip_databases 0CG2YlaPRoWzyYDoI_qrnw 1 0 40 41 37.9mb 37.9mb
  4. green open .kibana_task_manager_7.16.2_001 8gMpU8TgSquEjIogLYDx1Q 1 0 18 1009 379.5kb 379.5kb
  5. green open .apm-custom-link tdDMOb-6Q72Q0teB9gNOTQ 1 0 0 0 226b 226b
  6. green open .kibana_task_manager_1 GJZl69z0SKuI9RYywKzvdA 1 0 2 1 55.2kb 55.2kb
  7. green open .kibana_7.16.2_001 -XLPL15WSiCe58OIVby1UA 1 0 31 42 4.7mb 4.7mb
  8. green open .apm-agent-configuration A1f3pYIEQiOp2aNuIUWltA 1 0 0 0 226b 226b
  9. green open .kibana_1 InKzLmjZQlK_po5a_8VkLQ 1 0 2 0 10.1kb 10.1kb
  10. yellow open event_detail_2022 zBGxQH8qS7qoExmZEXL3fw 1 1 29 0 9.9kb 9.9kb
  11. yellow open shopping Djjkp-sNQPedDpKZ6cSZRg 1 1 0 0 226b 226b
  12. green open .tasks 3DxEBvwzTBSul8tzqh8BmQ 1 0 4 0 27.4kb 27.4kb
  1. 表头 含义
  2. health 当前服务器健康状态: green(集群完整) yellow(单点正常、集群不完整) red(单点不正常)
  3. status 索引打开、关闭状态
  4. index 索引名
  5. uuid 索引统一编号
  6. pri 主分片数量
  7. rep 副本数量
  8. docs.count 可用文档数量
  9. docs.deleted 文档删除状态(逻辑删除)
  10. store.size 主分片和副分片整体占空间大小
  11. pri.store.size 主分片占空间大小

2、查看单个索引

GET /shopping

三、删除索引

DELETE /shopping

四、映射关系

有了索引库,等于有了数据库中的 database。

接下来就需要建索引库(index)中的映射了,类似于数据库(database)中的表结构(table)。

创建数据库表需要设置字段名称,类型,长度,约束等;索引库也一样,需要知道这个类型下有哪些字段,每个字段有哪些约束信息,这就叫做映射(mapping)。

1、先创建一个索引

PUT /user


2、创建映射

2.1、创建映射

  1. PUT /user/_mapping
  2. {
  3. "properties": {
  4. "name":{
  5. "type": "text",
  6. "index": true
  7. },
  8. "sex":{
  9. "type": "keyword",
  10. "index": true
  11. },
  12. "tel":{
  13. "type": "keyword",
  14. "index": false
  15. }
  16. }
  17. }

 

2.2、创建映射设置分片,不设置会默认一个主分片一个备份分片

  • number_of_shards:分片的数量;(主分片)
  • number_of_replicas:副本的数量;(备份分片)
  • 一般配置,副本数少可以提高性能
  1. 在mappings中,我们设置索引的字段,
  2. 在这里,我们只设置了id和name,id的映射类型是long,name的映射类型是text。
  3. settings:就是索引库设置,其中可以定义索引库的各种属性,
  4. 目前我们可以不设置,都走默认
  5. PUT /stu
  6. {
  7. "settings":{
  8. "number_of_shards":5,
  9. "number_of_replicas":1
  10. },
  11. "mappings":{
  12. "properties":{
  13. "id":{
  14. "type":"long"
  15. },
  16. "name":{
  17. "type":"text"
  18. }
  19. }
  20. }
  21. }

2.3、ignore_above限定字符长度

1、创建 mapping 时,可以为字符串(专指 keyword) 指定 ignore_above ,用来限定字符长度。超过 ignore_above 的字符会被存储,但不会被索引。

注意,是字符长度,一个英文字母是一个字符,一个汉字也是一个字符。

在动态生成的 mapping 中,keyword类型会被设置ignore_above: 256。

ignore_above 可以在创建 mapping 时指定。
 

  1. PUT my_index
  2. {
  3. "mappings" : {
  4. "properties" : {
  5. "note" : {
  6. "type" : "keyword",
  7. "ignore_above": 4
  8. }
  9. }
  10. }
  11. }

2、ignore_above扩展

  1. POST _bulk
  2. {"index":{"_index":"my_index","_id":"1"}}
  3. {"note":"一二三"}
  4. {"index":{"_index":"my_index","_id":"2"}}
  5. {"note":"一二三四"}
  6. {"index":{"_index":"my_index","_id":"3"}}
  7. {"note":"一二三四五"}
  8. #用下面的查询验证 ignore_above
  9. # 能查到数据
  10. GET my_index/_search
  11. {
  12. "query": {
  13. "match": {
  14. "note": "一二三四"
  15. }
  16. }
  17. }
  18. # 不能查到数据 ignore_above限定长度为4,超过的不会被索引
  19. GET my_index/_search
  20. {
  21. "query": {
  22. "match": {
  23. "note": "一二三四五"
  24. }
  25. }
  26. }

 

3、修改 ignore_above 改大改小都行,但只对新数据有效

  1. PUT my_index/_mappings
  2. {
  3. "properties" : {
  4. "note" : {
  5. "type" : "keyword",
  6. "ignore_above": 2
  7. }
  8. }
  9. }

2.4、doc_values 属性

字段的 doc_values 属性有两个值, true、false。默认为 true ,即开启。

当 doc_values 为 fasle 时,无法基于该字段排序、聚合、在脚本中访问字段值。

当 doc_values 为 true 时,ES 会增加一个相应的正排索引,这增加的磁盘占用,也会导致索引数据速度慢一些。

 

  1. #新建索引
  2. PUT student6
  3. {
  4. "mappings" : {
  5. "properties" : {
  6. "name" : {
  7. "type" : "keyword",
  8. "doc_values": false
  9. },
  10. "age" : {
  11. "type" : "integer",
  12. "doc_values": false
  13. }
  14. }
  15. }
  16. }
  17. #插入数据
  18. POST _bulk
  19. { "index" : { "_index" : "student6", "_id" : "1" } }
  20. { "name" : "张三", "age": 12 }
  21. { "index" : { "_index" : "student6", "_id" : "2" } }
  22. { "name" : "李四", "age": 10 }
  23. { "index" : { "_index" : "student6", "_id" : "3" } }
  24. { "name" : "王五", "age": 11 }
  25. #查询数据并按照age排序,会报错
  26. POST student6/_search
  27. {
  28. "query": {
  29. "match_all": {}
  30. },
  31. "sort" : [
  32. {"age": {"order": "desc"}}
  33. ],
  34. "from": 0,
  35. "size": 1
  36. }
  37. #获取 age 平均值,会报错
  38. POST student6/_search
  39. {
  40. "aggs":{
  41. "age_stat": {
  42. "avg": {"field": "age"}
  43. }
  44. },
  45. "from": 0
  46. }

2.5、fielddata属性

在ES中,text类型的字段使用一种叫做fielddata的查询时内存数据结构。当字段被排序,聚合或者通过脚本访问时这种数据结构会被创建。它是通过从磁盘读取每个段的整个反向索引来构建的,然后存存储在java的堆内存中。

fileddata默认是不开启的。Fielddata可能会消耗大量的堆空间,尤其是在加载高基数文本字段时。一旦fielddata已加载到堆中,它将在该段的生命周期内保留。此外,加载fielddata是一个昂贵的过程,可能会导致用户遇到延迟命中。这就是默认情况下禁用fielddata的原因。

  1. PUT shopping3
  2. POST _bulk
  3. { "index" : { "_index" : "shopping3", "_id" : "1" } }
  4. { "category" : "大米", "price": 12}
  5. { "index" : { "_index" : "shopping3", "_id" : "2" } }
  6. { "category" : "黄豆", "price": 10 }
  7. { "index" : { "_index" : "shopping3", "_id" : "3" } }
  8. { "category" : "大米", "price": 12 }
  9. #如果尝试对文本字段进行排序,聚合或脚本访问,会报异常
  10. GET /shopping3/_search
  11. {
  12. "aggs":{
  13. "price_group":{
  14. "terms":{
  15. "field":"category"
  16. }
  17. }
  18. }}

如果要对分词的field执行聚合操作,必须将fielddata设置为true

  1. #先查看mapping,mapping字段类型与下面命令查出的类型一致才可操作
  2. GET /shopping3
  3. PUT shopping3/_mappings
  4. {
  5. "properties" : {
  6. "category" : {
  7. "type" : "text",
  8. "fielddata": true
  9. }
  10. }
  11. }
  12. GET /shopping3/_search
  13. {
  14. "aggs":{
  15. "price_group":{
  16. "terms":{
  17. "field":"category"
  18. }
  19. }
  20. }}
  21. 此时聚合查询正常

3、settings中定义索引库的各种属性

  1. #先建一个索引
  2. PUT /stu2
  3. {
  4. "settings": {
  5. "index": {
  6. "refresh_interval": "5s",
  7. "number_of_shards": "5",
  8. "translog": {
  9. "sync_interval": "5s",
  10. "durability": "async"
  11. },
  12. "max_result_window": "65536",
  13. "number_of_replicas": "1"
  14. }
  15. },
  16. "mappings":{
  17. "properties":{
  18. "id":{
  19. "type":"long"
  20. },
  21. "name":{
  22. "type":"text"
  23. }
  24. }
  25. }
  26. }

3.1、refresh_interval

1、当数据添加到索引后并不能马上被查询到,等到索引刷新后才会被查询到。 refresh_interval 配置的刷新间隔。refresh_interval 的默认值是 1s

单位:ms: 毫秒 s: 秒 m: 分钟 如果是指定的纯数字,单位是毫秒

2、当 refresh_interval 为 -1 时,意味着不刷新索引。

3、当需要大量导入数据到ES中,可以将 refresh_interval 设置为 -1 以加快导入速度。导入结束后,再将 refresh_interval 设置为一个正数,例如1s。或者手动 refresh 索引。

4、将某索引的 refresh_interval 设置为 1分钟

  1. PUT stu2/_settings
  2. {
  3. "index" : {
  4. "refresh_interval" : "1m"
  5. }
  6. }

5、添加数据时忽略 refresh_interval 配置,直接触发刷新索引(一般用不到)

  1. POST stu2/_doc?refresh
  2. {
  3. "name": "张三2"
  4. }

6、重置 refresh_interval

  1. PUT /stu2/_settings
  2. {
  3. "index" : {
  4. "refresh_interval" : null
  5. }
  6. }

3.2、max_result_window

es对from + size的大小进行限制,必须小于等于10000

因为es的优势在于搜索,不在于分页,对此做限制(10000)就是为不影响其性能,业务有需求可以按照下面方法设置放大:

  1. PUT /stu2/_settings
  2. {
  3. "index":{
  4. "max_result_window":100000000
  5. }
  6. }

3.3、动态映射

dynamic用来配置处理新出现字段的行为,有true,false,strict 三种。

true 是默认值,会自动在 mapping 中添加字段,并在文档中保存新字段的值。

false 代表不会在 mapping 中添加字段,但会将数据存起来。

strict 代表既不会新增字段,也不会保存新字段的内容,遇到新字段直接报错。 另外,对于嵌套类型的字段,可以单独设置。

默认情况下,我们向索引中写入数据,如果有些字段之前未定义过,ES会自动猜测其类型,然后在mapping中生成。

  1. #新建索引
  2. PUT student3
  3. {
  4. "mappings": {
  5. "properties": {
  6. "name": {
  7. "type": "keyword",
  8. "doc_values": false
  9. }
  10. }
  11. },
  12. "settings": {
  13. "number_of_shards": "8",
  14. "number_of_replicas": "2"
  15. }
  16. }
  17. #批量插入
  18. POST _bulk
  19. { "index" : { "_index" : "student3", "_id" : "1" } }
  20. { "name" : "张三", "age": 12 }
  21. #查询
  22. GET student3/_search

查询结果有 age

GET student3/_mapping

age 出现在了 mapping 中,且自动判断为 long 类型。

有时,自动判断的类型可能是不符合我们的预期的,但是呢,类型一旦确定,又是不能更改的。所以我们要谨慎对待这种特性。

dynamic 可以用来配置相关行为,如下配置:

  1. PUT student4
  2. {
  3. "mappings": {
  4. "dynamic": "false",
  5. "properties": {
  6. "name": {
  7. "type": "keyword",
  8. "doc_values": false
  9. }
  10. }
  11. },
  12. "settings": {
  13. "number_of_shards": "8",
  14. "number_of_replicas": "2"
  15. }
  16. }
  17. PUT student5
  18. {
  19. "mappings": {
  20. "dynamic": "strict",
  21. "properties": {
  22. "name": {
  23. "type": "keyword",
  24. "doc_values": false
  25. }
  26. }
  27. },
  28. "settings": {
  29. "number_of_shards": "8",
  30. "number_of_replicas": "2"
  31. }
  32. }

五、增加数据

  1. POST /user/_doc/1001
  2. {
  3. "name":"小米",
  4. "sex":"男的",
  5. "tel":"1111"
  6. }

六、简单查询

6.1、查找name含有”小“数据

  1. GET /user/_search
  2. {
  3. "query":{
  4. "match":{
  5. "name":"小"
  6. }
  7. }
  8. }

6.2、查找sex含有”男“数据:

找不想要的结果,只因创建映射时"sex"的类型为"keyword"。

"sex"只能完全为”男的“,才能得出原数据。

  1. GET /user/_search
  2. {
  3. "query":{
  4. "match":{
  5. "sex":"男"
  6. }
  7. }
  8. }
  9. GET /user/_search
  10. {
  11. "query":{
  12. "match":{
  13. "sex":"男的"
  14. }
  15. }
  16. }

6.3、查询电话

查询会报错,报错只因创建映射时"tel"的"index"为false。

  1. GET /user/_search
  2. {
  3. "query":{
  4. "match":{
  5. "tel":"11"
  6. }
  7. }
  8. }
  9. 返回结果如下:
  10. {
  11. "error" : {
  12. "root_cause" : [
  13. {
  14. "type" : "query_shard_exception",
  15. "reason" : "failed to create query: Cannot search on field [tel] since it is not indexed.",
  16. "index_uuid" : "angUCPxrQWGPBRb-gMci4Q",
  17. "index" : "user"
  18. }
  19. ],
  20. "type" : "search_phase_execution_exception",
  21. "reason" : "all shards failed",
  22. "phase" : "query",
  23. "grouped" : true,
  24. "failed_shards" : [
  25. {
  26. "shard" : 0,
  27. "index" : "user",
  28. "node" : "bKF-IwL0RPmFRkz7pnmapQ",
  29. "reason" : {
  30. "type" : "query_shard_exception",
  31. "reason" : "failed to create query: Cannot search on field [tel] since it is not indexed.",
  32. "index_uuid" : "angUCPxrQWGPBRb-gMci4Q",
  33. "index" : "user",
  34. "caused_by" : {
  35. "type" : "illegal_argument_exception",
  36. "reason" : "Cannot search on field [tel] since it is not indexed."
  37. }
  38. }
  39. }
  40. ]
  41. },
  42. "status" : 400
  43. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/279652
推荐阅读
相关标签
  

闽ICP备14008679号