2

Elasticsearch学习系列二(基础操作) - 女友在高考

 2 years ago
source link: https://www.cnblogs.com/javammc/p/16390582.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Elasticsearch学习系列二(基础操作)

本文将分为3块讲解Es的基础操作。分别为:索引(index)、映射(mapping)、文档(document)。

  1. 创建索引库
PUT /索引名称{
  "settings":{
    "属性名":"属性值"
  }
}

settings:就是索引库的设置,可以定义如分片数、副本数等等。不设置的话就是都走默认值。

PUT /test-demo
  1. 判断索引是否存在
HEAD /索引名称
  • 查看单个索引
GET /索引名称
  • 批量查看索引
GET /索引名称1,索引名称2
  • 查看所有索引
GET _all
POST /索引名称/_open
POST /索引名称/_close
DELETE /索引名称

索引创建之后,等于有了关系型数据库中的database。Es7.x取消了索引type类型的设置,不能指定类型,默认为_doc,但是字段仍然是有的,我们需要设置字段的约束信息,叫做字段映射(mapping)。

字段的约束包括:

  • 字段的数据类型
  • 是否要存储
  • 是否要索引
  1. 创建映射字段
PUT /索引名/_mapping
{
  "properties":{
    "字段名":{
      "type":"类型",
      "index":true,
      "store":true,
      "analyzer":"分词器"
    }
  }
}
  • 字段名:根据需要任意填写
  • type:类型,可以是text(可分词)、keyword(不可分词)、long、short、date、integer、object
  • index:是否索引,默认为true
  • store:是否独立存储,默认为false。原始的文本会存储在 _source 里面,如果设置为true,则是独立的存储某个字段,获取独立存储字段比从_source里解析快,但是更占空间。
  • analyzer:指定分词器,一般中文可以选择ik_max_word、ik_smart
PUT /test-demo1/_mapping
{
  "properties":{
    "name":{
      "type":"text",
      "index":true,
      "store":true,
      "analyzer":"ik_max_word"
    },
    "job":{
      "type":"text",
      "analyzer":"ik_max_word"
    },
    "logo":{
      "type":"keyword",
      "index":false
    }
    ,
    "amt":{
      "type":"double"
    }
  }
}
  1. 查看映射关系
  • 查看某个索引
GET /索引名称/_mapping
  • 查看所有索引
GET _mapping
#或者
GET _all/_mapping
  1. 修改映射关系

这里的修改指的是新增字段,其他更改不支持。只能删除索引,重建映射

PUT /索引库名/_mapping
{
 "properties": {
  "字段名": {
   "type": "类型",
   "index": true,
   "store": true,
   "analyzer": "分词器"
 }
}
}
  1. 一次性创建索引和映射
put /索引库名称
{
  "settings":{
    "索引库属性名":"索引库属性值"
},
  "mappings":{
    "properties":{
        "字段名":{
        "映射属性名":"映射属性值"
      }
    }
  }
}
PUT /test-demo2
{
  "settings":{},
  "mappings": {
    "properties": {
      "name":{
        "type":"text",
        "analyzer": "ik_max_word"
      }
    }
  }
}

文档,即索引库中的数据,会根据规则创建索引,将来用于搜索。可以类比做数据库中的一行数据。

#自动生成id
POST /索引名称/_doc
{
"field":"value"
}
#手动指定id
POST /索引名称/_doc/1
{
"field":"value"
}
POST /test-demo1/_doc/1
{
  "name":"百度",
  "job":"运营",
  "amt":"3000.34",
  "logo":"http://www.lgstatic.com/ttasdf2",
  "createTime":"20220303230000"
  
}
  1. 查看单个文档
GET /索引名称/_doc/{id}

结果如下:

{
  "_index" : "test-demo1",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 2,
  "found" : true,
  "_source" : {
    "name" : "百度",
    "job" : "运营",
    "amt" : "3000.34",
    "logo" : "http://www.lgstatic.com/ttasdf2",
    "createTime" : "20220303230000"
  }
}

元数据项 含义
_index document所属index
_type document所属type,Elasticsearch7.x默认type为_doc
_id 代表document的唯一标识,与index和type一起,可以唯一标识和定位一个document
_version document的版本号,Elasticsearch利用_version(版本号)的方式来确保应用中相互冲突的变更不会导致数据丢失。需要修改数据时,需要指定想要修改文档的version号,如果该版本不是当前版本号,请求将会失败
_seq_no 严格递增的顺序号,每个文档一个,Shard级别严格递增,保证后写入的Doc seq_no大于先写入的Doc的seq_no。任何类型的写操作,包括index、create、update和Delete,都会生成一个_seq_no。
_primary_term 当Primary Shard发生重新分配时,比如重启,Primary选举等,_primary_term会递增1。_primary_term主要是用来恢复数据时处理当多个文档的_seq_no一样时的冲突,避免Primary Shard上的写入被覆盖
found true/false,是否查找到文档
_source 存储原始文档
  1. 查看所有文档
POST /test-demo1/_search
{
  "query":{
    "match_all": {}
  }
}

  1. 仅查询部分字段
GET /test-demo1/_doc/1?_source=name,job
  1. 更新文档(全部更新)
PUT /test-demo1/_doc/1
{
  "name":"百度3",
  "job":"运营",
  "amt":"3000.34",
  "logo":"http://www.lgstatic.com/ttasdf2",
  "createTime":"20220303230000"
  
}

为什么说是全部更新呢?如果你只传了name,其他filed不传。那么文档里就只剩name了。

注意:Elasticsearch执行更新操作的时候,Elasticsearch首先将旧的文档标记为删除状态,然后添加新的文档,旧的文档不会立即消失,但是你也无法访问,Elasticsearch会在你继续添加更多数据的时候在后台清理已经标记为删除状态的文档。

全部更新,是直接把之前的老数据,标记为删除状态,然后,再添加一条更新的(使用PUT或者POST)

  1. 更新文档(部分更新)
POST /索引名称/_update/{id}
{
  "doc":{
    "field":"value"
  }
}
  • 根据id删除
DELETE /索引名称/_doc/{id}
  • 根据查询条件删除
POST /索引名称/_delete_by_query
{
  "query":{
    "match":{
      "字段名":"搜索关键字"
    }
  }
}
  • 删除所有文档
POST /索引名称/_delete_by_query
{
  "query":{
    "match_all":{}
  }
}
  1. 文档强制创建

本来如果不存在会创建,存在会更新。强制创建就是仅创建,不更新。已存在就报错。

PUT /索引名称/_doc/{id}?op_type=create
{
  "filed":"value"
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK