elasticsearch教程(六)之filter查询

本文深入探讨了Elasticsearch中Filter与Query的区别,详细解释了Filter如何提高搜索效率,尤其是在组合过滤查询和范围查询方面的应用。文章通过具体实例展示了不同类型的过滤条件,如term、terms、range和exists等,以及它们在实际场景中的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#filter查询是不计算下相关性的
#同时可以cache,filter速度要快于query

准备数据

POST /lib2/item/_bulk
{"index":{"_id":1}}
{"price":40,"itemID":"10301"}
{"index":{"_id":2}}
{"price":50,"itemID":"10302"}
{"index":{"_id":3}}
{"price":60,"itemID":"10303"}
{"index":{"_id":4}}
{"price":70,"itemID":"10304"}
{"index":{"_id":5}}
{"price":80,"itemID":"10305"}


  

GET /lib2/item/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "price":40
        }
      }
    }
  }
}

#terms不支持参数是数组式的查询

GET /lib2/item/_search
{
  "query": {
    "bool": {
      "filter": {
        "terms": {
          "price":[40,50]
        }
      }
    }
  }
}

#动态创建mapping是,文本类型的数据,在存储的时候会被转化成小写,查询的时候也要写成小写

#bool过滤查询
#可以实现组合过滤查询
#格式:{"bool":{"must":[],"should":[],"must_not":[]}}

#must:必须满足的条件-----and
#should:可以满足也可以不满足的条件---or
#must_not:不需要满足的条件--not

GET /lib2/item/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "price": 40
          }
        },
        {
          "term": {
            "itemID": "10302"
          }
        }
      ],
      "must_not": [
        {
          "term": {
           "price": 60
          }
        }
      ]
      
    }
  }
}


#查询itemID为10301或者(itemID为10302且price为50)

GET /lib2/item/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
           "itemID": "10301"
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                 "itemID": "10302"
                }
              },
              {
                "term": {
                 "price": 50
                }
              }
            ]
          }
        }
      ]
    }
  }
}

#范围查询
# gt:  >
# lt:  <
# gte: >=
# lte: <=

GET /lib2/item/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "price": {
            "gt":40,
            "lt":60
          }
        }
      }
    }
  }
}

#查询price属性不为null的文档

GET /lib2/item/_search
{
  "query": {
    "bool": {
      "filter": {
        "exists": {
          "field": "price"
        }
      }
    }
  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值