ES操作及与表的对应关系

0.与表的对应关系

关系型数据库(比如Mysql) 非关系型数据库(ElasticSearch)
数据库Database 索引Index
表Table 类型Type(6.0版本之后在一个索引下面只能有一个,7.0版本之后取消了Type)
数据行Row 文档Document(JSON格式)
数据列Column 字段Field
约束 Schema 映射Mapping

1.管理性命令


/

  1. GET /_cat
  2. #查看节点状况
  3. GET /_cat/nodes?v
  4. #查看健康状况
  5. GET /_cat/health
  6. #查看所有的index
  7. GET /_cat/indices

2.index操作(索引->数据库)

索引包含一堆有相似结构的文档数据,比如可以有一个客户索引,商品分类索引,订单索引,索引有一个名称。一个index包含很多document,一个index就代表了一类类似的或者相同的document。比如说建立一个product index,商品索引,里面可能就存放了所有的商品数据,所有的商品document。

  1. #查index
  2. #查看所有的index
  3. GET /_cat/indices
  4. #查看某个index的信息
  5. GET /_cat/indices/.kibana_1
  6. #查看某个index的元数据信息
  7. GET /.kibana_1
  8. ##查看某个index的表结构
  9. GET /stu2/_mapping
  10. #新增Index
  11. #手动创建 需要在创建index时指定mapping信息
  12. #6.0版本一个Index只能创建一个type,名称随意
  13. PUT stu
  14. {
  15. "mappings": {
  16. "table1":{
  17. "properties":{
  18. "id":{
  19. "type":"keyword"
  20. },
  21. "name":{
  22. "type":"text"
  23. },
  24. "sex":{
  25. "type":"integer"
  26. },
  27. "birth":{
  28. "type":"date"
  29. }
  30. }
  31. }
  32. }
  33. }
  34. #自动创建 直接向一个不存在的Index插入数据,在插入数据时,系统根据数据的类型,自动推断mapping,自动创建mapping
  35. # POST /indexname/typename/id
  36. POST /stu2/table1/1
  37. {
  38. "age":20,
  39. "name":"jack"
  40. }
  41. #删除index
  42. DELETE /stu2
  43. #修改index 需要执行迁移操作,从一个index读取数据,写入一个新的index
  44. #判断是否存在index 404 - Not Found代表不存在 200代表存在
  45. HEAD /stu2

3.type(类型->表)【新版本这东西已经没用了,index其实就可以代替表了】

6.0版本之前每个索引里都可以有多个type;
6.0版本之后每个索引里面只能有一个Type,一般使用_doc代替了。
商品index,里面存放了所有的商品数据,商品document
商品type:product_id,product_name,product_desc,category_id,category_name,service_period
每一个type里面,都会包含一堆document

4.Mapping(映射->约束)

数据如何存放到索引对象上,需要有一个映射配置,包括:数据类型、是否存储、是否分词等。
Mapping用来定义Document中每个字段的类型,即所使用的分词器、是否索引等属性,非常关键等。创建Mapping 的代码示例如下:

  1. PUT stu stu类似于MySQL中的数据库名)
  2. {
  3. "mappings": { (可以理解为建表语句 create table关键字)
  4. "_doc":{ (相当于MySQL中表名)
  5. "properties":{ (相当于建表语句中要指定字段及字段类型的关键字)
  6. "id":{ (相当于具体某个字段)
  7. "type":"keyword""type"相当于要指定字段类型的关键字,"keyword "相当于字段的具体类型)
  8. },
  9. "name":{
  10. "type":"text"
  11. },
  12. "sex":{
  13. "type":"integer"
  14. },
  15. "birth":{
  16. "type":"date"
  17. }
  18. }
  19. }
  20. }
  21. }
  1. PUT stu/_doc/1001
  2. {
  3. "id":"001",
  4. "name":"红红",
  5. "sex":0,
  6. "birth":"1999-01-01"
  7. }
  1. GET stu/_search
  2. 输出结果如下:
  3. {
  4. "took" : 9,
  5. "timed_out" : false,
  6. "_shards" : {
  7. "total" : 5, (切片总数)
  8. "successful" :
  9. "skipped" : 0,
  10. "failed" : 0
  11. },
  12. "hits" : { (命中)
  13. "total" : 1, (命中总数一条)
  14. "max_score" : 1.0, (数据在里面存储的时候是有一个评分,这个评分是按照搜索的关键字跟文章的匹配度得来的,如果这个关键字在文章中出现频率较高,这个分数也相应的较大)
  15. "hits" : [
  16. {
  17. "_index" : "stu",
  18. "_type" : "_doc",
  19. "_id" : "1001",
  20. "_score" : 1.0,
  21. "_source" : { (具体的数据)
  22. "id" : "001",
  23. "name" : "红红",
  24. "sex" : 0,
  25. "birth" : "1999-01-01"
  26. }
  27. }
  28. ]
  29. }
  30. }

