ElasticSearch数据管理

1 概述

  ES是面向文档(document oriented)的,这意味着它可以存储整个对象或文档(document)。然而它不仅仅是存储,还会索引(index)每个文档的内容使之可以被搜索。在ES中,你可以对文档(而非成行成列的数据)进行索引、搜索、排序、过滤。ES使用JSON作为文档序列化格式。JSON现在已经被大多语言所支持,而且已经成为NoSQL领域的标准格式。
2 基本操作
2.1 创建索引
格式: PUT /索引名称
举例: PUT /es_db
成功示例:
    {
        "acknowledged" : true,
        "shards_acknowledged" : true,
        "index" : "es_db"
       }

2.2 查询索引

格式: GET /索引名称
举例: GET /es_db
成功示例:
     {
      "es_db" : {
        "aliases" : { },
        "mappings" : { },
        "settings" : {
          "index" : {
            "creation_date" : "1657543636448",
            "number_of_shards" : "1",
            "number_of_replicas" : "1",
            "uuid" : "M4uDwbuCRn21pMCrAVy6QA",
            "version" : {
              "created" : "7060199"
            },
            "provided_name" : "es_db"
          }
        }
      }
    }

2.3 删除索引

格式: DELETE /索引名称
举例: DELETE /es_db    
成功示例:
        {
          "acknowledged" : true
        }

2.4 添加文档

格式: PUT /索引名称/类型/id
举例: PUT /es_db/_doc/1
        {
        "name": "张三",
        "sex": 1,
        "age": 25,
        "address": "广州天河公园",
        "remark": "java developer"
        }
成功示例:
        {
          "_index" : "es_db",
          "_type" : "_doc",
          "_id" : "1",
          "_version" : 1,
          "result" : "created",
          "_shards" : {
            "total" : 2,
            "successful" : 1,
            "failed" : 0
          },
          "_seq_no" : 0,
          "_primary_term" : 1
        }

 2.5 修改文档

 格式: PUT /索引名称/类型/id

举例: 
    PUT /es_db/_doc/1
    {
        "name": "檀潇兵老师",
        "sex": 1,
        "age": 25,
        "address": "张家界森林公园",
        "remark": "php developer assistant"                
    }
成功示例:
        {
          "_index" : "es_db",
          "_type" : "_doc",
          "_id" : "1",
          "_version" : 2,
          "result" : "updated",
          "_shards" : {
            "total" : 2,
            "successful" : 1,
            "failed" : 0
          },
          "_seq_no" : 6,
          "_primary_term" : 1
        }
注意:POST和PUT都能起到创建/更新的作用
1、需要注意的是==PUT==需要对一个具体的资源进行操作也就是要确定id才能进行==更新/创建,而==POST==是可以针对整个资源集合进行操作的,如果不写id就由ES生成一个唯一id进行==创建==新文档,如果填了id那就针对这个id的文档进行创建/更新
2、PUT只会将json数据都进行替换, POST只会更新相同字段的值
3、PUT与DELETE都是幂等性操作, 即不论操作多少次, 结果都一样
 
2.6 查询文档
格式: GET /索引名称/类型/id
举例: GET /es_db/_doc/1
成功示例:
        {
          "_index" : "es_db",
          "_type" : "_doc",
          "_id" : "1",
          "_version" : 2,
          "_seq_no" : 6,
          "_primary_term" : 1,
          "found" : true,
          "_source" : {
            "name" : "檀潇兵老师",
            "sex" : 1,
            "age" : 25,
            "address" : "张家界森林公园",
            "remark" : "php developer assistant"
          }
        }

2.7 删除文档

格式: DELETE /索引名称/类型/id
举例: DELETE /es_db/_doc/1
成功示例:
         {
          "_index" : "es_db",
          "_type" : "_doc",
          "_id" : "1",
          "_version" : 3,
          "result" : "not_found",
          "_shards" : {
            "total" : 2,
            "successful" : 1,
            "failed" : 0
          },
          "_seq_no" : 8,
          "_primary_term" : 1
        }

3 查询操作

3.1 查询当前类型中的所有文档 _search

格式: GET /索引名称/类型/_search
举例: GET /es_db/_doc/_search
SQL:  select * from student

3.2  条件查询

#查询age等于28岁的
格式: GET /索引名称/类型/_search?q=*:***
举例: GET /es_db/_doc/_search?q=age:28
SQL:  select * from student where age = 28

3.3 范围查询

#查询age在25至26岁之间的
格式: GET /索引名称/类型/_search?q=***[25 TO 26]
举例: GET /es_db/_doc/_search?q=age[25 TO 26]
SQL:  select * from student where age between 25 and 26

3.4 根据多个id进行批量查询

格式: GET /索引名称/类型/_mget
举例: GET /es_db/_doc/_mget 
{
 "ids":["1","2"]  
 }
SQL:  select * from student where id in (1,2)    

3.5 查询小于等于某个数值的

年龄小于28岁的(包括28岁)
格式: GET /索引名称/类型/_search?q=age:<=**
举例: GET /es_db/_doc/_search?q=age:<=28
SQL:  select * from student where age <= 28

3.6 查询大于某个数值的

#查询年龄大于28岁的(不包括28岁)
格式: GET /索引名称/类型/_search?q=age:>**
举例: GET /es_db/_doc/_search?q=age:>28
SQL:  select * from student where age > 28

3.7 分页查询

格式: GET /索引名称/类型/_search?q=age[25 TO 26]&from=0&size=1
举例: GET /es_db/_doc/_search?q=age[25 TO 26]&from=0&size=1
SQL:  select * from student where age between 25 and 26 limit 0, 1 

3.8 对查询结果只输出某些字段

格式: GET /索引名称/类型/_search?_source=字段,字段
举例: GET /es_db/_doc/_search?_source=name,age
SQL:  select name,age from student

3.9 对查询结果排序

格式: GET /索引名称/类型/_search?sort=字段 desc
举例: GET /es_db/_doc/_search?sort=age:desc
SQL:  select * from student order by age desc

 

posted @ 2022-07-11 21:32  檀潇兵  阅读(317)  评论(0编辑  收藏  举报