1. 插入数据

put请求, 插入的数据为json格式

image.png

返回格式

image.png

2.查询数据

1. 查询一条

image.png

2. 查询全部

image.png
你可以看到我们依然使用megacorp索引和employee类型,但是我们在结尾使用关键字_search来取代原来的文档ID。响应内容的hits数组中包含了我们所有的三个文档。默认情况下搜索会返回前10个结果
返回格式

  1. {
  2. "took": 36,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5, //总条数
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 4,
  12. "max_score": 1,
  13. "hits": [
  14. {
  15. "_index": "megacorp",
  16. "_type": "employee",
  17. "_id": "2",
  18. "_score": 1,
  19. "_source": {
  20. "first_name": "周",
  21. "last_name": "鹏",
  22. "age": 23,
  23. "about": "I think look this is so good",
  24. "interests": [
  25. "sports",
  26. "music",
  27. "look book"
  28. ]
  29. }
  30. },
  31. {
  32. "_index": "megacorp",
  33. "_type": "employee",
  34. "_id": "4",
  35. "_score": 1,
  36. "_source": {
  37. "first_name": "苏",
  38. "last_name": "哈哈",
  39. "age": 24,
  40. "about": "I think look this is so good",
  41. "interests": [
  42. "sports",
  43. "music"
  44. ]
  45. }
  46. },
  47. {
  48. "_index": "megacorp",
  49. "_type": "employee",
  50. "_id": "1",
  51. "_score": 1,
  52. "_source": {
  53. "first_name": "周",
  54. "last_name": "哈哈",
  55. "age": 23,
  56. "about": "I think think",
  57. "interests": [
  58. "sports",
  59. "music"
  60. ]
  61. }
  62. },
  63. {
  64. "_index": "megacorp",
  65. "_type": "employee",
  66. "_id": "3",
  67. "_score": 1,
  68. "_source": {
  69. "first_name": "苏",
  70. "last_name": "朝",
  71. "age": 24,
  72. "about": "I think look this is so good",
  73. "interests": [
  74. "sports",
  75. "music"
  76. ]
  77. }
  78. }
  79. ]
  80. }
  81. }

3. 模糊查询

查询 last_name是 哈哈 的

image.png
只需要在search后面,把查询的参数传递给 q=

返回结果:
image.png

4. 使用DSL语句查询

1. 使用match 查询 last_name是 哈哈 的

请求体
image.png
我们不在使用查询字符串(query String)作为参数, 而是使用请求体代替,这个请求体使用json表示,其中使用了 match语句

2. 更复杂的搜索

依旧搜索last_name为哈哈的员工,但是只要年龄大于23岁的,语句将添加 过滤器(filter)**,**他使得我们高效率的执行一个结构化搜索。

查找es进程

ps -ef | grep elastic

关闭es进程

kill -9 2382(进程号)