本文分类:
ElasticStack

基本概念

ES是一个分布式文档存储数据库,也有与关系型数据库类似的概念,比较如下:

可以这么理解:

类型 数据库 记录
关系型数据库 数据库(Database) 表(Table) 行(Row) 列(Column)
ES 索引(Index) 类型(Type)+ 映射(Mapping) 文档(Document) 字段(Field)

类型(Type)类似关系型数据库中的表,映射(Mapping)定义表结构,创建类型(Type)时需要配合映射(Mapping)。

安装

下载解压即可

运行~/elasticsearch-6.8.9/bin/elasticsearch

浏览器打开 http://localhost:9200/?pretty

{
  "name" : "-YFSXhC",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "duozN33iTMqNOBDWKKM2iA",
  "version" : {
    "number" : "6.8.9",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "be2c7bf",
    "build_date" : "2020-05-04T17:00:34.323820Z",
    "build_snapshot" : false,
    "lucene_version" : "7.7.3",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

节点(Node)与分片(Shard)

ES可部署为分布式集群,也可只作为单机单节点部署。一个ES实例称之为一个节点,单机部署的ES有且只有一个节点,集群部署的ES有多个节点,但只有一个主节点。

ES中的数据被分散存储在分片中,以便能够将数据分散存储到多个ES节点中进行同时查询。分片有主分片和副本分片之分,当主分片出现故障时,备份片充当主分片。默认情况下,创建索引时会创建5个主分片,每个主分片1个副本分片。主分片在创建索引时确定便不能修改,只能通过重新创建索引修改,副本分片可以修改。官方建议每个分片的数据量最好是在20G-40G左右(https://www.elastic.co/cn/blog/how-many-shards-should-i-have-in-my-elasticsearch-cluster).

简单 API

创建索引

请求

PUT http://localhost:9200/hiwzc

响应

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

查看索引

请求

GET http://localhost:9200/hiwzc

响应

{
    "hiwzc": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "creation_date": "1591519917803",
                "number_of_shards": "5",
                "number_of_replicas": "1",
                "uuid": "IZzMko-KRpWpy4o8-sFJbg",
                "version": {
                    "created": "6080999"
                },
                "provided_name": "hiwzc"
            }
        }
    }
}

查询ES中的索引

请求

GET http://localhost:9200/_cat/indices?v

响应

health status index uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   hiwzc IZzMko-KRpWpy4o8-sFJbg   5   1          0            0      1.1kb          1.1kb

字段说明如下:

删除索引

请求

DELETE http://localhost:9200/hiwzc

响应

{
    "acknowledged": true
}

创建 TYPE

类型Type(或者映射Mapping)一旦定义,就不能删除,只能修改。

请求

PUT http://localhost:9200/demo/_mapping/hiwzc_type

{
    "properties":{
        "created_date":{
            "type":"date"
        },
        "created_by":{
            "type":"keyword"
        }
    }
}

创建索引时创建 TYPE

请求

PUT http://localhost:9200/hiwzc

{
    "mappings":{
        "hiwzc_type":{
            "properties":{
                "created_date":{
                    "type":"date"
                },
                "created_by":{
                    "type":"keyword"
                }
            }
        }
    }
}

插入文档

请求

POST http://localhost:9200/hiwzc/hiwzc_type

{
    "created_date":1591521053398,
    "created_by":"hiwzc"
}

响应

{
    "_index": "hiwzc",
    "_type": "hiwzc_type",
    "_id": "OasMjnIBT7RTZj62_rdx",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

查看指定文档

请求

GET http://localhost:9200/hiwzc/hiwzc_type/OasMjnIBT7RTZj62_rdx

响应

{
    "_index": "hiwzc",
    "_type": "hiwzc_type",
    "_id": "OasMjnIBT7RTZj62_rdx",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "created_date": 1591521053398,
        "created_by": "hiwzc"
    }
}

查看全部文档

请求

GET http://localhost:9200/hiwzc/hiwzc_type/_search?pretty

响应

{
    "took": 116,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
            {
                "_index": "hiwzc",
                "_type": "hiwzc_type",
                "_id": "OasMjnIBT7RTZj62_rdx",
                "_score": 1,
                "_source": {
                    "created_date": 1591521053398,
                    "created_by": "hiwzc"
                }
            }
        ]
    }
}

修改文档

请求

POST http://localhost:9200/hiwzc/hiwzc_type/OasMjnIBT7RTZj62_rdx/_update

{
    "doc":{
        "created_by":"hiwzc_update"
    }
}

响应

{
    "_index": "hiwzc",
    "_type": "hiwzc_type",
    "_id": "OasMjnIBT7RTZj62_rdx",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

删除文档

请求

DELETE http://localhost:9200/hiwzc/hiwzc_type/OasMjnIBT7RTZj62_rdx

响应

{
    "_index": "hiwzc",
    "_type": "hiwzc_type",
    "_id": "OasMjnIBT7RTZj62_rdx",
    "_version": 3,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}
本文来自 [时光记 - 王智超的个人空间](www.hiwzc.com),转载请注明出处。