elasticsearch学习(五)文档CRUD

目录

上一篇文章

elasticsearch学习(四)kibana安装

https://blog.csdn.net/github_36801273/article/details/151076329?spm=1011.2124.3001.6209

文档CRUD

创建索引

复制代码
PUT /my-index
{
    "mappings": {
        "properties": {
                "testFieldA": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "testFieldB": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
    }
}

mapping的部分不是必须的,可以通过先创建索引,然后写入数据的方式自动生成mapping

查看索引字段mapping

复制代码
GET /my-index/_mapping

{
  "my-index": {
    "mappings": {
      "properties": {
        "testFieldA": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "testFieldB": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

更新mapping

复制代码
POST  /my-index/_mapping
{
    "properties": {
        "testFieldC": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                }
            }
        },
        "testFieldB": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 512
                }
            }
        }
    }
}

只能新增和更新字段,不能删除字段,删除字段用reindex

删除索引

复制代码
DELETE /my-index

测试索引是否存在

复制代码
HEAD /my-index

查看索引字段结构

复制代码
GET /my-index/_mapping

删除索引

复制代码
DELETE /my-index

文档命令

创建文档

Index

如果ID不存在,则创建新的,否则删除旧的再创建新的,增加版本号

复制代码
POST /my-index/_doc
{
    "id": "park_rocky-mountain",
    "title": "Rocky Mountain",
    "description": "Bisected north to south by the Continental Divide, this portion of the Rockies has ecosystems varying from over 150 riparian lakes to montane and subalpine forests to treeless alpine tundra."
}

Create

复制代码
// 不指定ID(系统自动生成)
POST /my-index/_doc
{
    "testFieldA":"testA1",
    "testFieldB":"testB1"
}

// 指定ID
POST /my-index/_doc/2
{
    "testFieldA":"testA2",
    "testFieldB":"testB2"
}
Create与Index的区别

create的文档ID如果已存在,会报失败

index的文档ID不存在,会新建,否则会把原来的删除了再增加

更新文档

复制代码
POST /my-index/_update/2
{
    "doc":{
        "testFieldA":"updateTestA2"
    } 
}

删除文档

复制代码
DELETE /my-index/_doc/2

查询文档

支持两种方式:

1 url查询,在url路径中使用查询参数

2 用DSL(Query Domain Specific Language)

简单查询语法

api 作用
/_search 查询集群上所有索引
/{index_name}/_search 查询指定索引
/{index_name1},{index_name2},{...}/_search 查询多个指定索引
/{index_prex}*/_search 查询以{index_prex}为开头的索引
复制代码
// 指定ID查询
GET /my-index/_doc/2

// 查询全量数据
GET /my-index/_search

// 查询my开头的索引数据
GET /my*/_search

// URL查询
GET /my-index/_search?q=testFieldA:updateTestA2

// DSL查询
GET /my-index/_search -H 'Content-Type:application/json' -d
{
    "query":{
        "match":{"testFieldA":"updateTestA2"}
    }
}
相关推荐
历程里程碑2 分钟前
滑动窗口------滑动窗口最大值
大数据·python·算法·elasticsearch·搜索引擎·flask·tornado
YangYang9YangYan3 分钟前
大数据与会计专业学习发展指南
大数据·学习
知识分享小能手17 分钟前
SQL Server 2019入门学习教程,从入门到精通,初识 SQL Server 2019 —— 语法知识点与使用方法详解(1)
数据库·学习·sqlserver
代码游侠18 分钟前
C语言核心概念复习(三)
开发语言·数据结构·c++·笔记·学习·算法
烧烧的酒0.o20 分钟前
Java——JavaSE完整教程
java·开发语言·学习
嗯嗯**25 分钟前
Neo4j学习4:数据导入
学习·neo4j·图数据库·csv·数据导入
代码游侠28 分钟前
学习笔记——Linux内核与嵌入式开发2
linux·运维·arm开发·嵌入式硬件·学习·架构
我是黄骨鱼28 分钟前
【零基础学数据库|第四篇】SQL通用语法学习
学习
lichenyang45344 分钟前
Node.js AI 开发入门 - 完整学习笔记
人工智能·学习·node.js
Gain_chance1 小时前
29-学习笔记尚硅谷数仓搭建-DWD层交易域下单事务事实表和交易域支付成功事务事实表
数据仓库·hive·笔记·学习·datagrip