布隆过滤器

一个 bool 过滤器由三部分组成:
{
“bool” : {
“must” : [],
“should” : [],
“mustnot” : [],
}
}
must所有的语句都
必须(must) 匹配,与 AND 等价。must_not所有的语句都 不能(must not)_ 匹配,与 NOT 等价。should至少有一个语句要匹配,与 OR 等价。

例子一

SELECT product
FROM products
WHERE (price = 20 OR productID = “XHDK-A-1293-#fJ3”)
AND (price != 30)

  1. GET /my_store/products/_search
  2. {
  3. "query" : {
  4. "filtered" : { //将所有查询包装起来
  5. "filter" : {
  6. "bool" : {
  7. "should" : [
  8. { "term" : {"price" : 20}},
  9. { "term" : {"productID" : "XHDK-A-1293-#fJ3"}}
  10. ],
  11. "must_not" : {
  12. "term" : {"price" : 30}
  13. }
  14. }
  15. }
  16. }
  17. }
  18. }

例子二

SELECT document
FROM products
WHERE productID = “KDKE-B-9947-#kL5”
OR ( productID = “JODL-X-1937-#pV7”
AND price = 30 )

  1. GET /my_store/products/_search
  2. {
  3. "query" : {
  4. "filtered" : {
  5. "filter" : {
  6. "bool" : {
  7. "should" : [
  8. { "term" : {"productID" : "KDKE-B-9947-#kL5"}},
  9. { "bool" : {
  10. "must" : [
  11. { "term" : {"productID" : "JODL-X-1937-#pV7"}},
  12. { "term" : {"price" : 30}}
  13. ]
  14. }}
  15. ]
  16. }
  17. }
  18. }
  19. }
  20. }

例子三-范围查找

  • gt: > 大于(greater than)
  • lt: < 小于(less than)
  • gte: >= 大于或等于(greater than or equal to)
  • lte: <= 小于或等于(less than or equal to)

SELECT document
FROM products
WHERE price BETWEEN 20 AND 40

  1. GET /my_store/products/_search
  2. {
  3. "query" : {
  4. "constant_score" : {
  5. "filter" : {
  6. "range" : {
  7. "price" : {
  8. "gte" : 20,
  9. "lt" : 40
  10. }
  11. }
  12. }
  13. }
  14. }
  15. }

日期

“range” : {
“timestamp” : {
“gt” : “2014-01-01 00:00:00”,
“lt” : “2014-01-07 00:00:00”
}
}

查找时间戳在过去一小时内的所有文档:
“range” : {
“timestamp” : {
“gt” : “now-1h”
}
}

加一个月查询
“range” : {
“timestamp” : {
“gt” : “2014-01-01 00:00:00”,
“lt” : “2014-01-01 00:00:00||+1M”
}
}