【ElasticSearch教程】--- Elasticsearch文档多条件查询(十二)

多条件查询

同时满足must

查询的body体的查询不能在使用match关键字了,而需要使用,bool然后要多个条件同时成立,接下来要填入must,多个条件用数组。body样例如下:

{
    "query":{
        "bool": {
            "must":[
                {
                    "match":{
                        "category":"床上用品"
                    }
                }
            ]
        }
    }
}

以上看起来就相当于是单条件要一样的效果。然后我们再加一个条件。如下:

{
    "query":{
        "bool": {
            "must":[
                {
                    "match":{
                        "category":"床上用品"
                    }
                },
                {
                    "match":{
                        "price":169.0
                    }
                }
                    
            ]
        }
    }
}

多条件查询
返回结果:

{
    "took": 17,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.7292862,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1001",
                "_score": 1.7292862,
                "_source": {
                    "title": "水星家纺被子夏季空调被 夏凉被夏天被子双人夏被被芯学生宿舍被子200*230cm",
                    "category": "床上用品",
                    "image": "https://img11.360buyimg.com/n7/jfs/t1/191056/14/24248/144443/62767f75E23ccafc4/5110691845cfa4cf.jpg",
                    "price": 169.0
                }
            }
        ]
    }
}

返回结果就同时满足了 "category":"床上用品""price":169.0

部分满足should

部分满足类似于sql语句中的or.
比如我们要查category床上用品3C数码的.

{
    "query":{
        "bool": {
            "should":[
                {
                    "match":{
                        "category":"床上用品"
                    }
                },
                {
                    "match":{
                        "category":"3C数码"
                    }
                }
                    
            ]
        }
    }
}

操作如下
should多条件查询

范围条件filter

比如我期望查询的是价格在500以上的产品。
查询内容体:

{
    "query":{
        "bool": {
            "should":[
                {
                    "match":{
                        "category":"床上用品"
                    }
                },
                {
                    "match":{
                        "category":"3C数码"
                    }
                }
                    
            ],
            "filter":{
                "range":{
                    "price":{
                        "gt":500
                    }
                }
            }
        }
        
    }
}

操作结果
范围查询
响应内容:

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 3.1789374,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1003",
                "_score": 3.1789374,
                "_source": {
                    "title": "华为手机",
                    "category": "3C数码",
                    "image": "https://img10.360buyimg.com/n7/jfs/t1/111175/35/24913/81730/625ed1a5Ed8f452a3/d0370cd4e6837908.jpg",
                    "price": 1699.0
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1002",
                "_score": 1.8126037,
                "_source": {
                    "title": "水星家纺 60S长绒棉贡缎床上四件套 酒店高档纯棉床单被套枕套 被罩被单1.8米床乔伊斯玫瑰(海蓝色/抗菌)",
                    "category": "床上用品",
                    "image": "https://www.shuixing.com/pc/images/2022/logo_02.png",
                    "price": 899.0
                }
            }
        ]
    }
}

响应的条件只有2条了,过滤掉了价格低与500的。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值