栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

es 基础语句 详细笔记

es 基础语句 详细笔记


请新建html文件后查看,html代码如下:




  
  
  
  document
  


  
全部折叠 / 全部展开

使用ik分词器

  //ik_smart最粗粒度,不会有重复
  GET _analyze
  {
    "analyzer": "ik_smart",
    "text": "中国人民共和国"
  }
  //ik_max_word最细粒度的拆分,会有重复
  GET _analyze
  {
    "analyzer": "ik_max_word",
    "text": "中国人民共和国"
  }
  

查看es集群状态

GET _cat/health?v
  

创建索引

  //创建索引: PUT + 索引名
  PUT index1
  //创建索引时如果索引已存在会报错提示: 索引已存在
  PUT index1
  //不能用POST创建索引
  POST index2
  //创建索引并制定字段类型mapping
  PUT user
  {
    "mappings": {
      "properties": {
        "name": {
          "type": "text",
          "index": true
        },
        "password": {
          "type": "keyword",
          "index": false
        }
      }
    }
  }
  //查看索引的字段类型mapping
  GET user/_mapping
  

查看索引信息

  //查看索引信息
  GET index1
  //查看所有索引详细信息
  GET _cat/indices?v
  

删除索引

  //删除单个索引
  DELETe index1
  //删除全部索引
  DELETE *
  

添加数据(文档)

  //添加数据(文档)时不指定id,id随机生成,由于id是随机生成的,不符合幂等性,因此采用POST请求
  POST index1/_doc
  {
    "name": "华为手机2",
    "type": "华为",
    "price": 5999
  }
  //添加数据(文档)时指定id,由于id是指定的,符合幂等性,因此采用PUT请求
  PUT index1/_doc/1
  {
    "name": "指定了id",
    "age": 18
  }
  //使用_create添加数据
  PUT user/_create/1
  {
    "name": "name1",
    "password": "12"
  }
  

查询数据(文档)

  //根据id查询
  GET index1/_doc/1
  //查询一个索引的全部数据
  GET index1/_search
  //条件查询
  //简单条件查询
  GET index1/_search?q=type:"华为"
  GET index1/_search
  {
    "query": {
      "match": {
        "type": "小米"
      }
    }
  }
  //条件查询之精确查询(使用match_phrase则不会查询关键词分词,这意味着搜索"小华"不会拆分成"小"和"华"然后去库中检索,使用.keyword则不对数据进行分词,这意味着想搜索type="米"的数据,不会匹配到type="小米"的数据)
  GET index1/_search
  {
    "query": {
      "bool": {
        "must": [
          {
            "match_phrase": {
              "type": "米"
            }
          }
        ]
      }
    }
  }
  //条件查询条件与(使用must)
  GET index1/_search
  {
    "query": {
      "bool": {
        "must": [
          {
            "match": {
              "type": "小米"
            }
          },
          {
            "match": {
              "name": "1"
            }
          }
        ]
      }
    }
  }
  //条件查询之条件或(使用should)
  GET index1/_search
  {
    "query": {
      "bool": {
        "should": [
          {
            "match": {
              "type": "小米"
            }
          },
          {
            "match": {
              "type": "华为"
            }
          }
        ]
      }
    }
  }
  //条件查询之范围查询
  GET index1/_search
  {
    "query": {
      "bool": {
        "filter": {
          "range": {
            "age": {
              "gte": 18,
              "lte": 28
            }
          }
        }
      }
    }
  }
  //分页查询
  GET index1/_search
  {
    "from": 0,
    "size": 1
  }
  //查询部分字段
  GET index1/_search
  {
    "_source": ["name", "type"]
  }
  //查询排序
  GET index1/_search
  {
    "sort": [
      {
        "name.keyword": {
          "order": "desc"
        }
      }
    ]
  }
  GET index2/_search
  {
    "sort": [
      {
        "age": {
          "order": "asc"
        }
      }
    ]
  }
  //查询高亮
  //对每个字进行高亮
  GET index1/_search
  {
    "query": {
      "bool": {
        "must": [
          {
            "match_phrase": {
              "type": "小米"
            }
          }
        ]
      }
    },
    "highlight": {
      "fields": {
        "type": {}
      }
    }
  }
  //对整个词语进行高亮
  GET index1/_search
  {
    "query": {
      "bool": {
        "must": [
          {
            "match_phrase": {
              "type.keyword": "小米"
            }
          }
        ]
      }
    },
    "highlight": {
      "fields": {
        "type.keyword": {}
      }
    }
  }
  //自定义高亮标签
  //对整个词语进行高亮
  GET index1/_search
  {
    "query": {
      "bool": {
        "must": [
          {
            "match_phrase": {
              "type.keyword": "小米"
            }
          }
        ]
      }
    },
    "highlight": {
      "fields": {
        "type.keyword": {}
      },
      "pre_tags": "",
      "post_tags": ""
    }
  }
  //聚合
  //统计数量
  GET index1/_search
  {
    "aggs": {
      "name_group": {
        "terms": {
          "field": "name.keyword"
        }
      }
    }
  }
  //求均值
  GET index1/_search
  {
    "aggs": {
      "price_avg": {
        "avg": {
          "field": "price"
        }
      }
    }
  }
  

修改数据(文档)

  //完全覆盖修改,符合幂等性(_version每次都会增加1),采用PUT
  PUT index1/_doc/3
  {
    "name": "华为手机1",
    "type": "华为"
  }
  //局部修改,只有被修改的字段会发生改变,其他字段不会被改变,不符合幂等性(_version会根据情况选择是否加1,如果新的值不等于原来的值,则加1,否则_version不变)
  POST index1/_update/1
  {
    "doc": {
      "name": "局部修改"
    }
  }
  

删除数据(文档)

DELETE index1/_doc/1
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/628963.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号