_source,是否启用原始数据,默认启用

includes,选择包含哪些字段,excludes:去除哪些字段

字段

不分词

  • keyword
  • constant_keyword
  • wildcard


数值类型

  • long
  • integer
  • short
  • byte
  • unsigned_long
  • double
  • float
  • half_float
  • scaled_float: 收缩浮点类型,可以指定收缩因子

日期类型

  • date
  • date_nanos


Object类型

array类型

range类型

  • integer_range
  • float_range
  • long_range
  • double_range
  • date_range
  • ip_range

适用场景:体重、日期范围,身高

join父子类型

  1. PUT ckiss-company-001
  2. {
  3. "mappings": {
  4. "properties": {
  5. "companyId": {
  6. "type": "integer"
  7. },
  8. "companyName": {
  9. "type": "keyword"
  10. },
  11. "join_father_child": {
  12. "type": "join",
  13. "relations": {
  14. "father": "child"
  15. }
  16. }
  17. }
  18. }
  19. }
  20. #填充集团数据
  21. PUT ckiss-company-001/_doc/1
  22. {
  23. "companyId": 1,
  24. "companyName": "华住会集团",
  25. "join_father_child":{
  26. "name": "father"
  27. }
  28. }
  29. #子公司数据, routing保证父子文档处于同一分片中
  30. PUT ckiss-company-001/_doc/2?routing=1
  31. {
  32. "companyId": 2,
  33. "companyName": "上海华住",
  34. "join_father_child": {
  35. "name": "child",
  36. "parent": "1"
  37. }
  38. }
  39. #子公司数据
  40. PUT ckiss-company-001/_doc/3?routing=1
  41. {
  42. "companyId": 3,
  43. "companyName": "杭州华住",
  44. "join_father_child": {
  45. "name": "child",
  46. "parent": "1"
  47. }
  48. }
  49. # 查询:根据父ID查询所有子节点
  50. GET ckiss-company-001/_search
  51. {
  52. "query": {
  53. "parent_id": {
  54. "type": "child",
  55. "id": 1
  56. }
  57. }
  58. }
  59. # 查询具有子数据的父数据列表
  60. GET ckiss-company-001/_search
  61. {
  62. "query": {
  63. "has_child": {
  64. "type": "child",
  65. "query": {
  66. "match_all": {}
  67. }
  68. }
  69. }
  70. }
  71. # 查询具有父节点的子数据列表
  72. GET ckiss-company-001/_search
  73. {
  74. "query": {
  75. "has_parent": {
  76. "parent_type": "father",
  77. "query": {
  78. "match_all": {}
  79. }
  80. }
  81. }
  82. }

Nested 嵌套键值对

案例练习:

  1. 假设一个公司有很多区域信息,每一组都是一个键值对,包括省份与城市
  2. 错误的案例查询使用array类型

