ES 23 - 检索和过滤的区别 (query vs. filter)
阅读原文时间:2023年07月15日阅读:2

目录

本文以 ES 6.6.0 版本为例进行演示.

1 filter与query示例

PUT website/_doc/1
{
    "title": "小白学ES01",
    "desc": "the first blog about es",
    "level": 1,
    "post_date": "2018-10-10",
    "post_address": {
        "country": "China",
        "province": "GuangDong",
        "city": "GuangZhou"
    }
}

PUT website/_doc/2
{
    "title": "小白学ES02",
    "desc": "the second blog about es",
    "level": 3,
    "post_date": "2018-11-11",
    "post_address": {
        "country": "China",
        "province": "ZheJiang",
        "city": "HangZhou"
    }
}

搜索条件: 搜索博客等级(level)大于等于2, 同时发布日期(post_date)是2018-11-11的博客:

(1) 不使用filter:

GET website/_doc/_search
{
    "query": {
        "bool": {
            "must": [
                { "match": { "post_date": "2018-11-11" } },
                { "range": { "level": { "gte": 2 } } }
            ]
        }
    }
}
// 结果信息:
"hits": {
    "total": 1,
    "max_score": 2.0,
    "hits": [
        {
            "_index": "website2",
            "_type": "blog",
            "_id": "2",
            "_score": 2.0,          // 评分为2.0
            "_source": {
                  "title": "小白学ES02",
                  "desc": "the second blog about es",
                  "level": 3,
                  "post_date": "2018-11-11",
                  "post_address": {
                    "country": "China",
                    "province": "ZheJiang",
                    "city": "HangZhou"
                  }
            }
          }
    ]
}

(2) 使用filter:

GET website/_doc/_search
{
    "query": {
        "bool": {
            "must": {
                "match": { "post_date": "2018-11-11" }
            },
            "filter": {
                "range": { "level": { "gte": 2 } }
            }
        }
    }
}
// 结果信息:
"hits": {
    "total": 1,
    "max_score": 1.0,
    "hits": [
        {
            "_index": "website2",
            "_type": "blog",
            "_id": "2",
            "_score": 1.0,      // 评分为1.0
            "_source": {
                  "title": "小白学ES02",
                  "desc": "the second blog about es",
                  "level": 3,
                  "post_date": "2018-11-11",
                  "post_address": {
                    "country": "China",
                    "province": "ZheJiang",
                    "city": "HangZhou"
                  }
            }
          }
    ]
}

2 filter与query的区别

filter和query一起使用时, 会先执行filter.

filter —— 只根据搜索条件过滤出符合的文档, 将这些文档的评分固定为1, 忽略TF/IDF信息, 不计算相关度分数;

query —— 先查询符合搜索条件的文档, 然后计算每个文档对于搜索条件的相关度分数, 再根据评分倒序排序.

建议:

  • 如果对搜索结果有排序的要求, 要将最匹配的文档排在最前面, 就用query;
  • 如果只是根据一定的条件筛选出部分数据, 不关注结果的排序, 就用filter.

filter 性能更好, 无排序 —— 不计算相关度分数, 不用根据相关度分数进行排序, 同时ES内部还会缓存(cache)比较常用的filter的数据 (使用bitset <0或1> 来记录包含与否).

query 性能较差, 有排序 —— 要计算相关度分数, 要根据相关度分数进行排序, 并且没有cache功能.

版权声明

作者: 马瘦风(https://healchow.com)

出处: 博客园 马瘦风的博客(https://www.cnblogs.com/shoufeng)

感谢阅读, 如果文章有帮助或启发到你, 点个[好文要顶