elasticsearch highlight高亮查询

刺骨的言语ヽ痛彻心扉 2022-11-19 01:18 292阅读 0赞

高亮查询highlight

预制数据

  1. PUT zhifou/doc/4
  2. {
  3. "name":"石头",
  4. "age":29,
  5. "from":"gu",
  6. "desc":"粗中有细,狐假虎威",
  7. "tags":["粗", "大","猛"]
  8. }

默认的高亮

  1. "highlight" : {
  2. "name" : [
  3. "<em>石</em><em>头</em>"
  4. ]
  5. }
  6. GET zhifou/doc/_search
  7. {
  8. "query": {
  9. "match": {
  10. "name": "石头"
  11. }
  12. },
  13. "highlight": {
  14. "fields": {
  15. "name": { }
  16. }
  17. }
  18. }
  19. ------> 返回
  20. {
  21. "took" : 3,
  22. "timed_out" : false,
  23. "_shards" : {
  24. "total" : 1,
  25. "successful" : 1,
  26. "skipped" : 0,
  27. "failed" : 0
  28. },
  29. "hits" : {
  30. "total" : {
  31. "value" : 1,
  32. "relation" : "eq"
  33. },
  34. "max_score" : 0.5753642,
  35. "hits" : [
  36. {
  37. "_index" : "zhifou",
  38. "_type" : "doc",
  39. "_id" : "4",
  40. "_score" : 0.5753642,
  41. "_source" : {
  42. "name" : "石头",
  43. "age" : 29,
  44. "from" : "gu",
  45. "desc" : "粗中有细,狐假虎威",
  46. "tags" : [
  47. "粗",
  48. "大",
  49. "猛"
  50. ]
  51. },
  52. "highlight" : {
  53. "name" : [
  54. "<em>石</em><em>头</em>"
  55. ]
  56. }
  57. }
  58. ]
  59. }
  60. }

自定义高亮

  1. "highlight": {
  2. "pre_tags": "<b class='key' style='color:red'>", // 标签前半段
  3. "post_tags": "</b>", //标签后半段
  4. "fields": {
  5. "from": { } // 高亮数据字段
  6. }
  7. GET zhifou/doc/_search
  8. {
  9. "query": {
  10. "match": {
  11. "from": "gu"
  12. }
  13. },
  14. "highlight": {
  15. "pre_tags": "<b class='key' style='color:red'>",
  16. "post_tags": "</b>",
  17. "fields": {
  18. "from": { }
  19. }
  20. }
  21. }
  22. ------> 返回
  23. {
  24. "took" : 2,
  25. "timed_out" : false,
  26. "_shards" : {
  27. "total" : 1,
  28. "successful" : 1,
  29. "skipped" : 0,
  30. "failed" : 0
  31. },
  32. "hits" : {
  33. "total" : {
  34. "value" : 1,
  35. "relation" : "eq"
  36. },
  37. "max_score" : 0.2876821,
  38. "hits" : [
  39. {
  40. "_index" : "zhifou",
  41. "_type" : "doc",
  42. "_id" : "4",
  43. "_score" : 0.2876821,
  44. "_source" : {
  45. "name" : "石头",
  46. "age" : 29,
  47. "from" : "gu",
  48. "desc" : "粗中有细,狐假虎威",
  49. "tags" : [
  50. "粗",
  51. "大",
  52. "猛"
  53. ]
  54. },
  55. "highlight" : {
  56. "from" : [
  57. "<b class='key' style='color:red'>gu</b>"
  58. ]
  59. }
  60. }
  61. ]
  62. }
  63. }

发表评论

表情:
评论列表 (有 0 条评论,292人围观)

还没有评论,来说两句吧...

相关阅读