← 返回首页
Elasticsearch-索引操作
发表时间:2021-10-18 17:38:12
Elasticsearch-索引操作

在ES中索引就类似MySQL的表,我们可以使用kibana练习如何操作索引。

1.创建索引

在浏览器打开:http://127.0.0.1:5601/,选择 devtools. 输入以下命令查看所有数据。

GET _search

返回信息如下:

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 0,
    "successful": 0,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": 0,
    "hits": []
  }
}

使用PUT创建students索引.

PUT /students

返回数据:

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

ES7的新特征:

Elasticsearch7.x 对比 Elasticsearch6.x 最大的区别就是去除了type, 没有类型的概念了。

因此我们对于index/type/document之间的关系,重新梳理如下:

ES7方式:

创建索引:

PUT /students/_doc/1
{
  "sid":"s001",
  "name": "张三",
  "age":18
}

PUT /students/_doc/2
{
  "sid":"s002",
  "name": "李四",
  "age":19
}

2.查询索引

使用GET查询索引。

GET /students

返回数据:

{
  "students": {
    "aliases": {},
    "mappings": {},
    "settings": {
      "index": {
        "creation_date": "1634608451293",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "WT0ega-fS72l8A0LrBoekA",
        "version": {
          "created": "6020499"
        },
        "provided_name": "students"
      }
    }
  }
}

修改副本数量:

PUT /students/_settings
{
    "number_of_replicas": "3"
}

GET /students

返回数据:

# PUT /students/_settings
{
  "acknowledged": true
}

# GET /students
{
  "students": {
    "aliases": {},
    "mappings": {},
    "settings": {
      "index": {
        "creation_date": "1634608451293",
        "number_of_shards": "5",
        "number_of_replicas": "3",
        "uuid": "WT0ega-fS72l8A0LrBoekA",
        "version": {
          "created": "6020499"
        },
        "provided_name": "students"
      }
    }
  }
}

ES7方式:


#查询所有学生
GET /students/_search

#查询学号为"s001"的学生
GET /students/_search
{
    "query": {
        "match": {
          "sid": "s001"
        }
    }
}

#模糊查询姓名为"张三"
GET /students/_search
{
  "query": {
    "match": {
      "name": "张三"
    }
  }
}

#精确查找姓名为"张三"
GET /students/_search
{
    "query": {
        "term": {
          "name.keyword": "张三"
        }
    }
}

#精确查找姓名是"张三"和"张三丰"
GET /students/_search
{
    "query": {
        "terms": {
          "name.keyword": ["张三","张三丰"]
        }
    }
}

3.更新索引

ES7方式:

POST /students/_doc/2
{
  "sid":"s002",
  "name": "李幺妹",
  "age":20
}

4.删除索引

使用DELETE删除索引。

DELETE /students

ES7方式:

delete students

#或者
delete /students

也可以一次性删除多个索引。

DELETE /students,/teachers

也可以删除全部索引。

DELETE /_all 或者 DELETE /*