ElasticSearch入门(三)

为了方便接下来的例子演示,我们新创建一个索引名为compony,type为person的文档,里面添加四个人,还是使用postman进行请求

  1. {"name": "zhangsan",
  2. "age":24,
  3. "address":"beijing",
  4. "word":"i like computer music",
  5. "interests":["football","dance"]
  6. }
  7. {"name": "lisi",
  8. "age":25,
  9. "address":"shanghai",
  10. "word":"i like computer game",
  11. "interests":["sing","dance"]
  12. }
  13. {"name": "wangwu",
  14. "age":26,
  15. "address":"guangzhou",
  16. "word":"i like read book",
  17. "interests":["poker","game"]
  18. }
  19. {"name": "zhaoliu",
  20. "age":27,
  21. "address":"chengdu",
  22. "word":"i like game",
  23. "interests":["forestry","game"]
  24. }

一、全文搜索

查找所有喜欢computer game的人:

  1. localhost:9200/compony/person/_search
  1. {
  2. "query" : {
  3. "match" : {
  4. "word" : "computer game"
  5. }
  6. }
  7. }

得到的结果:

  1. {
  2. "took": 225,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 3,
  13. "relation": "eq"
  14. },
  15. "max_score": 1.3494902,
  16. "hits": [
  17. {
  18. "_index": "compony",
  19. "_type": "person",
  20. "_id": "2",
  21. "_score": 1.3494902,
  22. "_source": {
  23. "name": "lisi",
  24. "age": 25,
  25. "address": "shanghai",
  26. "word": "i like computer game",
  27. "interests": [
  28. "sing",
  29. "dance"
  30. ]
  31. }
  32. },
  33. {
  34. "_index": "compony",
  35. "_type": "person",
  36. "_id": "4",
  37. "_score": 0.7549127,
  38. "_source": {
  39. "name": "zhaoliu",
  40. "age": 27,
  41. "address": "chengdu",
  42. "word": "i like game",
  43. "interests": [
  44. "forestry",
  45. "game"
  46. ]
  47. }
  48. },
  49. {
  50. "_index": "compony",
  51. "_type": "person",
  52. "_id": "1",
  53. "_score": 0.6747451,
  54. "_source": {
  55. "name": "zhangsan",
  56. "age": 24,
  57. "address": "beijing",
  58. "word": "i like computer music",
  59. "interests": [
  60. "football",
  61. "dance"
  62. ]
  63. }
  64. }
  65. ]
  66. }
  67. }

其中的_score代表相关性得分,Elasticsearch 默认按照相关性得分排序,即每个文档跟查询的匹配程度。我们这里看到即使是zhaoliu的爱好里面没有computer,但是结果也返回了,因为它里面有game,所以也返回了,只是相关性变低了。这也看出来区别于关系型数据库的概念,数据库中的一条记录要么匹配要么不匹配。

二、短语搜索

找出一个属性中的独立单词是没有问题的,但有时候想要精确匹配一系列单词或者短语 。 比如, 我们想执行这样一个查询,仅匹配同时包含 “computer” “game” ,并且 二者以短语 “computer game” 的形式紧挨着的人员记录。

  1. localhost:9200/compony/person/_search
  2. {
  3. "query" : {
  4. "match_phrase" : {
  5. "word" : "computer game"
  6. }
  7. }
  8. }

注意这里与全文搜索变化的地方只有match变为了match_phrase,返回结果:

  1. {
  2. "took": 4,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 1.3494902,
  16. "hits": [
  17. {
  18. "_index": "compony",
  19. "_type": "person",
  20. "_id": "2",
  21. "_score": 1.3494902,
  22. "_source": {
  23. "name": "lisi",
  24. "age": 25,
  25. "address": "shanghai",
  26. "word": "i like computer game",
  27. "interests": [
  28. "sing",
  29. "dance"
  30. ]
  31. }
  32. }
  33. ]
  34. }
  35. }

这样的查询,只匹配到了lisi这个人

三、高亮搜索

许多应用都倾向于在每个搜索结果中 高亮 部分文本片段,以便让用户知道为何该文档符合查询条件。在 Elasticsearch 中检索出高亮片段也很容易。

再次执行前面的查询,并增加一个新的 highlight 参数:

  1. {
  2. "query" : {
  3. "match_phrase" : {
  4. "word" : "computer game"
  5. }
  6. },
  7. "highlight":{
  8. "fields":{
  9. "word":{}
  10. }
  11. }
  12. }

查询结果:

  1. {
  2. "took": 31,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 1.3494902,
  16. "hits": [
  17. {
  18. "_index": "compony",
  19. "_type": "person",
  20. "_id": "2",
  21. "_score": 1.3494902,
  22. "_source": {
  23. "name": "lisi",
  24. "age": 25,
  25. "address": "shanghai",
  26. "word": "i like computer game",
  27. "interests": [
  28. "sing",
  29. "dance"
  30. ]
  31. },
  32. "highlight": {
  33. "word": [
  34. "i like <em>computer</em> <em>game</em>"
  35. ]
  36. }
  37. }
  38. ]
  39. }
  40. }

这样的效果可能与百度搜索那种类似,这只是我的猜测:
1.png
官网给出的基本入门也就这么多了,其它的后续看情况要不要做几个demo,现目前计划后续整合springboot在代码层面来展示