错误情况

  1. PUT ckiss-company-002
  2. {
  3. "mappings": {
  4. "properties": {
  5. "companyId": {
  6. "type": "integer"
  7. },
  8. "companyName": {
  9. "type": "keyword"
  10. },
  11. "area": {
  12. "properties": {
  13. "province": {
  14. "type": "keyword"
  15. },
  16. "city": {
  17. "type": "keyword"
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }
  24. # 填充数组
  25. PUT ckiss-company-002/_doc/1
  26. {
  27. "companyId": 1,
  28. "companyName": "华住集团",
  29. "area": [
  30. {
  31. "province": "HN",
  32. "city": "CS"
  33. },
  34. {
  35. "province": "ZJ",
  36. "city": "HZ"
  37. }
  38. ]
  39. }
  40. # 按照省份与城市查询
  41. GET ckiss-company-002/_search
  42. {
  43. "query": {
  44. "bool": {
  45. "must": [
  46. {
  47. "term": {
  48. "area.province": {
  49. "value": "HN"
  50. }
  51. }
  52. },
  53. {
  54. "term": {
  55. "area.city": {
  56. "value": "HZ"
  57. }
  58. }
  59. }
  60. ]
  61. }
  62. }
  63. }

响应结果有问题

  1. "hits" : [
  2. {
  3. "_index" : "ckiss-company-002",
  4. "_type" : "_doc",
  5. "_id" : "1",
  6. "_score" : 0.723315,
  7. "_source" : {
  8. "companyId" : 1,
  9. "companyName" : "华住集团",
  10. "area" : [
  11. {
  12. "province" : "HN",
  13. "city" : "CS"
  14. },
  15. {
  16. "province" : "ZJ",
  17. "city" : "HZ"
  18. }
  19. ]
  20. }
  21. }
  22. ]

正确做法

  • 使用nested 类型
  • 在area字段下增加 type=nested
  • 查询语法中增加 nested 标记 ```json DELETE ckiss-company-002

PUT ckiss-company-002 { “mappings”: { “properties”: { “companyId”: { “type”: “integer” }, “companyName”: { “type”: “keyword” }, “area”: { “type”: “nested”, “properties”: { “province”: { “type”: “keyword” }, “city”: { “type”: “keyword” } } } } } }

  1. 造数据
  2. ```json
  3. PUT ckiss-company-002/_doc/1
  4. {
  5. "companyId": 1,
  6. "companyName": "华住集团",
  7. "area": [
  8. {
  9. "province": "HN",
  10. "city": "CS"
  11. },
  12. {
  13. "province": "ZJ",
  14. "city": "HZ"
  15. }
  16. ]
  17. }
  18. GET ckiss-company-002/_search
  19. {
  20. "query": {
  21. "nested": {
  22. "path": "area",
  23. "query": {
  24. "bool": {
  25. "must": [
  26. {
  27. "term": {
  28. "area.province": {
  29. "value": "HN"
  30. }
  31. }
  32. },
  33. {
  34. "term": {
  35. "area.city": {
  36. "value": "HZ"
  37. }
  38. }
  39. }
  40. ]
  41. }
  42. }
  43. }
  44. }
  45. }

无响应内容

字段别名

  • alias: 字段类型关键字
  • path:设定别名代理的字段

    案例:假定基本信息,其中部分字段原来定义的是一个意思,后续又改变了

// TODO

fields 单值多字段

假设需要依据一个字段提供多种不同的检索要求,fields可以解决这种问题

案例: 假定一个基本信息,公司名称既能提供精确检锁,也能提供范围查询;公司收入也需要提供精确检索和数值比对的范围检索

  1. DELETE ckiss-company-002
  2. PUT ckiss-company-002
  3. {
  4. "mappings": {
  5. "properties": {
  6. "companyId": {
  7. "type": "integer"
  8. },
  9. "companyName": {
  10. "type": "keyword",
  11. "fields": {
  12. "text": {
  13. "type": "text"
  14. }
  15. }
  16. },
  17. "income": {
  18. "type": "integer",
  19. "fields": {
  20. "sortKey": {
  21. "type": "keyword"
  22. }
  23. }
  24. }
  25. }
  26. }
  27. }
  28. PUT ckiss-company-002/_doc/1
  29. {
  30. "companyId": 1,
  31. "companyName" : "华住集团",
  32. "income": 100000
  33. }
  34. PUT ckiss-company-002/_doc/2
  35. {
  36. "companyId": 2,
  37. "companyName" : "美团集团",
  38. "income": 1000
  39. }
  40. # 范围查询
  41. GET ckiss-company-002/_search
  42. {
  43. "query": {
  44. "bool": {
  45. "filter": {
  46. "range": {
  47. "income": {
  48. "gte": 1000,
  49. "lte": 20000
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. # 键值查询
  57. GET ckiss-company-002/_search
  58. {
  59. "query": {
  60. "bool": {
  61. "filter": {
  62. "term": {
  63. "income.sortKey": "1000"
  64. }
  65. }
  66. }
  67. }
  68. }

Copy_to 字段复制

  • 原始值 ,可以指定复制多个字段,且按照顺序复制
  • 历史版本是_all字段,后来取消;增加了copy_to 弥补
  • copy_to,数据字段关键字

案例:

假定公司基本信息有地址,地址信息需要省份、城市、区县、等组成

  1. DELETE ckiss-company-002
  2. PUT ckiss-company-002
  3. {
  4. "mappings": {
  5. "properties": {
  6. "companyId": {
  7. "type": "integer"
  8. },
  9. "companyName": {
  10. "type": "keyword"
  11. },
  12. "province": {
  13. "type": "text",
  14. "copy_to": "address"
  15. },
  16. "city": {
  17. "type": "text",
  18. "copy_to": "address"
  19. },
  20. "county":{
  21. "type": "text",
  22. "copy_to": "address"
  23. },
  24. "address": {
  25. "type": "text"
  26. }
  27. }
  28. }
  29. }
  30. PUT ckiss-company-002/_doc/1
  31. {
  32. "companyId": 1,
  33. "companyName": "美团集团",
  34. "province": "HN",
  35. "city": "CS",
  36. "county": "Yuelu"
  37. }
  38. PUT ckiss-company-002/_doc/2
  39. {
  40. "companyId": 2,
  41. "companyName": "美团集团",
  42. "province": "HN",
  43. "city": "CS",
  44. "county": "Yuelu",
  45. "address": "Hello"
  46. }
  47. GET ckiss-company-002/_search
  48. {
  49. "query": {
  50. "match": {
  51. "address": "Hello"
  52. }
  53. }
  54. }