elasticsearh搜索过滤filter

首先要讲下,为什么需要使用filter过滤

  • 过滤并不会返回一个匹配度score,以至于它比查询要快很多
  • 过滤查询后的结果能被缓存到内存中,并被多次重复使用.

1.如果我们要查询出account中blance从20000到30000之间的数据

 curl -XPOST localhost:9200/bank/_search?pretty -d ‘{
    "query":{
        "filtered":{
            "query":{
                "match_all":{}},
                "filter":{
                    "range":{
                        "balance":{
                            "gte":20000,
                            "lte":30000
                          }
                       }
                  }
             }
         }
}‘
{
  "took" : 102,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 217,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "49",
      "_score" : 1.0,
      "_source":{"account_number":49,"balance":29104,"firstname":"Fulton","lastname":"Holt","age":23,"gender":"F","address":"451 Humboldt Street","employer":"Anocha","email":"[email protected]","city":"Sunriver","state":"RI"}
    }, {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "102",
      "_score" : 1.0,
      "_source":{"account_number":102,"balance":29712,"firstname":"Dena","lastname":"Olson","age":27,"gender":"F","address":"759 Newkirk Avenue","employer":"Hinway","email":"[email protected]","city":"Choctaw","state":"NJ"}
    }, {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "133",
      "_score" : 1.0,
      "_source":{"account_number":133,"balance":26135,"firstname":"Deena","lastname":"Richmond","age":36,"gender":"F","address":"646 Underhill Avenue","employer":"Sunclipse","email":"[email protected]","city":"Austinburg","state":"SC"}
    }, {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "140",
      "_score" : 1.0,
      "_source":{"account_number":140,"balance":26696,"firstname":"Cotton","lastname":"Christensen","age":32,"gender":"M","address":"878 Schermerhorn Street","employer":"Prowaste","email":"[email protected]","city":"Mayfair","state":"LA"}
    }, {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "203",
      "_score" : 1.0,
      "_source":{"account_number":203,"balance":21890,"firstname":"Eve","lastname":"Wyatt","age":33,"gender":"M","address":"435 Furman Street","employer":"Assitia","email":"[email protected]","city":"Jamestown","state":"MN"}
    }, {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "239",
      "_score" : 1.0,
      "_source":{"account_number":239,"balance":25719,"firstname":"Chang","lastname":"Boyer","age":36,"gender":"M","address":"895 Brigham Street","employer":"Qaboos","email":"[email protected]","city":"Belgreen","state":"NH"}
    }, {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "241",
      "_score" : 1.0,
      "_source":{"account_number":241,"balance":25379,"firstname":"Schroeder","lastname":"Harrington","age":26,"gender":"M","address":"610 Tapscott Avenue","employer":"Otherway","email":"[email protected]","city":"Ebro","state":"TX"}
    }, {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "246",
      "_score" : 1.0,
      "_source":{"account_number":246,"balance":28405,"firstname":"Katheryn","lastname":"Foster","age":21,"gender":"F","address":"259 Kane Street","employer":"Quantalia","email":"[email protected]","city":"Bath","state":"TX"}
    }, {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "253",
      "_score" : 1.0,
      "_source":{"account_number":253,"balance":20240,"firstname":"Melissa","lastname":"Gould","age":31,"gender":"M","address":"440 Fuller Place","employer":"Buzzopia","email":"[email protected]","city":"Lumberton","state":"MD"}
    }, {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "277",
      "_score" : 1.0,
      "_source":{"account_number":277,"balance":29564,"firstname":"Romero","lastname":"Lott","age":31,"gender":"M","address":"456 Danforth Street","employer":"Plasto","email":"[email protected]","city":"Vincent","state":"VT"}
    } ]
  }
}

可以看到查询的结果都是在指定范围内

2.当然elasticsearch能够像sql一样使用聚合函数

curl -XPOST ‘localhost:9200/bank/_search?pretty‘ -d ‘
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state"
      }
    }
  }
}‘

这个例子其实就是根据state字段进行分组.相当于下列sql语句

SELECT COUNT(*) from bank GROUP BY state ORDER BY COUNT(*) DESC

结果为:

{
  "took" : 190,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1000,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_state" : {
      "buckets" : [ {
        "key" : "al",
        "doc_count" : 21
      }, {
        "key" : "tx",
        "doc_count" : 17
      }, {
        "key" : "id",
        "doc_count" : 15
      }, {
        "key" : "ma",
        "doc_count" : 15
      }, {
        "key" : "md",
        "doc_count" : 15
      }, {
        "key" : "pa",
        "doc_count" : 15
      }, {
        "key" : "dc",
        "doc_count" : 14
      }, {
        "key" : "me",
        "doc_count" : 14
      }, {
        "key" : "mo",
        "doc_count" : 14
      }, {
        "key" : "nd",
        "doc_count" : 14
      } ]
    }
  }
}

