当前位置:   article > 正文

Elasticsearch filter过滤查询_exceptionless filter 搜索

exceptionless filter 搜索

注:本实例代码是在ES marvel插件中执行

  1. # ---------------------------------------
  2. # filter 查询
  3. # 建立测试数据_1
  4. POST /store/products/_bulk
  5. { "index": {"_id": 1}}
  6. { "price": 10, "productID": "SD1002136"}
  7. { "index": {"_id": 2}}
  8. { "price": 20, "productID": "SD2678421"}
  9. { "index": {"_id": 3}}
  10. { "price": 30, "productID": "SD8897573"}
  11. { "index": {"_id": 4}}
  12. { "price": 40, "productID": "SD4535233"}
  13. # 查看测试数据
  14. GET /store/products/_mget
  15. {
  16. "ids" : ["1", "2", "3", "4"]
  17. }
  18. # 查看library的mapping信息
  19. GET /store/_mapping
  20. # ---------------------------------------
  21. # 简单过滤查询
  22. # 最简单filter查询
  23. # SELECT document FROM products where price = 20
  24. # filtered 查询价格是20的商品
  25. GET /store/products/_search
  26. {
  27. "query": {
  28. "filtered": {
  29. "query": {
  30. "match_all": {}
  31. },
  32. "filter": {
  33. "term": {
  34. "price": 200
  35. }
  36. }
  37. }
  38. }
  39. }
  40. # 也可以指定多个值
  41. GET /store/products/_search
  42. {
  43. "query": {
  44. "filtered": {
  45. "filter": {
  46. "terms": {
  47. "price": [10, 20]
  48. }
  49. }
  50. }
  51. }
  52. }
  53. # SELECT product FROM products WHERE productID = "SD4535233"
  54. # 由于默认分析器进行分析,会将大写转小写
  55. GET /store/products/_search
  56. {
  57. "query": {
  58. "filtered": {
  59. "filter": {
  60. "term": {
  61. "productID": "sd4535233"
  62. }
  63. }
  64. }
  65. }
  66. }
  67. # 查看分析器解析的结果
  68. GET /_analyze?text=SD4535233
  69. GET /store/_mapping
  70. DELETE /store
  71. # 重新建立一个映射,让productID处于not_analyzed模式
  72. PUT /store
  73. {
  74. "mappings": {
  75. "products": {
  76. "properties": {
  77. "productID" : {
  78. "type" : "string",
  79. "index": "not_analyzed"
  80. }
  81. }
  82. }
  83. }
  84. }
  85. # 重新创建mapping插入数据后,进行查看
  86. GET /store/products/_search
  87. {
  88. "query": {
  89. "filtered": {
  90. "filter": {
  91. "term": {
  92. "productID": "SD4535233"
  93. }
  94. }
  95. }
  96. }
  97. }
  98. # -------------------------
  99. # bool过滤查询,可以做组合过滤查询
  100. # SELECT product FROM products WHERE (price = 20 OR productID = "SD1002136") AND (price != 30)
  101. # 查询价格等于20的或者productID为SD4535233的商品,排除价格30元的。
  102. # 类似的,Elasticsearch也有 andornot这样的组合条件的查询 方式
  103. # 格式如下:
  104. # {
  105. # "bool" : {
  106. # "must" : [],
  107. # "should" : [],
  108. # "must_not" : []
  109. # }
  110. # }
  111. #
  112. # must: 条件必须满足,相当于 and
  113. # should: 条件可以满足也可以不满足,相当于 or
  114. # must_not: 条件不需要满足,相当于 not
  115. GET /store/products/_search
  116. {
  117. "query": {
  118. "filtered": {
  119. "filter": {
  120. "bool": {
  121. "should": [
  122. { "term": {
  123. "price": "20"
  124. }},
  125. { "term": {
  126. "productID": "SD1002136"
  127. }}
  128. ],
  129. "must_not": [
  130. { "term": {
  131. "price": "30"
  132. }}
  133. ]
  134. }
  135. }
  136. }
  137. }
  138. }
  139. # 嵌套处查询
  140. # SELECT document FROM products WHERE productID ="SD1002136" OR (productID = "SD4535233" AND price = 30)
  141. GET /store/products/_search
  142. {
  143. "query": {
  144. "filtered": {
  145. "filter": {
  146. "bool": {
  147. "should": [
  148. { "term": {
  149. "productID": "SD1002136"
  150. }},
  151. {
  152. "bool": {
  153. "must": [
  154. { "term": {
  155. "productID": "SD4535233"
  156. }},
  157. {
  158. "term": {
  159. "price": "30"
  160. }
  161. }
  162. ]
  163. }
  164. }
  165. ]
  166. }
  167. }
  168. }
  169. }
  170. }
  171. # 另外一种 andornot查询
  172. # 没有bool,直接使用andornot
  173. #
  174. # 查询价格既是10元,productID又为SD1002136的结果
  175. GET /store/products/_search
  176. {
  177. "query": {
  178. "filtered": {
  179. "query": {
  180. "match_all": {}
  181. },
  182. "filter": {
  183. "and": [
  184. {
  185. "term" : {
  186. "price": 10
  187. }
  188. },
  189. {
  190. "term" : {
  191. "productID" : "SD1002136"
  192. }
  193. }
  194. ]
  195. }
  196. }
  197. }
  198. }
  199. # or
  200. # 查询价格是10元或productID 是SD4535233的一些商品
  201. GET /store/products/_search
  202. {
  203. "query": {
  204. "filtered": {
  205. "query": { "match_all": {}},
  206. "filter": {
  207. "or": [
  208. {
  209. "term": {
  210. "price": 10
  211. }
  212. },
  213. {
  214. "term": {
  215. "productID": "SD4535233"
  216. }
  217. }
  218. ]
  219. }
  220. }
  221. }
  222. }
  223. # not
  224. # 查询productID不是SD1002136的商品
  225. GET /store/products/_search
  226. {
  227. "query": {
  228. "filtered": {
  229. "query": { "match_all": {}},
  230. "filter": {
  231. "not":
  232. {
  233. "term": {
  234. "productID": "SD1002136"
  235. }
  236. }
  237. }
  238. }
  239. }
  240. }
  241. # range范围过滤
  242. # SELECT document FROM products WHERE price BETWEEN 20 AND 40
  243. # gt : > 大于
  244. # lt : < 小于
  245. # gte : >= 大于等于
  246. # lte : <= 小于等于
  247. GET /store/products/_search
  248. {
  249. "query": {
  250. "filtered": {
  251. "filter": {
  252. "range": {
  253. "price": {
  254. "gt": 20,
  255. "lt": 40
  256. }
  257. }
  258. }
  259. }
  260. }
  261. }
  262. # -------------------------
  263. # 过滤空和非空
  264. # 建立测试数据_2
  265. POST /test_index/test/_bulk
  266. { "index": { "_id": "1"}}
  267. { "tags" : ["search"]}
  268. { "index": { "_id": "2"}}
  269. { "tags" : ["search", "open_source"]}
  270. { "index": { "_id": "3"}}
  271. { "other_field" : ["some data"]}
  272. { "index": { "_id": "4"}}
  273. { "tags" : null}
  274. { "index": { "_id": "5"}}
  275. { "tags" : ["search", null]}
  276. # 处理null空值的方法
  277. # SELECT tags FROM test WHERE tags IS NOT NULL
  278. # SELECT tags FROM test WHERE tags IS NULL
  279. GET /test_index/test/_search
  280. {
  281. "query": {
  282. "filtered": {
  283. "filter": {
  284. "exists": {
  285. "field": "tags"
  286. }
  287. }
  288. }
  289. }
  290. }
  291. GET /test_index/test/_search
  292. {
  293. "query": {
  294. "filtered": {
  295. "filter": {
  296. "missing": {
  297. "field": "tags"
  298. }
  299. }
  300. }
  301. }
  302. }

 

cache缓存

Elasticsearch在执行带有filter查询时,会打开索引的每个segment文件(Lucene式底层文件),然后去判断里面的文档是否符合filter要求。
注意:旧的segment文件不会变,新来的数据会产生新的segment。

匹配的结果会用一个大型的BigSet数组来存储,这个数组的值只有0和1
匹配:1
不匹配:0
BigSet值是存在内存里的,而不是硬盘里,所以速度快!

开启方式:在filter查询语句后面加"_cache": true

注意:
Scriptfilters,Geo-filters,Data ranges这样的过滤方式开启cache无意义
exists,missing,range,term和terms查询是默认开启cache的

 

 

如果想要开启cache缓存,只需要在filter中添加"_cache": true属性就可以,例如:

  1. # cache缓存
  2. GET /store/products/_search
  3. {
  4. "query": {
  5. "filtered": {
  6. "filter": {
  7. "bool": {
  8. "should": [
  9. { "term": {
  10. "price": "20"
  11. }},
  12. { "term": {
  13. "productID": "SD1002136"
  14. }}
  15. ],
  16. "_cache": true,
  17. "must_not": [
  18. { "term": {
  19. "price": "30"
  20. }}
  21. ]
  22. }
  23. }
  24. }
  25. }
  26. }

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号