ElasticSearch 5.5.0学习笔记(一)

ElasticSearch 5.5.0学习笔记(一)

1. ES中的基本概念

1.1 接近实体

  • ElasticSearch是一个接近实体的搜索平台,意味着,从索引一个文档直到这个文档能够被搜索到有一个轻微的延迟(通常是1秒内)

1.2 索引(index)

  • 一个索引就是一个拥有几分相似特征的文档集合。一个索引由一个名字来标识,并且当我们要对这个索引中的文档进行索引、搜索、更新和删除的时候,都要使用到这个名字。索引类似于关系型数据库中的Database概念

1.3 类型(type)

  • 在一个索引中,你可以定义一个或多个类型。类型类似于关系型数据库中的表的概念。具有一组共同的字段。
  • 在5版本以前可以在一个索引中定义多个类型,在6版本中也可以使用,在7、8版本中一个索引对应一个类型

1.4 映射(mapping)

  • 映射实际上就是定义的类型中的各个字段,对应关系型数据库中表中的字段类型

1.5 文档(document)

  • 一个文档就是一个可以被索引的基础信息单元,类似表中的一条记录。

2. Kibana索引操作

2.1 创建索引

  • PUT user//创建名为 user 的索引

  • 返回结果:
    {
      "acknowledged": true,
      "shards_acknowledged": true
    }
    

2.2 查看所有索引

  • GET _cat/indices?v// 参数v 显示返回的列的表头

  • 返回结果:
    health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
    yellow open   .kibana E18fY25GSFKZx7MAMKNF8g   1   1          1            0      3.2kb          3.2kb
    yellow open   user    xclxPHyWRbGXXWbXdBbIpw   5   1          0            0       810b           810b
    

