Elasticsearch 实战之三:ES 基本操作

目录

0. 数据格式说明

1. ES的基本操作

1.1 索引操作

1.1.1 建立索引

1.1.2  删除索引

1.1.3  查询索引

1.2 映射操作

1.2.1 建立映射

1.2.2 查询映射

1.3 基本操作-CRUD

1.3.1 新增和替换文档

1.3.2 查询文档


0. 数据格式说明

在实战开始之前,为了便于书写和沟通,本文先来约定一下如何在文章中表达请求和响应的信息:

1. 假设通过Postman工具或者Kibana向服务器发送一个PUT类型的请求,地址是:http://{IP}:9200/test001/article/1

2. 请求的内容是JSON格式的,内容如下:

{ 
  "settings": {
    "number_of_shards": 5	//设置5个片区
    , "number_of_replicas": 1	//设置1个备份
  }
}

对于上面的请求,本文中就以如下格式描述:

PUT /user

{ 
  "settings": {
    "number_of_shards": 5	//设置5个片区
    , "number_of_replicas": 1	//设置1个备份
  }
}

1. ES的基本操作

1.1 索引操作

1.1.1 建立索引

语法:PUT /索引名

# 发送请求
# 在没有特殊设置的情况下,默认有5个分片,1个备份,也可以通过请求参数的方式来指定.。
PUT test_index
{}

# 返回值

#! Deprecation: the default number of shards will change from [5] to [1] in 7.0.0; if you wish to continue using the default of [5] shards, you must manage this on the create index request or with an index template
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "test_index"
}

结果返回创建成果,同时kinbana显示警告,从ES7开始默认的分片数将从5修改为1,如果需要继续指定分片数,则需要采用index参数模板传入参数。

1.1.2  删除索引

语法:DELETE /索引名

# 发送请求
DELETE test_03
{}

# 返回值
{
  "acknowledged" : true
}

 删除成功: 

1.1.3  查询索引

语法:GET /索引名

# 发送请求
GET test_index

# 返回值
{
  "test_index" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1664806581608",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "qcVTOE2VRpieJi3sZOjZwg",
        "version" : {
          "created" : "6050499"
        },
        "provided_name" : "test_index"
      }
    }
  }
}

查询结果中包括了别名(aliases)、映射(mappings)以及配置参数(mappings)等详细信息。

1.2 映射操作

1.2.1 建立映射

语法:POST/索引/映射

# 发送请求
POST /test_index/student
{ 
  "mappings": { 
  "info": { 
    "properties": { 
      "name": { 
        "type": "text"
        },
        "clazz":{
          "type":"string"
        },
        "id":{
          "type":"double"
        }
      } 
    } 
  } 
}

# 返回值
{
  "_index" : "test_index",
  "_type" : "student",
  "_id" : "bB1InoMBWJmEB7k-HcSI",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

字段类型 type:double / long / integer / text / keyword / date / binary

注意:text和keyword都是字符串类型,但是只有text类型的数据才能分词,字段的配置一旦确定就不能更改 映射的配置项有很多,我们可以根据需要只配置用得上的属性.

创建成果,返回值中除了执行结果(result)之外,还有当前映射的详细信息。 

1.2.2 查询映射

语法:GET /索引名/_mapping

# 发送请求
GET /test_index/_mapping

#返回值
{
  "test_index" : {
    "mappings" : {
      "student" : {
        "properties" : {
          "mappings" : {
            "properties" : {
              "info" : {
                "properties" : {
                  "properties" : {
                    "properties" : {
                      "clazz" : {
                        "properties" : {
                          "type" : {
                            "type" : "text",
                            "fields" : {
                              "keyword" : {
                                "type" : "keyword",
                                "ignore_above" : 256
                              }
                            }
                          }
                        }
                      },
                      "id" : {
                        "properties" : {
                          "type" : {
                            "type" : "text",
                            "fields" : {
                              "keyword" : {
                                "type" : "keyword",
                                "ignore_above" : 256
                              }
                            }
                          }
                        }
                      },
                      "name" : {
                        "properties" : {
                          "type" : {
                            "type" : "text",
                            "fields" : {
                              "keyword" : {
                                "type" : "keyword",
                                "ignore_above" : 256
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

返回值包括type和field详细的JSON信息。

1.3 基本操作-CRUD

1.3.1 新增和替换文档

语法:PUT /索引名/类型名/文档ID

# 发送请求
PUT /test_index/student/1
{
  "id":1,
  "name":"战三",
  "clazz":12
}

# 返回值
{
  "_index" : "test_index",
  "_type" : "student",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

注意:

  • 新增和替换文档时需要使用标准的JSON格式,不能使用简版的JSON 格式。
  • POST 操作也可以对文档进行新增和删除操作。他们的唯一区别就在于PUT 请求是硬着陆,会把文档中的属性全部强制更新(如:_id、_version等)。POST 请求 和 DEPETE 不会重置_verison,只会在其基础上+1。
  • 一般推荐使用的是PUT 进行文档的更新和替换,POST 用的比较少。POST操作可以不指明文档的ID,但是我们存在ES中的数据一般都是转存过来的,不会自动生成,故不推荐使用POST。
  • 当索引/类型/映射不存在时,会使用默认设置自动添加。 ES中的数据一般是从别的数据库导入的,所以文档的ID会沿用原数据库中的ID 索引库中没有该ID对应的文档时则新增,拥有该ID对应的文档时则替换。

1.3.2 查询文档

语法:1. 查询所有(基本查询语句): GET /索引名/类型名/_search

# 发送请求
GET /test_index/student/_search

# 返回值
{
  "took" : 151,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_index",
        "_type" : "student",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "id" : 1,
          "name" : "战三",
          "clazz" : 12
        }
      },
      {
        "_index" : "test_index",
        "_type" : "student",
        "_id" : "bB1InoMBWJmEB7k-HcSI",
        "_score" : 1.0,
        "_source" : {
          "mappings" : {
            "info" : {
              "properties" : {
                "name" : {
                  "type" : "text"
                },
                "clazz" : {
                  "type" : "string"
                },
                "id" : {
                  "type" : "double"
                }
              }
            }
          }
        }
      }
    ]
  }
}

 查询所有结果中包含以下字段:

  • took:耗时
  • _shards.total:分片总数
  • hits.total:查询到的数量
  • hits.max_score:最大匹配度
  • hits.hits:查询到的结果
  • hits.hits._score:匹配度

语法:2. 根据ID查询: GET /索引名/类型名/文档ID

# 发送请求
GET /test_index/student/1


# 返回值
{
  "_index" : "test_index",
  "_type" : "student",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "id" : 1,
    "name" : "战三",
    "clazz" : 12
  }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值