栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Elasticearch Filter Query 过滤查询--(五)

Elasticearch Filter Query 过滤查询--(五)

Elasticearch Filter Query 过滤查询

1,过滤查询

ES查询操作分为2种:查询(query)和 过滤(filter)。
1. Query,默认计算每个返回文档的得分,然后根据得分排序。
2. Filter,只会筛选符合条件的文档,不计算得分,可以缓存文档。
注:单从性能考虑,过滤查询更快。过滤适合在大范围筛选数据,而查询适合精确匹配数据。一般应用时,先使用过滤操作过滤数据,然后使用查询匹配数据。

2,使用语法

GET /lanlan/_search
{
“query”: {
“bool”: {
“must”: [
{}
],
“filter”: [
{}
]
}
}
}

注:
在执行 filter 和 query 时,先执行 filter 在执行 query
Elasticsearch 会自动缓存经常使用的过滤器,以加快性能

3,常见过滤类型

• term
• terms
• range
• exists
• ids

4,Filter query 案例
4.1,term 过滤查询

GET /lanlan/_search
{
“query”: {
“bool”: {
“must”: [
{
“match_all”: {}
}
],
“filter”: [
{
“term”: {
“description”: “真”
}
}
]
}
}
}

4.2,terms 多值过滤查询

GET /lanlan/_search
{
“query”: {
“bool”: {
“must”: [
{
“match_all”: {}
}
],
“filter”: [
{
“terms”: {
“age”: [
12,
11,
18,
20
]
}
}
]
}
}
}

4.3,range 范围过滤查询

GET /lanlan/_search
{
“query”: {
“bool”: {
“must”: [
{
“term”: {
“description”: {
“value”: “西”
}
}
}
],
“filter”: [
{
“range”: {
“age”: {
“gte”: 10,
“lte”: 30
}
}
}
]
}
}
}

4.4,exists 存在过滤查询

GET /lanlan/_search
{
“query”: {
“bool”: {
“must”: [
{
“range”: {
“age”: {
“gte”: 10,
“lte”: 18
}
}
}
],
“filter”: [
{
“exists”: {
“field”: “id”
}
}
]
}
}
}

4.5,ids 过滤查询

GET /lanlan/_search
{
“query”: {
“bool”: {
“must”: [
{
“match_all”: {}
}
],
“filter”: [
{
“ids”: {
“values”: [
1,
2,
3,
4,
5,
6
]
}
}
]
}
}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/728934.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号