2.3 删除索引

  • DELETE user//删除名为 user 的索引

  • 返回结果:
    {
      "acknowledged": true
    }
    
  • DELETE /*//删除索引索引

3. Kibana类型操作

3.1 创建类型

  • PUT /user
    {
      "mappings": {
        "customer":{
          "properties": {
            "id":{
              "type": "keyword"
            },
            "name":{
             "type": "text"  
            },
            "age":{
            "type": "integer"  
            },
            "bir":{
            "type": "date"  
            }
          }
        }
      }
    }
    
  • mappings: 关键词,表示 类型映射

  • customer: 你要添加的 类型的名称

  • properties:关键词,表示映射字段

  • id、name、age、bir:你要添加的字段名称

  • type:字段类型

    • 常用的有:text、keyword、date、integer、long、double、boolean or ip
  • 返回结果:
    {
      "acknowledged": true,
      "shards_acknowledged": true
    }
    

3.2 查看创建的索引及类型映射

  • GET user//查看索引

  • 返回结果:
    {
      "user": {
        "aliases": {},
        "mappings": {
          "customer": {
            "properties": {
              "age": {
                "type": "integer"
              },
              "bir": {
                "type": "date"
              },
              "id": {
                "type": "keyword"
              },
              "name": {
                "type": "text"
              }
            }
          }
        },
        "settings": {
          "index": {
            "creation_date": "1631518834326",
            "number_of_shards": "5",
            "number_of_replicas": "1",
            "uuid": "SVkUCauvRyON-KZYs6XIQw",
            "version": {
              "created": "5050099"
            },
            "provided_name": "user"
          }
        }
      }
    }
    
  • GET user/_mapping//只查看其类型映射

  • 返回结果:
    {
      "user": {
        "mappings": {
          "customer": {
            "properties": {
              "age": {
                "type": "integer"
              },
              "bir": {
                "type": "date"
              },
              "id": {
                "type": "keyword"
              },
              "name": {
                "type": "text"
              }
            }
          }
        }
      }
    }
    

4. Kibana文档操作

4.1 插入文档

  • # PUT /索引/类型/_id
    PUT /user/customer/1
    {
      "id":1,
      "name":"hjx",
      "age":24,
      "bir":"2021-09-10"
    }
    
  • 返回结果:
    {
      "_index": "user",
      "_type": "customer",
      "_id": "1",
      "_version": 1,
      "result": "created",
      "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
      },
      "created": true
    }
    
  • # 自动生成id
    POST /user/customer
    {
      "name":"zs",
      "age":20,
      "bir":"2021-08-10"
    }
    
  • 返回结果:
    {
      "_index": "user",
      "_type": "customer",
      "_id": "AXveKbYSN3SMJmlGOrdz",
      "_version": 1,
      "result": "created",
      "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
      },
      "created": true
    }
    

4.2 查询文档

  • GET /user/customer/1 //获取user索引中customer类型中_id为1的文档
    
  • 返回结果:
    {
      "_index": "user",
      "_type": "customer",
      "_id": "1",
      "_version": 1,
      "found": true,
      "_source": {
        "id": 1,
        "name": "hjx",
        "age": 24,
        "bir": "2021-09-10"
      }
    }
    

4.3 删除文档

  • DELETE /user/customer/1 //删除user索引中customer类型中_id为1的文档
    
  • 返回结果:
    {
      "found": true,
      "_index": "user",
      "_type": "customer",
      "_id": "1",
      "_version": 2,
      "result": "deleted",
      "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
      }
    }
    

4.4 更新文档

  • POST /user/customer/1/_update
    {
      "doc": {
        "name":"ls"
      }
    }
    doc:关键词,表示文档
    更新操作会将数据先查询出来,再根据{}中的映射和值进行相应的更新
    如果添加的映射是原先没有的映射,es会自动创建相应的映射
    
  • 返回结果:
    {
      "_index": "user",
      "_type": "customer",
      "_id": "1",
      "_version": 2,
      "result": "noop",
      "_shards": {
        "total": 0,
        "successful": 0,
        "failed": 0
      }
    }
    

4.5 批量操作

  • _bulk

    • PUT /user/customer/_bulk
      {"index":{"_id":"5"}}
        {"name":"小明","age":23,"bir":"2011-11-11"}
      {"delete":{"_id":"4"}}
      {"update":{"_id":"1"}}
        {"doc":{"name":"hjx"}}
      
    • 返回结果:
      {
        "took": 14,
        "errors": false,
        "items": [
          {
            "index": {
              "_index": "user",
              "_type": "customer",
              "_id": "5",
              "_version": 1,
              "result": "created",
              "_shards": {
                "total": 2,
                "successful": 1,
                "failed": 0
              },
              "created": true,
              "status": 201
            }
          },
          {
            "delete": {
              "found": false,
              "_index": "user",
              "_type": "customer",
              "_id": "4",
              "_version": 1,
              "result": "not_found",
              "_shards": {
                "total": 2,
                "successful": 1,
                "failed": 0
              },
              "status": 404
            }
          },
          {
            "update": {
              "_index": "user",
              "_type": "customer",
              "_id": "1",
              "_version": 3,
              "result": "updated",
              "_shards": {
                "total": 2,
                "successful": 1,
                "failed": 0
              },
              "status": 200
            }
          }
        ]
      }
      
    • 上面的代码分别执行了三条指令,插入一条文档、删除一条文档和更新一条文档。

    • 返回结果有三条结果,说明不是原子操作,每条语句互不影响

5 检索

5.1 QueryString查询

  • 通过URL参数的形式进行搜索

  • GET /user/customer/_search?q=*&sort=age:desc&size=2&from=0&_source=name
    
  • q

    • q表示查询,*匹配所有
  • sort

    • 按照age排序,desc:倒序
  • size

    • 一页数量
  • from

    • 起始页
  • _source

    • 选择返回的结果包含哪些映射,不写默认返回全部的映射

5.2 QueryDSL查询

  • 基于传递JSON作为请求体格式与ES进行交互

  • 查询所有

    • GET /user/customer/_search
      {
        "query": {
          "match_all": {}
        }
      }
      
      返回结果:
      {
        "took": 0,
        "timed_out": false,
        "_shards": {
          "total": 5,
          "successful": 5,
          "failed": 0
        },
        "hits": {
          "total": 3,
          "max_score": 1,
          "hits": [
            {
              "_index": "user",
              "_type": "customer",
              "_id": "5",
              "_score": 1,
              "_source": {
                "name": "小明",
                "age": 23,
                "bir": "2011-11-11"
              }
            },
            {
              "_index": "user",
              "_type": "customer",
              "_id": "AXveKbYSN3SMJmlGOrdz",
              "_score": 1,
              "_source": {
                "name": "zs",
                "age": 20,
                "bir": "2021-08-10"
              }
            },
            {
              "_index": "user",
              "_type": "customer",
              "_id": "1",
              "_score": 1,
              "_source": {
                "id": 1,
                "name": "hjx",
                "age": 24,
                "bir": "2021-09-10"
              }
            }
          ]
        }
      }
      
  • 查询索引并排序

    • GET user/customer/_search
      {
        "query": {
          "match_all": {}
        },
        "sort": [
          {
            "age": {
              "order": "desc"
            }
          }
        ]
      }
      注意的是,如果字段是text类型的话,是不支持排序的
          
      返回结果:
      {
        "took": 1,
        "timed_out": false,
        "_shards": {
          "total": 5,
          "successful": 5,
          "failed": 0
        },
        "hits": {
          "total": 3,
          "max_score": null,
          "hits": [
            {
              "_index": "user",
              "_type": "customer",
              "_id": "1",
              "_score": null,
              "_source": {
                "id": 1,
                "name": "hjx",
                "age": 24,
                "bir": "2021-09-10"
              },
              "sort": [
                24
              ]
            },
            {
              "_index": "user",
              "_type": "customer",
              "_id": "5",
              "_score": null,
              "_source": {
                "name": "小明",
                "age": 23,
                "bir": "2011-11-11"
              },
              "sort": [
                23
              ]
            },
            {
              "_index": "user",
              "_type": "customer",
              "_id": "AXveKbYSN3SMJmlGOrdz",
              "_score": null,
              "_source": {
                "name": "zs",
                "age": 20,
                "bir": "2021-08-10"
              },
              "sort": [
                20
              ]
            }
          ]
        }
      }
      
  • 查询所有并分页

    • GET /user/customer/_search
      {
        "query": {
          "match_all": {}
        },
        "size": 2,
        "from": 0
      }
      
      返回结果:
      {
        "took": 0,
        "timed_out": false,
        "_shards": {
          "total": 5,
          "successful": 5,
          "failed": 0
        },
        "hits": {
          "total": 3,
          "max_score": 1,
          "hits": [
            {
              "_index": "user",
              "_type": "customer",
              "_id": "5",
              "_score": 1,
              "_source": {
                "name": "小明",
                "age": 23,
                "bir": "2011-11-11"
              }
            },
            {
              "_index": "user",
              "_type": "customer",
              "_id": "AXveKbYSN3SMJmlGOrdz",
              "_score": 1,
              "_source": {
                "name": "zs",
                "age": 20,
                "bir": "2021-08-10"
              }
            }
          ]
        }
      }
      
  • 查询所有并返回指定的映射

    • GET /user/customer/_search
      {
        "query": {
          "match_all": {}
        },
        "_source": ["name","age"]
      }
      
      返回结果:
      {
        "took": 1,
        "timed_out": false,
        "_shards": {
          "total": 5,
          "successful": 5,
          "failed": 0
        },
        "hits": {
          "total": 3,
          "max_score": 1,
          "hits": [
            {
              "_index": "user",
              "_type": "customer",
              "_id": "5",
              "_score": 1,
              "_source": {
                "name": "小明",
                "age": 23
              }
            },
            {
              "_index": "user",
              "_type": "customer",
              "_id": "AXveKbYSN3SMJmlGOrdz",
              "_score": 1,
              "_source": {
                "name": "zs",
                "age": 20
              }
            },
            {
              "_index": "user",
              "_type": "customer",
              "_id": "1",
              "_score": 1,
              "_source": {
                "name": "hjx",
                "age": 24
              }
            }
          ]
        }
      }
      
  • term查询

    • GET /user/customer/_search
      {
        "query": {
          "term": {
            "name": {
              "value": "hjx"
            }
          }
        }
      }
      
      term 精确查找
      name:所要查询的字段
      hjx:所要查询字段的值
          
      返回结果:
      {
        "took": 4,
        "timed_out": false,
        "_shards": {
          "total": 5,
          "successful": 5,
          "failed": 0
        },
        "hits": {
          "total": 1,
          "max_score": 0.2876821,
          "hits": [
            {
              "_index": "user",
              "_type": "customer",
              "_id": "1",
              "_score": 0.2876821,
              "_source": {
                "id": 1,
                "name": "hjx",
                "age": 24,
                "bir": "2021-09-10"
              }
            }
          ]
        }
      }
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值