ES 索引的基本操作

2021-03-04  本文已影响0人  小P聊技术

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 字段。状态可能是下列三个值之一:

2.2 创建索引

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

传递的JSON参数

{
     "settings": {
        "index": {
            "number_of_shards": "2",
            "number_of_replicas": "0"
        }
    }
}

返回结果

{
    "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 相关信息

上一篇下一篇

猜你喜欢

热点阅读