01.ElasticSearch (RestFul Api 基本操作)

创建操作

  • 创建索引(库)

    #number_of_shards  设置分片
    #number_of_replicas 设置备份
    PUT 索引
    {
      "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 0
      }
    }
    
  • 创建映射类型(表)

    #字段名: 相当于数据库中的列名
    #type: 类型,例如:text,short,data,integer,object等
    #keyword: 存储数据时候,不会分词建立索引
    #index: 是否索引,默认为true,不设置索引的话就无法查询该字段
    #store: 是否存储,默认为false
    #analyzer: 分词器,推荐使用 ik_max_word,即ik分词器
    PUT 索引/_mapping/类型
    {
      "properties": {
        "字段名": {
          "属性名": "属性值"
        }
      }
    }
    

添加/更新操作

  • 无id添加数据

    POST 索引/类型
    {
      "字段名": "value"
    }
    
  • 有id添加

    # 如果不存在该id,则为添加操作
    # 如果存在该id,则为更新操作
    PUT 索引/类型/_id
    {
      "字段名": "value"
    }
    

删除操作

  • 删除索引/类型/数据

    DELETE 索引
    
    DELETE 索引/类型
    
    DELETE 索引/类型/_id
    

查询操作

  • 查询所有

    #完整写法
    GET 索引/_search
    {
      "query": {
        "match_all": {
          
        }
      }
    }
    
    #简化写法
    GET 索引/_search
    
    
  • 通过id查询

    GET 索引/类型/_id
    
    #展示部分 相当于selct (字段1,字段2...) from
    GET 索引/类型/_id?_source=字段1,字段2
    
  • 条件查询

    #单条件查询 match
    GET 索引/_search
    {
      "query": {
        "match": {
          "字段名": "value"
        }
      }
    }
    
    
    #多条件查询: bool/must/must_not (交集 and)
    #bool: 条件拼接
    #must: 匹配的条件/=
    #must_not: 不匹配的条件/!=
    GET 索引/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "字段名": "value"
              }
            },
            ......
          ],
          "must_not": [
            {
              "match": {
                "字段名": "value"
              }
            }
          ]
        }
      }
    }
    
    
    # 多条件查询: should
    #should: 并集/or
    GET 索引/_search
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "字段名": "value"
              }
            },
            {
              "match": {
                "字段名": "value"
              }
            },
            ......
          ]
        }
      }
    }
    
  • 其他操作

    # 精准查询: term
    # 需要查询的该字段不进行分词
    GET 索引/_search
    {
      "query": {
        "term": {
          "字段名": {
            "value": "值"
          }
        }
      }
    }
    
    
    # 区间查询: range
    # gt: 大于
    # lt: 小于
    # gte: 大于等于
    # lte: 大于等于
    GET 索引/_search
    {
      "query": {
        "range": {
          "字段名": {
            "gte": 10,
            "lte": 20
          }
        }
      }
    }
    
    
    # 排序操作: sort
    # asc: 正序
    # desc: 倒叙
    GET 索引/_search
    {
      "query": {
        "match_all": {
          
        }
      },
      "sort": [
        {
          "字段": {
            "order": "desc"
          }
        },
        ......
      ]
    }
    
    # 分页操作: 
    # form: 从索引几开始(从0开始)
    # size: 长度
    GET 索引/_search
    {
      "query": {
        "match_all": {
          
        }
      },
      "from": 0,
      "size": 5
    }
    
    
发布了31 篇原创文章 · 获赞 0 · 访问量 166

猜你喜欢

转载自blog.csdn.net/weixin_46759279/article/details/105723322