query string search

  1. GET /ecommerce/product/_search
  2. // 返回结果
  3. {
  4. "took": 31, // 耗费了31ms
  5. "timed_out": false, // 是否超时
  6. "_shards": {
  7. "total": 5,
  8. "successful": 5,
  9. "failed": 0
  10. },
  11. "hits": {
  12. "total": 2, // 查询结果数量
  13. "max_score": 1, // score就是document对于一个search的相关度的匹配分数,匹配度越高,分数越高
  14. "hits": [ // 匹配了搜索的详细分数
  15. {
  16. "_index": "ecommerce",
  17. "_type": "product",
  18. "_id": "2",
  19. "_score": 1,
  20. "_source": {
  21. "name": "jiajieshi yagao",
  22. "desc": "youxiao fangzhu",
  23. "price": 25,
  24. "producer": "jiajieshi producer",
  25. "tags": [
  26. "fangzhu"
  27. ]
  28. }
  29. },
  30. {
  31. "_index": "ecommerce",
  32. "_type": "product",
  33. "_id": "3",
  34. "_score": 1,
  35. "_source": {
  36. "name": "zhonghua yagao",
  37. "desc": "qingxinkouqi",
  38. "price": 40,
  39. "producer": "zhonghua producer",
  40. "tags": [
  41. "qingxin"
  42. ]
  43. }
  44. }
  45. ]
  46. }
  47. }
  1. GET /ecommerce/product/_search?q=name:yagao&sort=price:desc
  2. // 返回结果
  3. {
  4. "took": 2,
  5. "timed_out": false,
  6. "_shards": {
  7. "total": 5,
  8. "successful": 5,
  9. "failed": 0
  10. },
  11. "hits": {
  12. "total": 2,
  13. "max_score": null,
  14. "hits": [
  15. {
  16. "_index": "ecommerce",
  17. "_type": "product",
  18. "_id": "3",
  19. "_score": null,
  20. "_source": {
  21. "name": "zhonghua yagao",
  22. "desc": "qingxinkouqi",
  23. "price": 40,
  24. "producer": "zhonghua producer",
  25. "tags": [
  26. "qingxin"
  27. ]
  28. },
  29. "sort": [
  30. 40
  31. ]
  32. },
  33. {
  34. "_index": "ecommerce",
  35. "_type": "product",
  36. "_id": "2",
  37. "_score": null,
  38. "_source": {
  39. "name": "jiajieshi yagao",
  40. "desc": "youxiao fangzhu",
  41. "price": 25,
  42. "producer": "jiajieshi producer",
  43. "tags": [
  44. "fangzhu"
  45. ]
  46. },
  47. "sort": [
  48. 25
  49. ]
  50. }
  51. ]
  52. }
  53. }

生产中一般不用,构建不好维护

query DSL

DSL:domain specified language

查询所有商品

  1. // 查询所有商品
  2. GET /ecommerce/product/_search
  3. {
  4. "query": {
  5. "match_all": {}
  6. }
  7. }

根据名称查询并且按照价格排序

  1. GET /ecommerce/product/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "yagao"
  6. }
  7. },
  8. "sort": [
  9. {
  10. "price": {
  11. "order": "asc"
  12. }
  13. }
  14. ]
  15. }

分页查询

  1. GET /ecommerce/product/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "from": 0, // 从哪一条开始查
  7. "size": 1 // 查询多少条
  8. }

只查询部分字段

  1. GET /ecommerce/product/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "_source": ["name"]
  7. }

复杂查询条件 query filter

  1. GET /ecommerce/product/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match": {
  7. "name": "yagao"
  8. }
  9. },
  10. "filter": {
  11. "range": {
  12. "price": {
  13. "gt": 30
  14. }
  15. }
  16. }
  17. }
  18. }
  19. }

全文检索

