Elasticsearch(二)索引与映射


一、说明

以下我们都在Kibana -> dev tools下操作

安装Kibana请参考安装Kibana

二、索引基本操作

2.1 创建索引

PUT employee
{
    "settings": {
        "index": {
            "number_of_shards": 3,
            "number_of_replicas": 1
        }
    },
    "mappings": {
        "properties": {
            "age": {
                "type": "integer"
            },
            "first_name": {
                "type": "keyword"
            },
            "last_name": {
                "type": "keyword"
            },
            "about": {
                "type": "text"
            },
            "interests": {
                "type": "keyword"
            }
        }
    }
}

2.2 查看索引

GET employee

2.4 删除索引

DELETE employee

2.5 批量获取索引

GET employee,orderlist

2.6 获取全部索引

GET _all

2.7 使用_cat获取全部索引 GET请求

GET _cat/indices?bytes=kb&s=store.size:desc&v

2.8 判断索引是否存在

HEAD employee

2.9 关闭索引 不删除

POST employee/_close

2.10 打开索引 POST请求

POST employee/_open

三、映射的介绍与使用

- type: text 可分词

- type: keyword 不可分词

3.1 创建Mapping

PUT employee/_mapping
{
    "properties": {
        "age": {
            "type": "integer"
        },
        "first_name": {
            "type": "keyword"
        },
        "last_name": {
            "type": "keyword"
        },
        "about": {
            "type": "text"
        },
        "interests": {
            "type": "keyword"
        }
    }
}

3.2 查看Mapping信息 GET请求

GET employee/_mapping

3.3 批量获取Mapping信息

GET employee,orderlist/_mapping

3.4 获取所有Mapping信息第一种方式

GET _mapping

3.5 获取所有Mapping信息第二种方式

GET _all/_mapping

3.6 增加Mapping字段 POST请求

- Mapping 只可增加字段不可修改字段,比如下面增加了部门department

POST employee/_mapping
{
    "properties": {
        "age": {
            "type": "integer"
        },
        "first_name": {
            "type": "keyword"
        },
        "last_name": {
            "type": "keyword"
        },
        "about": {
            "type": "text"
        },
        "interests": {
            "type": "keyword"
        },
        "department": {
            "type": "keyword"
        }
    }
}

搜索

GET /<target>/_search

GET /_search

xxx

查看文档数

统计集群所有文档

GET /_count
GET /_all/_count

统计单个索引文档数

## target 单个索引和索引别名,或索引和索引别名逗号分隔列表
GET /<target>/_count
# 如有个orderlist索引
GET /orderlist/_count