05 elasticsearch学习笔记-基本CRUD

视频教程

Elasticsearch(7.8.1)沥血之作(包含仿百度搜索案例)
https://www.bilibili.com/video/BV1y5411j7su?p=5

视频资料提取地址:
链接:https://pan.baidu.com/s/1qvdGvhMX3tGLLVSaHSWg0w
提取码:qumi

链接:https://pan.baidu.com/s/1Kh_ne_gaJ0ONz319YSOnog
提取码:2uo8

4.1 基本CRUD

GET movies/_search # 查询movies的数据

GET movies/_count #查询movies的总数

GET _cat/indices #查看所有的索引

GET movies/_doc/24 #查询id为24的数据

POST users/_doc/1 #添加id为1的文档 ,如果没有指定id,ES会自动生成 { "firstname": "will",
"lastname": "smith" }

POST users/_create/2 #创建id为2的文档,如果索引中已存在相同id,会报错; { "firstname": "will",
"lastname": "smith" }

POST users/_update/2 #在id位2的文档中添加一个age属性,修改结构 { "doc": { "age": 30 } }

DELETE users/_doc/2 #删除id为2的文档

DELETE users #删除 users 索引

PUT users/_doc/1 #创建或者修改文档

PUT users/_create/2 #创建id为2的文档,如果已存在就报错,如果不存在就创建 { "firstname": "will",
"lastname": "smith" }

GET mget #批量查询多个指定的id的数据,也可以批量查询 { "docs": [ {" index": "users", " id": 1},

批量插入数据

POST users/ bulk 
{"index": {" id": 3}} 
{"firstname": "A", "lastname": "a"} 
{"index":{" id": 4}} 
{"firstname": "B", "lastname": "b"} 
{"index": {" id": 5}} 
{"firstname": "X", "lastname": "x"}
{"index": {"_id": 6}} 
{"firstname": "Z", "lastname": "z"}

4.2 URI查询

GET movies/_search?q=2012 #查询所有的属性中只要包含2012的所有的数据,泛查询

GET movies/ search?q=2012&df=title #查询title中包含2012的所有的电影,df(default field)或者 GET movies/ search?q=title:2012

GET movies/_search?q=title:2012&from=10&size=8 #查询title中包含2012,从第10条开始,查询8条
数据

GET movies/_search?q=title:Beautiful Mind #查询title中包含Beautiful或者Mind的所有的数据
GET movies/_search?q=title:(Beautiful Mind)
GET movies/_search?q=title:(+Beautiful +Mind)

GET movies/_search?q=title:"Beautiful Mind" #查询title中包含 "Beautiful Mind"这个短语的所
有的数据

GET movies/_search?q=title:(Mind AND Beautiful) #查询title中既包含Mind又包含Beautiful的所有的数据,与顺序没有关系

>GET movies/_search?q=title:(Beautiful NOT Mind) #查询title中包含Beautiful但是不包含mind的所有的数据

GET movies/_search?q=title:(Beautiful -Mind)

GET movies/_search?q=title:Beautiful AND year:>=2012 #查询title中包含Beautiful且电影上映时间
在2012年之后的所有的数据

GET movies/_search?q=year:>=2018 #查询2018年之后上映的电影

GET movies/_search?q=year:(>=2012 AND <2018) #查询在2012到2017年上映的电影

GET movies/_search?q=year:{2015 TO 2017] #查询2016年到2017年上映的电影,必须以 ] 结尾

GET movies/_search?q=title:Min?x # ?代表一个字母

GET movies/_search?q=title:Min* # 查询title中包含以 Min开头的字母的电影

# 查看elaticsearch的状态
GET _cat/health

# 查看有哪些索引 (6.x版本需要后面加?v )
GET _cat/indices?v

#创建test6
PUT /test6
{
  "mappings": {
    "properties": {
      "name":{
        "type":"text"
      },
      "age":{
        "type": "long"
      },
      "birthday":{
        "type": "date"
      }
    }
  }
}

# 查看mapping 方法一
GET /test6

# 查看mapping 方法二
GET /test6/_mapping?pretty

# 删除索引
DELETE test6

# 同时删除多个索引
DELETE fluentd-20210319,fluentd-20210419

DELETE fluentd-20210412/access_log/ZCbUxHgBHgZJ0U05KD3A

