elasticsearch6.8 基础查询类型(must、must_not、should、filter)

因为记不住DSL语句的写法,本文为个人记录elasticsearch查询示例,以免用到的时候到处百度。

 

https://github.com/memoryFuhao/elasticsearch_client  (打个广告 以上链接是本人开发的一个es客户端工具,支持es大部分 CRUD操作  分页、分组、嵌套分组、and or ···,有需要的朋友可以pull代码直接使用)

 

第一步:创建测试索引

curl -H "Content-Type: application/json" -H "Authorization: Basic ZWxhc3RpYzoxMjM0NTY=" -XPUT "http://172.16.1.119:9200/test" -d'
{
    "mappings": {
        "data": {
            "properties": {
                "name": {
                    "type": "keyword",
                    "index": "true"
                },
                "age": {
                    "type": "integer",
                    "index": "true"
                },
                "sex": {
                    "type": "integer",
                    "index": "true"
                }
            }
        }
    }
}
'

第二步:插入测试数据

curl -H "Authorization: Basic ZWxhc3RpYzoxMjM0NTY=" -X POST "http://172.16.1.119:9200/_bulk?pretty" -H 'Content-Type: application/json' -d'
{ "index" : { "_index" : "test", "_type" : "data", "_id" : "1" } }
{ "name" : "fuhao","age":11 ,"sex":0}
{ "index" : { "_index" : "test", "_type" : "data", "_id" : "2" } }
{ "name" : "fuhao","age":22 ,"sex":1}
{ "index" : { "_index" : "test", "_type" : "data", "_id" : "3" } }
{ "name" : "fuhao","age":33 ,"sex":1}
{ "index" : { "_index" : "test", "_type" : "data", "_id" : "4" } }
{ "name" : "xiaohong","age":11 ,"sex":0}
{ "index" : { "_index" : "test", "_type" : "data", "_id" : "5" } }
{ "name" : "xiaohong","age":22 ,"sex":1}
{ "index" : { "_index" : "test", "_type" : "data", "_id" : "6" } }
{ "name" : "xiaohong","age":33 ,"sex":1}
{ "index" : { "_index" : "test", "_type" : "data", "_id" : "7" } }
{ "name" : "xiaoming","age":11 ,"sex":0}
{ "index" : { "_index" : "test", "_type" : "data", "_id" : "8" } }
{ "name" : "xiaoming","age":22 ,"sex":1}
{ "index" : { "_index" : "test", "_type" : "data", "_id" : "9" } }
{ "name" : "xiaoming","age":33 ,"sex":1}
'

 

第三步:构建查询DSL语句

    should查询↓

curl -H "Authorization: Basic ZWxhc3RpYzoxMjM0NTY=" -X POST "http://172.16.1.119:9200/test/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
        "bool": {
            "should": [
                {
                    "term": {
                        "name": "fuhao"
                    }
                },
                {
                    "term": {
                        "age": 11
                    }
                }
            ]
        }
    }
}
'

    filter查询↓

curl -H "Authorization: Basic ZWxhc3RpYzoxMjM0NTY=" -X POST "http://172.16.1.119:9200/test/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
        "bool": {
            "filter": [
                {
                    "term": {
                        "name": "fuhao"
                    }
                },
                {
                    "term": {
                        "age": 11
                    }
                }
            ]
        }
    }
}
'


ps:filter和must大致相同,唯一区别是must有评分,而filter只是作为过滤,所以filter性能优于must

    must查询↓

curl -H "Authorization: Basic ZWxhc3RpYzoxMjM0NTY=" -X POST "http://172.16.1.119:9200/test/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "name": "fuhao"
                    }
                },
                {
                    "term": {
                        "age": 11
                    }
                }
            ]
        }
    }
}
'

ps:filter和must大致相同,唯一区别是must有评分,而filter只是作为过滤,所以filter性能优于must

    must_not查询↓

curl -H "Authorization: Basic ZWxhc3RpYzoxMjM0NTY=" -X POST "http://172.16.1.119:9200/test/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
        "bool": {
            "must_not": [
                {
                    "term": {
                        "sex": 1
                    }
                },
                {
                    "term": {
                        "age": 22
                    }
                }
            ]
        }
    }
}
'

 

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值