ElasticSearch 全文搜索引擎使用回顾

官方使用入门手册

GitHub中文手册

个人使用案例分析

  1. {
  2. "index": "zbb.t_project",
  3. "type": "_doc",
  4. "size": 10,
  5. "from": 0,
  6. "body": {
  7. "query": {
  8. "bool": {
  9. "should": [
  10. {
  11. "match_phrase": {
  12. "project_name": {
  13. "query": "上海",
  14. "boost": 2
  15. }
  16. }
  17. },
  18. {
  19. "match_phrase": {
  20. "content": {
  21. "query": "上海",
  22. "boost": 1
  23. }
  24. }
  25. },
  26. {
  27. "match_phrase": {
  28. "project_name": {
  29. "query": "虹桥",
  30. "boost": 2
  31. }
  32. }
  33. },
  34. {
  35. "match_phrase": {
  36. "content": {
  37. "query": "虹桥",
  38. "boost": 1
  39. }
  40. }
  41. },
  42. {
  43. "match_phrase": {
  44. "project_name": {
  45. "query": "广东",
  46. "boost": 2
  47. }
  48. }
  49. },
  50. {
  51. "match_phrase": {
  52. "content": {
  53. "query": "广东",
  54. "boost": 1
  55. }
  56. }
  57. },
  58. {
  59. "match_phrase": {
  60. "project_name": {
  61. "query": "农业",
  62. "boost": 2
  63. }
  64. }
  65. },
  66. {
  67. "match_phrase": {
  68. "content": {
  69. "query": "农业",
  70. "boost": 1
  71. }
  72. }
  73. }
  74. ],
  75. "must_not": [
  76. {
  77. "match_phrase": {
  78. "project_name": "上海"
  79. }
  80. },
  81. {
  82. "match_phrase": {
  83. "content": "上海"
  84. }
  85. },
  86. {
  87. "match_phrase": {
  88. "project_name": "虹桥"
  89. }
  90. },
  91. {
  92. "match_phrase": {
  93. "content": "虹桥"
  94. }
  95. }
  96. ],
  97. "filter": [
  98. {
  99. "bool": {
  100. "should": [
  101. {
  102. "term": {
  103. "industry_id.keyword": "5de08de68dca10baa18a0985"
  104. }
  105. },
  106. {
  107. "term": {
  108. "industry_id.keyword": "5de08de88dca10baa18a099c"
  109. }
  110. },
  111. {
  112. "term": {
  113. "industry_id.keyword": "5de08de88dca10baa18a09a1"
  114. }
  115. },
  116. {
  117. "term": {
  118. "industry_id.keyword": "5de08de78dca10baa18a098c"
  119. }
  120. },
  121. {
  122. "term": {
  123. "industry_id.keyword": "5de08de88dca10baa18a09a6"
  124. }
  125. },
  126. {
  127. "term": {
  128. "industry_id.keyword": "5de08de78dca10baa18a0990"
  129. }
  130. }
  131. ]
  132. }
  133. },
  134. {
  135. "range": {
  136. "budget_fee": {
  137. "gte": 0
  138. }
  139. }
  140. },
  141. {
  142. "range": {
  143. "budget_fee": {
  144. "lte": 500000
  145. }
  146. }
  147. },
  148. {
  149. "bool": {
  150. "should": [
  151. {
  152. "term": {
  153. "district_id.keyword": "5ddf9096027d26b7fed6d79e"
  154. }
  155. },
  156. {
  157. "term": {
  158. "district_id.keyword": "5ddf907c027d26b7fed6d254"
  159. }
  160. },
  161. {
  162. "term": {
  163. "district_id.keyword": "5ddf906e027d26b7fed6cedd"
  164. }
  165. }
  166. ]
  167. }
  168. },
  169. {
  170. "bool": {
  171. "should": [
  172. {
  173. "term": {
  174. "type_id.keyword": "5de091e67156d964904dab83"
  175. }
  176. },
  177. {
  178. "term": {
  179. "type_id.keyword": "5de091fc7156d964904dab93"
  180. }
  181. },
  182. {
  183. "term": {
  184. "type_id.keyword": "5de092137156d964904dab9e"
  185. }
  186. }
  187. ]
  188. }
  189. },
  190. {
  191. "range": {
  192. "publish_time": {
  193. "gte": 1573089951
  194. }
  195. }
  196. }
  197. ]
  198. }
  199. }
  200. }
  201. }

上面代码使用解析

  • index : 需要被搜索的索引
  • type : _doc elasticsearch 7.0 以后版本固定为 _doc类型
  • size : 获取的数量,分页数据的偏移量
  • from : 开始从第几个数据获取数量
  • body : 搜索内容体
    • query : 搜索标识
      • bool : 多条件查询,可支持数组查询
        • should: 或查询 及查询的条件一个满足即满足
          • match_phrase : 不分词短语查询,如果使用match 则会进行分词
            • filed_name: 字段名称
              • query: 指定查询内容
              • boost: 增加权重值,也就是增加该字段的搜索评分,评分越高越靠前
        • must_not: 必须不存在的查询
        • filter: 不参与评分的查询
          • bool: 再前台bool 查询
          • range: 范围查询 lte 小于 gte 大于


评论 0

发表评论

Top