# 分词器
GET _analyze
{
  "analyzer":"ik_smart",
  "text": "我是中国人"
}

GET _analyze
{
  "analyzer":"ik_max_word",
  "text": "我是中国人"
}

GET _analyze
{
  "analyzer":"ik_max_word",
  "text": "狂神说java"
}




#创建test3
PUT /test3
{
  "mappings": {
    "properties": {
      "name":{
        "type":"text"
      },
      "age":{
        "type": "long"
      },
      "birthday":{
        "type": "date"
      }
    }
  }
}

GET test3


PUT /test3/_doc/3
{
    "name":"王五",
    "age":22,
    "birth":"1990-01-06"
}


# test里的总数
GET test3/_count

# 查所有的数据
GET test3/_search

# 查id为1的数据
GET test3/_doc/1

# 删除指定id为1的记录
DELETE test3/_doc/1

GET test3



POST /test3/_doc/1/_update
{
  "doc":{
    "name":"fong"
  }
}

# es 7写法
POST /test3/_update/1
{
  "doc":{
    "name":"fong"
  }
}



GET /test3/_doc/1

DELETE /test1

DELETE /test2


PUT /haima/user/1
{
     "name":"狂神说123",
     "age":13,
     "desc":"我是描述",
     "tags":["技术宅","温暖","直男"]
}

GET /haima

DELETE /haima

GET /haima/_search


GET /haima/user/1


PUT /haima/user/2
{
     "name":"张三说",
     "age":22,
     "desc":"我是描述22",
     "tags":["技术宅","温暖","直男"]
}


PUT /haima/user/3
{
     "name":"李四说",
     "age":33,
     "desc":"我是描述33",
     "tags":["技术宅","温暖","直男"]
}

PUT /haima/user/6
{
     "name":"狂神说",
     "age":27,
     "desc":"我是描述66",
     "tags":["java","mysql","redis"]
}

GET /haima/user/_search?q=name:李四


GET /haima/user/_search
{
  "query": {
    "match": {
      "name": "李四"
    }
  },
  "_source":["name","age"]
}


GET /haima/user/_search
{
  "query": {
    "match": {
      "name": "李四"
    }
  },
  "_source":{"includes":["name","age"]}
}


GET /haima/user/_search
{
  "query": {
    "match": {
      "name": "说"
    }
  },
  "sort":{
    "age":{
      "order":"asc" 
    }
  },
  "from":0,
  "size":2
}



GET /haima/user/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "match": {
            "name": "说"
          }
        },{
          "match": {
            "age": "44"
          }
        }
      ]
    }
  }
}


GET /haima/user/_search
{
  "query":{
    "bool": {
      "should": [
        {
          "match": {
            "name": "王五"
          }
        },{
          "match": {
            "age": "44"
          }
        }
      ]
    }
  }
}


GET /haima/user/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "match": {
            "name": "王五"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 10,
            "lte": 50
          }
        }
      }
    }
  }
}


GET /haima/user/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "match": {
            "tags": "技术 女"
          }
        }
      ]
    }
  }
}

GET _analyze
{
  "analyzer":"standard",
  "text":"张三说"
}

GET _analyze
{
  "analyzer":"keyword",
  "text":"狂神说"
}

GET /employee/_search
{}

GET /employee/_search
{
  "size": 0,
  "aggs": {
    "job_info": {
      "terms": {
        "field": "job",
        "size": 9999
      }
    }
  }
}

GET /employee/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "job": "java"
          }
        }
      ]
    }
  },
  "size": 0, 
  "aggs": {
    "aggs_job": {
      "terms": {
        "field": "job",
        "size": 10
      },
      "aggs": {
        "aggs_job_gender": {
          "terms": {
            "field": "gender",
            "size": 10
          }
        }
      }
    },
    "aggs_gender":{
      "terms": {
        "field": "gender",
        "size": 10
      }
    }
  }
}

按时间段查

{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "lastupdatetime": {
              "gt": "2021-08-16 00:00:00",
              "lt": "2021-08-17 00:00:00"
            }
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "_source": [
    "port"
  ],
  "from": 0,
  "size": 9999,
  "sort": [],
  "aggs": {}
}

更多文章:
es常用操作手册
https://blog.csdn.net/qqsilhonette/article/details/105447921

posted @ 2021-09-13 01:20  HaimaBlog  阅读(67)  评论(0编辑  收藏  举报