ES 索引的基本操作

1 介绍

主要介绍索引请求的基础API操作,使用postman进行请求,接口请求的前缀地址统一为elasticsearch 部署IP地址+端口号(例如 http://192.168.51.4:9200 。下方提供postman测试使用的接口地址JSON导出文件:

postman 接口集合下载地址 https://download.csdn.net/download/qq_15769939/15469409

2 索引基础操作

2.1 集群健康状态

官网地址:

https://www.elastic.co/guide/cn/elasticsearch/guide/current/_cluster_health.html#_cluster_health

请求方式 接口地址
GET /_cluster/health
{
    "cluster_name": "auskat-elasticsearch",
    "status": "yellow",
    "timed_out": false,
    "number_of_nodes": 1,
    "number_of_data_nodes": 1,
    "active_primary_shards": 8,
    "active_shards": 8,
    "relocating_shards": 0,
    "initializing_shards": 0,
    "unassigned_shards": 1,
    "delayed_unassigned_shards": 0,
    "number_of_pending_tasks": 0,
    "number_of_in_flight_fetch": 0,
    "task_max_waiting_in_queue_millis": 0,
    "active_shards_percent_as_number": 88.88888888888889
}

响应信息中最重要的一块就是 status 字段。状态可能是下列三个值之一:

  • green

    所有的主分片和副本分片都已分配。你的集群是 100% 可用的。

  • yellow

    所有的主分片已经分片了,但至少还有一个副本是缺失的。不会有数据丢失,所以搜索结果依然是完整的。不过,你的高可用性在某种程度上被弱化。如果 更多的 分片消失,你就会丢数据了。把 yellow 想象成一个需要及时调查的警告。

  • red

    至少一个主分片(以及它的全部副本)都在缺失中。这意味着你在缺少数据:搜索只能返回部分数据,而分配到这个分片上的写入请求会返回一个异常。

    扫描二维码关注公众号,回复: 12695638 查看本文章

2.2 创建索引

请求方式 接口地址 备注
PUT /index_api_demo index_api_demo 要创建的索引名称

传递的JSON参数

{
    
    
     "settings": {
    
    
        "index": {
    
    
            "number_of_shards": "2",
            "number_of_replicas": "0"
        }
    }
}
  • number_of_shards 分片数量
  • number_of_replicas 副本数量

返回结果

{
    
    
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "index_api_demo"
}

2.3 查询指定索引信息

请求方式 接口地址 备注
GET /index_api_demo index_api_demo 要查询的索引名称

请求结果

{
    
    
    "index_api_demo": {
    
    
        "aliases": {
    
    },
        "mappings": {
    
    },
        "settings": {
    
    
            "index": {
    
    
                "creation_date": "1614258485568",
                "number_of_shards": "2",
                "number_of_replicas": "0",
                "uuid": "vnErVtHjSH-n29F1df8tDg",
                "version": {
    
    
                    "created": "7040299"
                },
                "provided_name": "index_api_demo"
            }
        }
    }
}

2.4 查询所有索引的信息

请求方式 接口地址
GET /_cat/indices?v

请求结果

health status index          uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   index_mapping  ECD_xsBERPa5gSLCYe3r4g   1   1          0            0       283b           283b
green  open   index_demo     NieZpAnYTSSSkC1umY7u7g   5   0          0            0      1.3kb          1.3kb
green  open   my_doc         ZEEYJASoTIqKyr8SycMQxw   1   0          5            3     11.4kb         11.4kb
green  open   index_field    xRd8d_bvSmWYK9_HFJfe2A   1   0          0            0       283b           283b
green  open   index_api_demo vnErVtHjSH-n29F1df8tDg   2   0          0            0       566b           566b

2.5 删除索引

请求方式 接口地址 备注
DELETE /index_api_demo index_api_demo 要查询的索引名称

请求结果

{
    
    
    "acknowledged": true
}

2.6 创建索引的同时创建mappings

请求方式 接口地址 备注
PUT /index_mapping_demo index_mapping_demo 要创建的索引名称

传递json数据

{
    
    
    "mappings": {
    
    
        "properties": {
    
    
            "realname": {
    
    
                "type": "text",
                "index": true
            },
             "username": {
    
    
                "type": "keyword",
                "index": false
            }
        }
    }
}

index:默认为true,设置为false的话,那么这个字段就不会被索引

官方地址: https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-index.html

index

The index option controls whether field values are indexed. It accepts true or false and defaults to true. Fields that are not indexed are not queryable.

请求结果

{
    
    
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "index_mapping_demo"
}

2.7 修改mappings

请求方式 接口地址 备注
POST /index_mapping_demo/_mapping index_mapping_demo 索引名称

传递JSON数据

{
    
    
    "properties": {
    
    
        "id": {
    
    
            "type": "long"
        },
            "age": {
    
    
            "type": "integer"
        },
            "money1": {
    
    
            "type": "double"
        },
            "money2": {
    
    
            "type": "float"
        },
            "sex": {
    
    
            "type": "byte"
        },
            "score": {
    
    
            "type": "short"
        },
            "is_teenger": {
    
    
            "type": "boolean"
        },
            "birthday": {
    
    
            "type": "date"
        },
            "relationship": {
    
    
            "type": "object"
        }
    }
}

请求结果

{
    "acknowledged": true
}

某个属性一旦被建立,就不能修改了,但是可以新增额外的属性

3 相关信息

  • 博文不易,辛苦各位猿友点个关注和赞,感谢

猜你喜欢

转载自blog.csdn.net/qq_15769939/article/details/114356484