索引操作

  1. PUT human
  2. {
  3. "settings":{
  4. "number_of_shards":1,
  5. "number_of_replicas":1
  6. },
  7. "mappings":{
  8. "dynamic":"false",
  9. "properties":{
  10. "blackFlag":{
  11. "type":"byte"
  12. },
  13. "brandName":{
  14. "type":"keyword",
  15. "index":false
  16. },
  17. "keyWord":{
  18. "type":"text",
  19. "analyzer":"ik_max_word"
  20. },
  21. "onlineDate":{
  22. "type":"long"
  23. },
  24. "unitPrice":{
  25. "type":"scaled_float",
  26. "scaling_factor":100
  27. },
  28. "publicAttrList":{
  29. "type":"nested",
  30. "dynamic":"false",
  31. "properties":{
  32. "publicAttrCode":{
  33. "type":"keyword"
  34. },
  35. "publicAttrName":{
  36. "type":"keyword",
  37. "index":false
  38. },
  39. "publicAttrValue":{
  40. "type":"keyword"
  41. },
  42. "publicAttrValueCode":{
  43. "type":"keyword"
  44. },
  45. "publicAttrValueName":{
  46. "type":"keyword",
  47. "index":false
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
  54. GET human
  55. DELETE human
  1. DELETE test_object_nested
  2. PUT test_object_nested
  3. {
  4. "mappings": {
  5. "dynamic": "false",
  6. "properties": {
  7. "name_object": {
  8. "type": "object",
  9. "properties": {
  10. "first": {
  11. "type": "keyword"
  12. },
  13. "last": {
  14. "type": "keyword"
  15. }
  16. }
  17. },
  18. "name_nested": {
  19. "type": "nested",
  20. "properties": {
  21. "first": {
  22. "type": "keyword"
  23. },
  24. "last": {
  25. "type": "keyword"
  26. }
  27. }
  28. }
  29. }
  30. }
  31. }
  32. GET test_object_nested/_mapping
  33. POST test_object_nested/_doc/1
  34. {
  35. "name_object":[
  36. {
  37. "first" : "John",
  38. "last" : "Smith"
  39. },
  40. {
  41. "first" : "Alice",
  42. "last" : "White"
  43. }
  44. ],
  45. "name_nested":[
  46. {
  47. "first" : "John",
  48. "last" : "Smith"
  49. },
  50. {
  51. "first" : "Alice",
  52. "last" : "White"
  53. }
  54. ]
  55. }
  56. GET test_object_nested/_search
  57. # 对象数组查询
  58. GET test_object_nested/_search
  59. {
  60. "query": {
  61. "bool": {
  62. "must": [
  63. {
  64. "term": {
  65. "name_object.first": {
  66. "value": "John"
  67. }
  68. }
  69. },
  70. {
  71. "term": {
  72. "name_object.last": {
  73. "value": "White"
  74. }
  75. }
  76. }
  77. ]
  78. }
  79. }
  80. }
  81. # 嵌套对象数组查询
  82. GET test_object_nested/_search
  83. {
  84. "query": {
  85. "bool": {
  86. "must": [
  87. {
  88. "nested": {
  89. "path": "name_nested",
  90. "query": {
  91. "bool": {
  92. "must": [
  93. {
  94. "term": {
  95. "name_nested.first": {
  96. "value": "John"
  97. }
  98. }
  99. },
  100. {
  101. "term": {
  102. "name_nested.last": {
  103. "value": "White"
  104. }
  105. }
  106. }
  107. ]
  108. }
  109. }
  110. }
  111. }
  112. ]
  113. }
  114. }
  115. }
  1. DELETE book
  2. # 索引添加文档
  3. POST book/_doc/1
  4. {
  5. "name": "ES in aciton",
  6. "price": 11.22,
  7. "stockFlag": true,
  8. "count": 12,
  9. "author": {
  10. "name": "some gay",
  11. "age": 30
  12. },
  13. "labelList": ["ES", "IT"],
  14. "date": null
  15. }
  16. # 查看文档
  17. GET book/_search
  18. # 查看指定索引的Mapping
  19. GET book/_mapping?pretty
  20. # 新增文档新字段
  21. POST book/_doc/2
  22. {
  23. "country": "China"
  24. }
  1. DELETE book
  2. # 设置索引Mapping
  3. PUT book
  4. {
  5. "mappings": {
  6. "dynamic": "false",
  7. "properties": {
  8. "country": {
  9. "type": "keyword"
  10. }
  11. }
  12. }
  13. }
  14. # 查看指定索引的Mapping
  15. GET book/_mapping?pretty
  16. # 索引添加文档
  17. POST book/_doc/1
  18. {
  19. "name": "ES in aciton",
  20. "type": "IT",
  21. "country": "china"
  22. }
  23. # 查看文档
  24. GET book/_search
  25. # 搜索Mapping有的字段
  26. GET book/_search?q=country:china
  27. # 搜索Mapping没有的字段
  28. GET book/_search?q=type:IT
  29. # 新增Mapping里面没有的字段
  30. POST book/_doc/2
  31. {
  32. "age": 110
  33. }
  34. # 搜索Mapping没有的字段
  35. GET book/_search?q=age:110
  1. DELETE book
  2. # 设置索引Mapping
  3. PUT book
  4. {
  5. "mappings": {
  6. "dynamic": "strict",
  7. "properties": {
  8. "country": {
  9. "type": "keyword"
  10. }
  11. }
  12. }
  13. }
  14. # 查看指定索引的Mapping
  15. GET book/_mapping?pretty
  16. # 新增Mapping里面有的字段
  17. POST book/_doc/1
  18. {
  19. "country": "china"
  20. }
  21. # 查看文档
  22. GET book/_search
  23. # 索引添加文档
  24. POST book/_doc/1
  25. {
  26. "name": "ES in aciton",
  27. "type": "IT",
  28. "country": "china"
  29. }
  30. # 搜索Mapping有的字段
  31. GET book/_search?q=country:china
  1. DELETE book
  2. # 设置索引Mapping
  3. PUT book
  4. {
  5. "mappings": {
  6. "dynamic": "strict",
  7. "properties": {
  8. "country": {
  9. "type": "keyword"
  10. }
  11. }
  12. }
  13. }
  14. # 新增Mapping里面有的字段
  15. POST book/_doc/1
  16. {
  17. "country": "china"
  18. }
  19. # 查看指定索引的Mapping
  20. GET book/_mapping?pretty
  21. # 查看文档
  22. GET book/_search
  23. GET book_alias/_search
  24. POST _aliases
  25. {
  26. "actions": [
  27. {
  28. "add": {
  29. "index": "book",
  30. "alias": "book_alias"
  31. }
  32. }
  33. ]
  34. }
  35. # 别名列表
  36. GET _cat/aliases?v

文档操作

  1. DELETE book
  2. # 设置索引Mapping
  3. PUT book
  4. {
  5. "mappings": {
  6. "dynamic": "false",
  7. "properties": {
  8. "country": {
  9. "type": "keyword"
  10. }
  11. }
  12. }
  13. }
  14. # 查看指定索引的Mapping
  15. GET book/_mapping?pretty
  16. # 指定 IP 创建文档
  17. # 再次请求就是覆盖更新
  18. POST book/_doc/1
  19. {
  20. "name": "ES in aciton",
  21. "type": "IT",
  22. "country": "china"
  23. }
  24. # 不指定 ID 创建,ES会随机分配ID
  25. # 再次请求就继续创建,不幂等
  26. POST book/_doc
  27. {
  28. "name": "ES in aciton",
  29. "type": "IT",
  30. "country": "china"
  31. }
  32. # 查看文档
  33. GET book/_search
  34. # 部分更新
  35. POST book/_update/1
  36. {
  37. "doc": {
  38. "name": "Lucene",
  39. "age": 12
  40. }
  41. }
  42. # 使内外部版本号,_seq_no _primary_term
  43. # 可确一个文档的版本号,两者要跟文档一致
  44. PUT book/_doc/1?if_seq_no=16&if_primary_term=1
  45. {
  46. "name": "ES in aciton1",
  47. "type": "IT",
  48. "country": "china"
  49. }
  50. # 使用外部版本号,一定要比内部version
  51. PUT book/_doc/1?version=19&version_type=external
  52. {
  53. "name": "ES in aciton1",
  54. "type": "IT",
  55. "country": "china"
  56. }
  57. # 根据 id 查找
  58. GET book/_doc/1

搜索与聚合

  1. # 删除指定索引
  2. DELETE book
  3. GET book
  4. # 设置索引Mapping
  5. PUT book
  6. {
  7. "mappings": {
  8. "dynamic": "false",
  9. "properties": {
  10. "nameText": {
  11. "type": "text",
  12. "analyzer": "standard"
  13. },
  14. "nameKeyWord": {
  15. "type": "keyword"
  16. },
  17. "country": {
  18. "type": "keyword"
  19. },
  20. "price": {
  21. "type": "float"
  22. }
  23. }
  24. }
  25. }
  26. # 查看指定索引的Mapping
  27. GET book/_mapping?pretty
  28. # 索引添加文档
  29. POST book/_bulk
  30. {"create" : {"_id": 1}}
  31. { "nameText": "ES in action ES","nameKeyWord": "ES in action ES","country": "China","price":100}
  32. {"create" : {"_id": 2}}
  33. { "nameText": "ES in one action","nameKeyWord": "ES in one action","country": "Japan","price":200}
  34. {"create" : {"_id": 3}}
  35. { "nameText": "ES in one two action","nameKeyWord": "ES in one two action","country": "America","price":300}
  36. {"create" : {"_id": 4}}
  37. { "nameText": "What is ES","nameKeyWord": "What is ES","country": "China","price":400}
  38. {"create" : {"_id": 5}}
  39. { "nameText": "one thing is good","nameKeyWord": "one thing is good","country": "America","price":500}
  40. # 查看文档
  41. GET book/_search
  42. # 查看指定索引指定字段的分词效果
  43. GET book/_analyze
  44. {
  45. "field": "nameText",
  46. //"field": "nameKeyWord",
  47. "text": "china"
  48. }
  49. # 全文检索
  50. GET book/_search
  51. {
  52. "query": {
  53. "match": {
  54. "nameText": "ES"
  55. }
  56. }
  57. }
  58. # 带有 operator
  59. GET book/_search
  60. {
  61. "query": {
  62. "match": {
  63. "nameText": {
  64. "query": "what is",
  65. "operator": "and"
  66. }
  67. }
  68. }
  69. }
  70. # match_phrase
  71. GET book/_search
  72. {
  73. "query": {
  74. "match_phrase": {
  75. "nameText": {
  76. "query": "in action"
  77. , "slop": 0
  78. }
  79. }
  80. }
  81. }
  82. # match_phrase_prefix
  83. GET book/_search
  84. {
  85. "query": {
  86. "match_phrase_prefix": {
  87. "nameText": {
  88. "query": "in a",
  89. "slop": 0
  90. }
  91. }
  92. }
  93. }
  94. # 精确检索
  95. GET book/_search
  96. {
  97. "query": {
  98. "term": {
  99. "country": "china"
  100. }
  101. }
  102. }
  103. # 精确检索
  104. GET book/_search
  105. {
  106. "query": {
  107. "term": {
  108. "nameKeyWord": "ES in one two"
  109. }
  110. }
  111. }
  112. # Range Query
  113. GET book/_search
  114. {
  115. "query": {
  116. "range": {
  117. "price": {
  118. "gte": 400,
  119. "lte": 500
  120. }
  121. }
  122. }
  123. }
  124. # Prefix Query
  125. GET book/_search
  126. {
  127. "query": {
  128. "prefix": {
  129. "nameKeyWord": "ES in a"
  130. }
  131. }
  132. }
  133. # constant_score 转成 filtering
  134. GET book/_search
  135. {
  136. "query": {
  137. "constant_score": {
  138. "filter": {
  139. "term": {
  140. "country": "China"
  141. }
  142. }
  143. }
  144. }
  145. }
  146. # bool query
  147. GET book/_search
  148. {
  149. "query": {
  150. "bool": {
  151. "must": {
  152. "match": {
  153. "nameText": "ES"
  154. }
  155. },
  156. "must_not": {
  157. "match": {
  158. "nameText": "two"
  159. }
  160. },
  161. "filter": {
  162. "range": {
  163. "price": {
  164. "gte": 300
  165. }
  166. }
  167. },
  168. "should": [
  169. {
  170. "term": {
  171. "country": "China"
  172. }
  173. },
  174. {
  175. "term": {
  176. "country": "America"
  177. }
  178. }
  179. ],
  180. "minimum_should_match": 1
  181. }
  182. }
  183. }
  184. # Bucket 聚合
  185. GET book/_search
  186. {
  187. "size": 0,
  188. "aggs": {
  189. "country_aggs": {
  190. "terms": {
  191. "field": "country"
  192. }
  193. }
  194. }
  195. }
  196. # Metric 聚合
  197. GET book/_search
  198. {
  199. "size": 0,
  200. "aggs": {
  201. "average_price": {
  202. "avg": {
  203. "field": "price"
  204. }
  205. },
  206. "max_price": {
  207. "max": {
  208. "field": "price"
  209. }
  210. },
  211. "min_price": {
  212. "min": {
  213. "field": "price"
  214. }
  215. }
  216. }
  217. }