04 elasticsearch学习笔记-基本CRUD

视频教程
https://www.bilibili.com/video/BV1y5411j7su?p=5





# 查看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": {}
}

[Haima的博客] http://www.cnblogs.com/haima/
posted @ 2021-09-13 01:20  HaimaBlog  阅读(9)  评论(0编辑  收藏  举报