ElasticSearch(四):Filter查询、聚合查询

梦里梦外; 2023-05-21 15:18 99阅读 0赞

1. 简述

Filter查询时不计算相关性的,同时可以进行缓存,因此,Filter查询的速度要快于query查询;

2. 数据准备

  1. POST /lib9/items/_bulk
  2. {"index": {"id": 1}}
  3. {"price": 40,"itemID": "ID100123"}
  4. {"index": {"_id": 2}}
  5. {"price": 50,"itemID": "ID100124"}
  6. {"index": {"_id": 3}}
  7. {"price": 25,"itemID": "ID100124"}
  8. {"index": {"_id": 4}}
  9. {"price": 30,"itemID": "ID100125"}
  10. {"index": {"_id": 5}}
  11. {"price": null,"itemID": "ID100127"}

3. filter查询

3.1 简单过滤查询

  1. # 查询price=40的文档
  2. GET /lib9/items/_search
  3. {
  4. "post_filter": {
  5. "term": {
  6. "price": 40
  7. }
  8. }
  9. }
  10. # 查询price等于25或40的文档
  11. GET /lib9/items/_search
  12. {
  13. "post_filter": {
  14. "terms": {
  15. "price": [
  16. 25,
  17. 40
  18. ]
  19. }
  20. }
  21. }
  22. # 查询itemID等于ID100123的文档(注意在这里要写成小写id)
  23. GET /lib9/items/_search
  24. {
  25. "post_filter": {
  26. "term": {
  27. "itemID": "id100123"
  28. }
  29. }
  30. }

3.2 bool过滤查询

可以实现组合过滤查询

  1. # 格式
  2. {
  3. "bool": {
  4. "must": [],
  5. "should": [],
  6. "must_not": []
  7. }
  8. }

注意
**1. must:必须要满足的条件 —and

  1. should: 可以满足也可以不满足的条件 —or
  2. must_not:不需要满足的条件 —not**

    GET /lib9/items/_search
    {
    “post_filter”: {

    1. "bool": {
    2. "should": [
    3. {
    4. "term": {
    5. "price": 25
    6. }
    7. },
    8. {
    9. "term": {
    10. "itemID": "id100123"
    11. }
    12. }
    13. ],
    14. "must_not": [
    15. {
    16. "term": {
    17. "price": 30
    18. }
    19. }
    20. ]
    21. }

    }
    }

    GET /lib9/items/_search
    {
    “post_filter”: {

    1. "bool": {
    2. "should": [
    3. {
    4. "term": {
    5. "itemID": "id100123"
    6. }
    7. },
    8. {
    9. "bool": {
    10. "must": [
    11. {
    12. "term": {
    13. "itemID": "id100124"
    14. }
    15. },
    16. {
    17. "term": {
    18. "price": 40
    19. }
    20. }
    21. ]
    22. }
    23. }
    24. ]
    25. }

    }
    }

3.3 range范围过滤

gt:>
lt:<
gte:>=
lte:<=

  1. GET /lib9/items/_search
  2. {
  3. "post_filter": {
  4. "range": {
  5. "price": {
  6. "gt": 35,
  7. "lt": 50
  8. }
  9. }
  10. }
  11. }

3.4 过滤非空

  1. GET /lib9/items/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "filter": {
  6. "exists": {
  7. "field": "price"
  8. }
  9. }
  10. }
  11. }
  12. }
  13. GET /lib9/items/_search
  14. {
  15. "query": {
  16. "constant_score": {
  17. "filter": {
  18. "exists": {
  19. "field": "price"
  20. }
  21. }
  22. }
  23. }
  24. }

3.5 过滤器缓存

ElasticSearch有一种特殊的缓存,即过滤器缓存(filter cache),用来存储过滤器的结果,被缓存的过滤器并不需要消耗过多的内存,而且可供后续所有与之相关的查询重复使用,从而极大地提高了查询性能。
注意:ElasticSearch并不是默认缓存所有过滤器,

  1. 以下过滤器默认不缓存:

    numeric_range
    script
    geo_bbox
    geo_distance
    geo_distance_range
    geo_polygon
    geo_shape
    and
    or
    not

  2. 以下过滤器默认开启缓存

    exists
    missing
    range
    term
    terms

  3. 开启方式:在filter查询语句后边加上 “_catch”:true

4. 聚合查询

  1. sum

    GET /lib9/items/_search
    {
    “size”:0,
    “aggs”: {

    1. "price_of_sum": {
    2. "sum": {
    3. "field": "price"
    4. }
    5. }

    }
    }

运行结果

  1. {
  2. "took": 6,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 5,
  12. "max_score": 0,
  13. "hits": []
  14. },
  15. "aggregations": {
  16. "price_of_sum": {
  17. "value": 160
  18. }
  19. }
  20. }
  1. min

    GET /lib9/items/_search
    {
    “size”: 0,
    “aggs”: {

    1. "price_of_min": {
    2. "min": {
    3. "field": "price"
    4. }
    5. }

    }
    }

运行结果

  1. {
  2. "took": 4,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 5,
  12. "max_score": 0,
  13. "hits": []
  14. },
  15. "aggregations": {
  16. "price_of_min": {
  17. "value": 30
  18. }
  19. }
  20. }
  1. max、avg同上
  2. terms:分组

    GET /lib9/items/_search
    {
    “size”: 0,
    “aggs”: {

    1. "price_group_by": {
    2. "terms": {
    3. "field": "price"
    4. }
    5. }

    }
    }

发表评论

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

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

相关阅读