分数反应了分数

  1. GET /ecommerce/product/_search
  2. {
  3. "query": {
  4. "match": {
  5. "producer": "gaolujie producer"
  6. }
  7. }
  8. }
  9. // 返回结果
  10. {
  11. "took": 3,
  12. "timed_out": false,
  13. "_shards": {
  14. "total": 5,
  15. "successful": 5,
  16. "failed": 0
  17. },
  18. "hits": {
  19. "total": 4,
  20. "max_score": 0.51623213,
  21. "hits": [
  22. {
  23. "_index": "ecommerce",
  24. "_type": "product",
  25. "_id": "1",
  26. "_score": 0.51623213,
  27. "_source": {
  28. "name": "gaolujie yagao",
  29. "desc": "gaoxiao meibai",
  30. "price": 30,
  31. "producer": "gaolujie producer",
  32. "tags": [
  33. "meibai",
  34. "fangzhu"
  35. ]
  36. }
  37. },
  38. {
  39. "_index": "ecommerce",
  40. "_type": "product",
  41. "_id": "3",
  42. "_score": 0.25811607,
  43. "_source": {
  44. "name": "heiren yagao",
  45. "desc": "hei",
  46. "price": 38,
  47. "producer": "heiren producer",
  48. "tags": [
  49. "hei"
  50. ]
  51. }
  52. },
  53. {
  54. "_index": "ecommerce",
  55. "_type": "product",
  56. "_id": "2",
  57. "_score": 0.16358379,
  58. "_source": {
  59. "name": "jiajieshi yagao",
  60. "desc": "youxiao fangzhu",
  61. "price": 25,
  62. "producer": "jiajieshi producer",
  63. "tags": [
  64. "fangzhu"
  65. ]
  66. }
  67. },
  68. {
  69. "_index": "ecommerce",
  70. "_type": "product",
  71. "_id": "4",
  72. "_score": 0.16358379,
  73. "_source": {
  74. "name": "heiren yagao",
  75. "desc": "hei",
  76. "price": 38,
  77. "producer": "heiren producer",
  78. "tags": [
  79. "hei"
  80. ]
  81. }
  82. }
  83. ]
  84. }
  85. }

短语搜索

不会对查询条件进行分词

  1. GET /ecommerce/product/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "producer": "gaolujie"
  6. }
  7. }
  8. }
  9. // 返回结果
  10. {
  11. "took": 1,
  12. "timed_out": false,
  13. "_shards": {
  14. "total": 5,
  15. "successful": 5,
  16. "failed": 0
  17. },
  18. "hits": {
  19. "total": 1,
  20. "max_score": 0.25811607,
  21. "hits": [
  22. {
  23. "_index": "ecommerce",
  24. "_type": "product",
  25. "_id": "1",
  26. "_score": 0.25811607,
  27. "_source": {
  28. "name": "gaolujie yagao",
  29. "desc": "gaoxiao meibai",
  30. "price": 30,
  31. "producer": "gaolujie producer",
  32. "tags": [
  33. "meibai",
  34. "fangzhu"
  35. ]
  36. }
  37. }
  38. ]
  39. }
  40. }

高亮搜索结果

  1. GET /ecommerce/product/_search
  2. {
  3. "query": {
  4. "match": {
  5. "producer": "producer"
  6. }
  7. },
  8. "highlight": {
  9. "fields": {
  10. "producer": {}
  11. }
  12. }
  13. }
  14. // 返回结果
  15. {
  16. "took": 7,
  17. "timed_out": false,
  18. "_shards": {
  19. "total": 5,
  20. "successful": 5,
  21. "failed": 0
  22. },
  23. "hits": {
  24. "total": 4,
  25. "max_score": 0.25811607,
  26. "hits": [
  27. {
  28. "_index": "ecommerce",
  29. "_type": "product",
  30. "_id": "1",
  31. "_score": 0.25811607,
  32. "_source": {
  33. "name": "gaolujie yagao",
  34. "desc": "gaoxiao meibai",
  35. "price": 30,
  36. "producer": "gaolujie producer",
  37. "tags": [
  38. "meibai",
  39. "fangzhu"
  40. ]
  41. },
  42. "highlight": {
  43. "producer": [
  44. "gaolujie <em>producer</em>"
  45. ]
  46. }
  47. },
  48. {
  49. "_index": "ecommerce",
  50. "_type": "product",
  51. "_id": "3",
  52. "_score": 0.25811607,
  53. "_source": {
  54. "name": "heiren yagao",
  55. "desc": "hei",
  56. "price": 38,
  57. "producer": "heiren producer",
  58. "tags": [
  59. "hei"
  60. ]
  61. },
  62. "highlight": {
  63. "producer": [
  64. "heiren <em>producer</em>"
  65. ]
  66. }
  67. },
  68. {
  69. "_index": "ecommerce",
  70. "_type": "product",
  71. "_id": "2",
  72. "_score": 0.16358379,
  73. "_source": {
  74. "name": "jiajieshi yagao",
  75. "desc": "youxiao fangzhu",
  76. "price": 25,
  77. "producer": "jiajieshi producer",
  78. "tags": [
  79. "fangzhu"
  80. ]
  81. },
  82. "highlight": {
  83. "producer": [
  84. "jiajieshi <em>producer</em>"
  85. ]
  86. }
  87. },
  88. {
  89. "_index": "ecommerce",
  90. "_type": "product",
  91. "_id": "4",
  92. "_score": 0.16358379,
  93. "_source": {
  94. "name": "heiren yagao",
  95. "desc": "hei",
  96. "price": 38,
  97. "producer": "heiren producer",
  98. "tags": [
  99. "hei"
  100. ]
  101. },
  102. "highlight": {
  103. "producer": [
  104. "heiren <em>producer</em>"
  105. ]
  106. }
  107. }
  108. ]
  109. }
  110. }