其中key就是分组中的state值,doc_count就是个数.group_by_state只是分组的一个别名

我们再使用gender来进行分组

curl -XPOST ‘localhost:9200/bank/_search?pretty‘ -d ‘
{
  "size": 0,
  "aggs": {
    "group_by_gender": {
      "terms": {
        "field": "gender"
      }
    }
  }
}‘
{
  "took" : 30,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1000,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_gender" : {
      "buckets" : [ {
        "key" : "m",
        "doc_count" : 507
      }, {
        "key" : "f",
        "doc_count" : 493
      } ]
    }
  }
}

可以发现M的有507个,f的有493个,可以看到这两次查询都设置了size为0,因为我们不想显示匹配的一条条数据,只想看聚合的结果.如果去掉size=0,那么hits节点下hits中会存在数据.

如果我需要不仅要查询state的分组信息,还要查询出各个分组中的blance的平均数

curl -XPOST ‘localhost:9200/bank/_search?pretty‘ -d ‘
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state"
        },
        "aggs": {
        "average_balance": {
          "avg": {
            "field": "balance"
              }
          }
      }
    }
  }
}‘
{
  "took" : 34,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1000,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_state" : {
      "buckets" : [ {
        "key" : "al",
        "doc_count" : 21,
        "average_balance" : {
          "value" : 25377.571428571428
        }
      }, {
        "key" : "tx",
        "doc_count" : 17,
        "average_balance" : {
          "value" : 22466.058823529413
        }
      }, {
        "key" : "id",
        "doc_count" : 15,
        "average_balance" : {
          "value" : 23614.933333333334
        }
      }, {
        "key" : "ma",
        "doc_count" : 15,
        "average_balance" : {
          "value" : 29064.666666666668
        }
      }, {
        "key" : "md",
        "doc_count" : 15,
        "average_balance" : {
          "value" : 20143.733333333334
        }
      }, {
        "key" : "pa",
        "doc_count" : 15,
        "average_balance" : {
          "value" : 25320.933333333334
        }
      }, {
        "key" : "dc",
        "doc_count" : 14,
        "average_balance" : {
          "value" : 24543.64285714286
        }
      }, {
        "key" : "me",
        "doc_count" : 14,
        "average_balance" : {
          "value" : 20061.14285714286
        }
      }, {
        "key" : "mo",
        "doc_count" : 14,
        "average_balance" : {
          "value" : 25414.64285714286
        }
      }, {
        "key" : "nd",
        "doc_count" : 14,
        "average_balance" : {
          "value" : 31717.571428571428
        }
      } ]
    }
  }
}

注意average_balance是别名

如果我需要根据查出的balance平均数进行一个排序呢?

curl -XPOST ‘localhost:9200/bank/_search?pretty‘ -d ‘
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state",
        "order": {
          "average_balance": "desc"
        }
      },
      "aggs": {
        "average_balance": {
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  }
}‘

3.我需要将20-29,30-39,40-49这三个年龄段的账户信息进行分组

curl -XPOST ‘localhost:9200/bank/_search?pretty‘ -d ‘
{
  "size": 0,
  "aggs": {
    "group_by_age": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 20,
            "to": 30
          },
          {
            "from": 30,
            "to": 40
          },
          {
            "from": 40,
            "to": 50
          }
        ]
      },
      "aggs": {
        "group_by_gender": {
          "terms": {
            "field": "gender"
          },
          "aggs": {
            "average_balance": {
              "avg": {
                "field": "balance"
              }
            }
          }
        }
      }
    }
  }
}‘
{
  "took" : 21,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1000,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_age" : {
      "buckets" : [ {
        "key" : "20.0-30.0",
        "from" : 20.0,
        "from_as_string" : "20.0",
        "to" : 30.0,
        "to_as_string" : "30.0",
        "doc_count" : 451,
        "group_by_gender" : {
          "buckets" : [ {
            "key" : "m",
            "doc_count" : 232,
            "average_balance" : {
              "value" : 27374.05172413793
            }
          }, {
            "key" : "f",
            "doc_count" : 219,
            "average_balance" : {
              "value" : 25341.260273972603
            }
          } ]
        }
      }, {
        "key" : "30.0-40.0",
        "from" : 30.0,
        "from_as_string" : "30.0",
        "to" : 40.0,
        "to_as_string" : "40.0",
        "doc_count" : 504,
        "group_by_gender" : {
          "buckets" : [ {
            "key" : "f",
            "doc_count" : 253,
            "average_balance" : {
              "value" : 25670.869565217392
            }
          }, {
            "key" : "m",
            "doc_count" : 251,
            "average_balance" : {
              "value" : 24288.239043824702
            }
          } ]
        }
      }, {
        "key" : "40.0-50.0",
        "from" : 40.0,
        "from_as_string" : "40.0",
        "to" : 50.0,
        "to_as_string" : "50.0",
        "doc_count" : 45,
        "group_by_gender" : {
          "buckets" : [ {
            "key" : "m",
            "doc_count" : 24,
            "average_balance" : {
              "value" : 26474.958333333332
            }
          }, {
            "key" : "f",
            "doc_count" : 21,
            "average_balance" : {
              "value" : 27992.571428571428
            }
          } ]
        }
      } ]
    }
  }
}

