[ ElasticSearch基础 ]


Es基础操作

官方文档

Python的官方文档

前提条件

  1. 安装es
  2. kibana
  3. ik分词器

索引的操作

  • 相当于表

创建 修改(不允许) 判断是否存在 查询

# 创建索引
# PUT 索引名称
PUT test_index

# PUT 索引 不允许修改索引信息
# aliases 别名
PUT test_index_1
{
  "aliases": {
    "test1": {}
  }
}

# DELETE 删除索引
DELETE test_index_1



# HEAD索引 http状态码 200, 404
head test_index

# 查询索引
GET test_index

# 查询所有索引
GET _cat/indices

创建文档

  • 相当于表里面的数据
# 创建索引
PUT test_doc

# 在索引里面
# 创建_doc文档 
# 手动创建/1001为_id唯一性标识
PUT test_doc/_doc/1001
{
  "id": 1001,
  "name": "张三",
  "age": 30
}

# 自动生成_id唯一性标识
POST test_doc/_doc
{
  "id": 1002,
  "name": "lisi",
  "age": 40
}

# 查询文档
GET test_doc/_doc/1001

# 查询索引中所有的文档数据
GET test_doc/_search

# 修改文档数据
PUT test_doc/_doc/1001
{
  "id": 10011,
  "name": "zhangsan1213",
  "age": 100

}

文档搜索

PUT test_query
# 空格保存的时候会自动进行分词
# 批量插入
PUT test_query/_bulk
{"index":{"_index":"test_query", "_id":"1001"}}
{"id":"1001", "name":"zhang san", "age": 30}
{"index":{"_index":"test_query", "_id":"1002"}}
{"id":"1002", "name":"li si", "age": 40}
{"index":{"_index":"test_query", "_id":"1003"}}
{"id":"1003", "name":"wang wu", "age": 50}
{"index":{"_index":"test_query", "_id":"1004"}}
{"id":"1004", "name":"zhangsan", "age": 30}
{"index":{"_index":"test_query", "_id":"1005"}}
{"id":"1005", "name":"lisi", "age": 40}
{"index":{"_index":"test_query", "_id":"1006"}}
{"id":"1006", "name":"wangwu", "age": 50}

# query查询
# match是分词查询ES会将数据分词(关键词)保存
# name 查询的字段
# zhangsan字段的值
GET test_query/_search
{
  "query":{
    "match": {
      "name": "zhang li"
    }
  }
}

# 完整查询term
GET test_query/_search
{
  "query":{
    "term": {
      "name": {"value":"zhangsan"}
    }
  }
}

# 对查询结果进行限制 _source
GET test_query/_search
{
  "_source":["name","age"], 
  "query":{
    "match":{
      "name": "zhang"
    }
  }
}

排序-分页查询

# 组合查询 使用should, 会组合在两个两个条件
GET test_query/_search
{
  "query":{
    "bool": {
      "should": [
        {"match": {"name": "zhang"}},{"match":{"age": "40"}}
      ]
    }
  }
}

# 排序查询
GET test_query/_search
{
  "query": {
    "match": {
      "name": "zhang li"
    }
  },
  "sort":{
    "age":{
      "order": "desc"
    }
  }
}

# 分页查询
# form 相当于offset
# size 相当于limit
GET test_query/_search
{
  "query": {
    "match": {
      "name": "zhang li"
    }
  },
  "from": 0,
  "size": 2
}

分组-求和-平均

# aggs分组查询
# ageGroup为分组名字
# terms为分组的字段
# field为分组字段
GET test_query/_search
{
  "aggs": {
    "ageGroup": {
      "terms": {
        "field": "age"
      }
    }
  }
}


# 分组后求和
GET test_query/_search
{
  "aggs": {
    "ageGroup": {
      "terms": {
        "field": "age"
      },
      "aggs":{
        "ageSum":{
          "sum":{
            "field": "age"
          }
        }
      }
    }
  }
}

# 求年龄平均值
# aggs分组
# agsAvg分组起名字
# avg平均聚合函数
# field选择字段
GET test_query/_search
{
  "aggs":{
    "agsAvg":{
      "avg": {
        "field": "age"
      }
    }
  }
}

取前几名-排序再取前几名

# 获取前几名(默认为id排序)
GET test_query/_search
{
  "aggs": {
    "top3": {
      "top_hits": {
        "size": 3
      }
    }
  },
  "size": 0
}


# 按照年龄排序, 取前三名
GET test_query/_search
{
  "aggs": {
    "top3": {
      "top_hits": {
        "sort": [{"age":{"order": "desc"}}], 
        "size": 3
      }
    }
  },
  "size": 0
}

创建模板

# 配置索引设置信息(只能一个一个改)
PUT test_temp
GET test_temp_1
PUT test_temp_1
{
  "settings": {
    "number_of_shards": 2
  }
}


# 创建模板_template
# myTemplate名字
# index_patterns匹配规则
PUT _template/mytemplate
{
  "index_patterns": ["my*"],
  "settings": {
    "index":{
      "number_of_shards": "2"
    }
  },
  "mappings": {
    "properties": {
      "now": {
        "type": "date",
        "format": "yyyy/MM/dd"
      }
    }
  }
}


PUT my_test_temp
GET my_test_temp
DELETE my_test_temp

分词器配置

# analyze数据分析
# analyzer分析器
# standard标准分析器,chinese中国分词(不友好), ik_smart分词插件(默认推荐),ik_max_word(更加细)
# 基本是根据空格来分词
GET _analyze
{
  "analyzer": "ik_max_word",
  "text": ["我是一个三好学生"]
}

# 可以根据需求修改配置文件, 自定义分词的效果, 例如:把《我是一个三好学生》变成一个整体

IK分词器实例使用

# ik实际应用, 创建索引配置ik分词
PUT test_china
{
  "mappings": {
    "properties": {
      "content":{
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      }
    }
  }
}


PUT test_china/_doc/1
{
  "content": "欢迎学习中文"
}

GET test_china/_search
{
  "query": {
    "match": {
      "content": "中文"
    }
  }
}

索引方面的主要点 - _index当前这条数据属于的索引 - _id - _version版本号 - _srq_no索引级别的版本号 - _primary_term是一个整数, 每当primary shard发生重新分配, 会递增1,主要用来恢复数据,处理文档

# aliases别名
# settings索引设置
# mapping映射, 定义字段等信息
PUT test_index
{
  "aliases": {
    "NAME": {}
  },
  "settings": {},
  "mappings": {}
}

# 什么是索引, 
# 1. 标识源文件数据
# 2. 索引文件(正排索引, !!倒排索引:重要!!)


# 创建索引
# 7.x 以后的版本创建时不能指定type
# 如果有需求, 就创建多个索引
# 使用