当前位置 博文首页 > m0_55415810的博客:es查询,json格式查询,基础查询

    m0_55415810的博客:es查询,json格式查询,基础查询

    作者:[db:作者] 时间:2021-09-22 16:51

    备注:

    1、query及sort等条件为json格式的一级条件

    1、单条件查询

    query 中放筛选类型,match为单条件查询,match内部放筛选条件

    {
    	"query": {
    		"match": {
    			"seller.tax_code": "92510100MA6C5AH52D"
    		}
    	}
    }

    2、查询结果排序

    sort代表排序,与query同一级别,sort内部放排序条件和排序方式(desc 降序,asc 升序)

    {
    	"query": {
    		"match": {
    			"seller.tax_code": "92510100MA6C5AH52D"
    		}
    	},
    	"sort": {
    		"created_at": "asc"
    	}
    }

    3、查询结果相关信息

    took:查询花费时长(毫秒)

    timed_out:请求是否超时

    ?_shards:搜索了多少分片,成功、失败或者跳过了多个分片(明细)

    hits.total.value : 查询结果条数

    hits.total.relation : #查询的关系(我猜测的,哈哈)

    {
        "took": 1,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 468,
                "relation": "eq"
            },
            "max_score": null,
            "hits": [
                {内部数据,
                        "created_at": 1558073693,
                        
                    },
                    "sort": [
                        1558073693
                    ]
                }
            ]
        }
    }

    4、分页查询

    类似sql的limit,from和size都与query同级,from表示从第n-1个记录开始(mysql中第一条是0),size表示查询往后的n条记录。

    {
    	"query": {
    		"match": {
    			"seller.tax_code": "92510100MA6C5AH52D"
    		}
    	},
    	"sort": {
    		"created_at": "asc"
    	},
    	"from": "0",
    	"size": "1"
    }

    5、多种match(匹配查询)

    ?match 模糊搜索 (搜索中文时,容易出错),分词,包含任一分词就会展示

    match_phrase 精准搜索(必须包含一模一样的串,才会返回),分词,包含所以分词就会展示

    term查询与match的效果类似,区别是term不分词,match会分词

    terms查询类似于sql中的in效果,同样也是不能分词

    可以参考:https://blog.csdn.net/qq_28988969/article/details/96178111

    {
    ?? ?"query": {
    ?? ??? ?"terms": {
    ?? ??? ??? ?"invoice.invoice_type": ["026", "007","028"]
    ?? ??? ?}
    ?? ?}
    }

    6、多条件查询(bool联合查询: must,should,must_not,filter)可以相互嵌套使用

    must :类似sql的 and

    should :类似sql的or?

    must_not : 类似sql的 not? and not

    filter: 作用和must一样,但是仅过滤,不评分,所以性能更高(可以直接替换must,查询结果是一样的)

    {
        "query":{
            "bool":{
            "must":[
                {"match":{"invoice.invoice_type":"026"}},
                {"match":{"merchant.tax_code":"92510100MA6C5AH52D"}},
                {"match_phrase":{"buyer.title":"金牛区慧宸"}}
        ]}},
        "sort":{"created_at":"desc"}
    }
    
    

    相互嵌套示例:

    {
        "query":{
            "bool":{
            "must":[
                {"match":{"merchant.tax_code":"91510100MA6CT7K03N"}},
                {"match_phrase":{"buyer.title":"金牛区慧宸"}},
                {"bool":{
                    "should":[
                        {"match":{"invoice.invoice_type":"026"}},
                        {"match":{"invoice.invoice_type":"007"}}
                    ]
                }}
        ]
        }},
        "sort":{"created_at":"desc"}
    }
    
    

    cs