可以看到结果如上.

时间: 2024-08-03 10:05:31

elasticsearh搜索过滤filter的相关文章

签发token、校验token、多方式登录签发token的实现、自定义认证反爬规则的认证类、admin使用自定义User表:新增用户密码密文、群查接口各种筛选组件数据准备、drf搜索过滤组件、drf排序过滤组件、drf基础分页组件

签发token 源码入口 # 前提:给一个局部禁用了所有 认证与权限 的视图类发送用户信息得到token,其实就是登录接口 # 1)rest_framework_jwt.views.ObtainJSONWebToken 的 父类 JSONWebTokenAPIView 的 post 方法 # 接收有username.password的post请求 # 2)post方法将请求得到的数据交给 rest_framework_jwt.serializer.JSONWebTokenSerializer 处

利用css实现搜索过滤

无意中找到一种利用css就可实现的搜索过滤的方法,不得不说看了代码之后确实被惊艳到了,亏我之前面试还因为做这个功能做太慢而拖了后腿.在此记录下代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"&g

实现多选和搜索过滤功能的jQuery下拉列表框插件

Selectator是一款实现多选和搜索过滤功能的jQuery下拉列表框插件.通过该下拉列表框插件可以多选项进行分组,设置选项的图标,对选项进行搜索过滤,以及进行多选选择. 在线预览   源码下载 使用方法 使用该下拉类别框插件需要在页面中引入fm.selectator.jquery.css.jQuery和fm.selectator.jquery.js文件. <link rel="stylesheet" href="fm.selectator.jquery.css&qu

搜索过滤grep(win下为findstr)

搜索过滤grep(win下为findstr) 1.主要参数 [options]主要参数: -c:只输出匹配行的计数. -i:不区分大小写 -h:查询多文件时不显示文件名. -l:查询多文件时只输出包含匹配字符的文件名. -n:显示匹配行及行号. -s:不显示不存在或无匹配文本的错误信息. -v:显示不包含匹配文本的所有行. pattern正则表达式主要参数: \: 忽略正则表达式中特殊字符的原有含义. ^:匹配正则表达式的开始行. $: 匹配正则表达式的结束行. \<:从匹配正则表达 式的行开始

EasyUI combobox下拉列表实现搜索过滤(模糊匹配)

项目中的某个下拉列表长达200多个项,这么巨大的数量一个一个找眼镜都得看花,于是就得整了个搜索功能.看网上别人帖子有只能前缀匹配的方案,但只能前缀匹配的话用起来也不是很方便.于是就记录一下模糊匹配的方案. 实现效果: 这里使用的是combobox组合框,对于combobox的创建可以使用<input>输入框,也可以使用<select>下拉选.我使用的是<select>: HTML代码 <label>关联课程</label> <select

Linux常用指令---grep(搜索过滤)

Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户. grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板.如果模板包括空格,则必须被引用,模板后的所有字符串被看作文件名.搜索的结果被送到标准输出,不影响原文件内容. grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成

整理grep实战文本搜索过滤技巧

一:grep的简介: 文本搜索工具,根据用户指定的文本模式对目标文件进行逐行搜索,显示能够被模式所匹配到的行.配合正则表达式的使用可以实现强大的文本处理.下面一一说明正则的例子. 二:文本处理工具分类 常用的有:grep,egrep,fgrep. 区别: grep:在没有参数的情况下,只输出符合RE(Regular Expression)字符. egrep:等同于grep -E,和grep最大的区别就是表现在转义符上比如grep 做次数匹配时\{n,m\}egrep则不需要直接{n,m}.egr

kendo grid的过滤filter

kendo是一套强大方便的前端解决方案,而且新版还可以与angularjs集成,项目中就使用了一些kendo的控件,比如grid表格. grid提供了过滤功能,但中文网站缺少这方面的资料,在这里整理一下kendo grid怎么使用过滤. 下面代码包含了grid的过滤所有涉及的所有设置,以这个完整的例子,加上注释讲解其使用方法: 1 <div id="grid"></div> 2 <script> 3 $("#grid").kend

SpringCloud过滤filter

目录 配置文件 application.yml eureka: client: service-url: defaultZone: http://localhost:8001/eureka server: port: 9001 spring: application: name: zuul ##配置zuul网关 zuul: routes: api-a: path: /member-zuul/** service-id: memeber-service api-b: path: /order-zu