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

  • Post author:
  • Post category:其他

因为记不住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
                    }
                }
            ]
        }
    }
}
'

 

 


版权声明:本文为qq_24570443原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。