5.Document(文档->行)

文档是ES中的最小数据单元,一个document可以是一条客户数据,一条商品分类数据,一条订单数据,
通常用JSON数据结构表示,每个index下的type中,都可以去存储多个document。

6.Field(字段->列)

一个document里面有多个field,每个field就是一个数据字段。

  1. 商品type(表):
  2. document1(一条数据)
  3. {
  4. "product_id": "1",
  5. "product_name": "长虹电视机",
  6. "product_desc": "4k高清",
  7. "category_id": "3",
  8. "category_name": "电器",
  9. "service_period": "1年"
  10. }
  11. document2(一条数据)
  12. {
  13. "product_id": "2",
  14. "product_name": "基围虾",
  15. "product_desc": "纯天然,冰岛产",
  16. "category_id": "4",
  17. "category_name": "生鲜",
  18. "eat_period": "7天"
  19. }

7.分词操作

  1. #查
  2. #全表查询
  3. GET /stu/_search
  4. GET /stu
  5. #查询单个元素 GET /indexname/typename/id
  6. GET /stu/table1/3
  7. #增
  8. #POST /indexname/typename/id
  9. POST /stu/table1/3
  10. {
  11. "name":"jack"
  12. }
  13. #POST也可以实现更新操作,如果当前记录的ID不存在,就insert,存在就update 更新是全量更新
  14. POST /stu/table1/1
  15. {
  16. "name":"tom",
  17. "sex":0
  18. }
  19. #POST新增,不指定ID,就随机生成ID
  20. POST /stu/table1
  21. {
  22. "name":"marry"
  23. }
  24. #改 PUT
  25. #新增 PUT在新增时,必须指定id!
  26. #id存在就更新,不存在就插入,默认也是全量更新
  27. PUT /stu/table1/3
  28. {
  29. "name":"marry3",
  30. "sex":0
  31. }
  32. #不能增量更新
  33. PUT /stu/table1/3/_update
  34. {
  35. "name":"marry4"
  36. }
  37. # 4xxx开头的都是客户端错误
  38. # 405: 客户端发送的请求方式错误,例如只允许发POST,你发了PUT
  39. # 400 : 请求参数格式错误。没有按照人家指定的格式发参数
  40. POST /stu/table1/3/_update
  41. {
  42. "doc": {
  43. "name":"marry4"
  44. }
  45. }
  46. #删
  47. DELETE /stu/table1/3
  48. #判断是否存在
  49. HEAD /stu/table1/1

text(允许分词) keyword(不允许分词)

  1. GET /_analyze
  2. {
  3. "text": "I am a Chinese!"
  4. }
  5. # 默认的分词器,用来进行英文分词,按照空格分
  6. # 汉语按照字切分
  7. GET /_analyze
  8. {
  9. "text": "我是清朝老兵!"
  10. }
  11. GET /_analyze
  12. {
  13. "keyword": "我是清朝老兵!"
  14. }
  15. #ik_smart 智能分词。切分的所有单词的总字数等于 词的总字数 输入总字数=输出总字数
  16. #ik_max_word 最大化分词。 输入总字数 <= 输出总字数
  17. GET /_analyze
  18. {
  19. "analyzer": "ik_smart",
  20. "text": "我是清朝老兵!我请求为国出战!"
  21. }
  22. GET /_analyze
  23. {
  24. "analyzer": "ik_smart",
  25. "text": "我是中国人"
  26. }
  27. GET /_analyze
  28. {
  29. "analyzer": "ik_max_word",
  30. "text": "我喜欢抽烟喝酒烫头洗屁股眼子!"
  31. }

8.子属性

  1. java中:
  2. public class Person{
  3. public String name;
  4. public Address address;
  5. }
  6. public class Address{
  7. public String provinceName;
  8. }
  9. provinceName称为是Person类的 级联(层级联系)属性, 或子属性(属性的属性)
  10. json中:
  11. person:
  12. {
  13. age: 20
  14. address:{
  15. "provinceName":"广东"
  16. }
  17. }
  18. 注意:
  19. "name" : {
  20. "type" : "text",
  21. "fields" : {
  22. "aaa" : {
  23. "type" : "keyword",
  24. "ignore_above" : 256
  25. }
  26. }
  27. }
  28. text类型的字段,如果将来需要聚合,一定需要为其设置一个子属性,子属性的类型必须是keyword类型!

9.批量导入数据语法

导入数据:
#_bulk代表批量写
#格式 : {“action”: {metadata}}\n {data}
# action: insert,update,delete, index(upsert): 存在就更新,不存在就插入
#metadata 指定当前向哪个index,哪个type,哪个id进行写
#_id: id _index:xxx _type:哪个type