• ES(elasticSearch学习笔记)


    什么是es

    es是面向文档型的数据库,一条数据在es数据库就是一个文档,和关系型数据库做一个类比:
    1:es的索引类比关系型数据库的数据库,
    2:es的type类比数据表(7.0版本以上删除了type这个概念),
    3:es的索引下的文档document类比关系型数据库的行,新增时可以指定文档主键id,不指定会自动生成
    4:es的文档中的字段类比关系关系型数据库的列。

    现实生产中一般一个索引类似张表,但是一个索引下的字段还可以是一个对象,可以嵌套。
    match每一次全文搜索分词是针对某一个字段的,可以是嵌套搜索,一次全文搜索不是针对整个索引的全部字段,想要同时全文搜多个字段可以一次请求中match多个字段

    put请求新建索引,post/put请求新增数据,新增的数据就是一个文档,新增会有一个文档id就是索引下一个数据的主键

    涉及的操作索引的增删改查和文档的增删改查

    什么是倒排索引

    在这里插入图片描述

    es基本入门操作

    1.安装访问
    在本地es执行目录:/Users/liuqinhua/Desktop/错误/elasticsearch-7.6.2/bin
    执行可执行文件:./elasticsearch
    访问
    在这里插入图片描述
    2.创建索引put请求http://localhost:9200/liuqinhua(liuqinhua是索引名)
    在这里插入图片描述

    在这里插入图片描述

    3**.get请求查看索引信息**:http://localhost:9200/索引名
    例如:http://localhost:9200/liuqinhua
    在这里插入图片描述

    4.get请求查看所有的索引:http://localhost:9200/_cat/indices?v
    这时候他是强制返回的内容是text格式的,所以会展示成表格的格式
    在这里插入图片描述
    5.delete请求删除索引wudonghui:http://localhost:9200/索引名
    例如:http://localhost:9200/wudonghui
    在这里插入图片描述

    6.post请求给索引新增文档数据http://localhost:9200/索引名字/_doc
    eg:http://localhost:9200/liuqinhua/_doc
    这种会返回文档id
    或者post或者put请求:http://localhost:9200/liuqinhua/_doc/自定义文档id

    在这里插入图片描述
    在这里插入图片描述

    7.get请求查询索引的数据
    查询某一个文档id内容
    http://localhost:9200/索引名/_doc/文档id
    例如:http://localhost:9200/liuqinhua/_doc/1001
    在这里插入图片描述

    get请求查询某一个索引下所有的文档
    http://localhost:9200/索引名/_search
    例如:
    http://localhost:9200/liuqinhua/_search

    在这里插入图片描述

    put请求根据索引下的主键id全量修改更新某一条文档数据

    格式:http://localhost:9200/索引名/_doc/数据主键id
    eg:http://localhost:9200/liuqinhua/_doc/1001

    在这里插入图片描述
    在这里插入图片描述
    put全量会把该主键id的文档数据变成这一次传入的
    在这里插入图片描述

    在这里插入图片描述
    再更新一下:
    在这里插入图片描述

    在这里插入图片描述

    post请求局部更新某索引下某主键文档id数据
    http://localhost:9200/索引名/_update/文档主键id
    eg:http://localhost:9200/liuqinhua/_update/1001
    在这里插入图片描述
    在这里插入图片描述

    delete请求删除索引下某主键的文档
    格式http://localhost:9200/索引名/_doc/文档主键id
    在这里插入图片描述

    条件查询

    url拼接查询条件
    格式:http://localhost:9200/liuqinhua/_search?q=文档下一级key :文档下一级value
    二级的key-value是搜不出来的

    在这里插入图片描述

    查询条件放requestbody进行查询放入query-match里面
    在这里插入图片描述
    放requestbody全量查询索引下文档
    在这里插入图片描述
    分页查询
    在这里插入图片描述

    get请求http://localhost:9200/索引名/_search加reqbody里面的多条件查询和范围查询

    reqbody内容

    {
        "query":{
            "bool":{
                "should":[// must相当于mysql的and,should相当于or
                    {
                        "match":{
                         "uname" :"forbidden"
                          //"gender":26
                      }
                    },
                    {
                        "match":{
                         //"uname" :"forbidden"
                          "gender":26
                      }
                    }
                ],
                "filter":{//过滤
                    "range":{//范围查询
                        "gender":{
                            "gt":26//gt表示大于,gender大于26岁的
                        }
                    }
                }
            }
            
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    查出结果

    {
        "took": 23,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 4,
                "relation": "eq"
            },
            "max_score": 1.4769237,
            "hits": [
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "-rD414IBzFC3z-tJgVq3",
                    "_score": 1.4769237,
                    "_source": {
                        "schoolId": "2000014495",
                        "teacherUid": "100000094198",
                        "uname": "forbidden zhang",
                        "unamePing": "forbiddenzhang",
                        "gender": 28
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "-7D414IBzFC3z-tJ2Fqf",
                    "_score": 1.4769237,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094198",
                        "uname": "forbidden city",
                        "unamePing": "forbiddencity",
                        "gender": 30
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "_LD714IBzFC3z-tJBFra",
                    "_score": 1.4769237,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094198",
                        "uname": "forbidden city",
                        "unamePing": "forbiddencity",
                        "gender": 30
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "_bAC2IIBzFC3z-tJB1qB",
                    "_score": 0.0,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094199",
                        "uname": "sijiali",
                        "unamePing": "sijiali",
                        "gender": 30
                    }
                }
            ]
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71

    全文检索和完全匹配

    url同上,全文检索的核心就是分词后用了倒排,,中文一个字会分做一个词,英文字母是以空格结束当做是一个单词做分词,连续没有空格的n个数字不会分成多个词

    reqbody内容:”forbidden 小“把这两个字分词把还有forbidden 和小都查了出来

    {
        "query":{
            "bool":{
                "must":[// must相当于mysql的and,should相当于or
                    {
                        "match":{
                         "uname" :"sijiali 华"
                          //"gender":26
                      }
                    },
                    {
                        "match":{
                         "unamePing" :"sijiali liu"
                          //"gender":26
                      }
                    }
            
                ]
                
            }
            
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    {
        "took": 146,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 9,
                "relation": "eq"
            },
            "max_score": 4.4716845,
            "hits": [
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "A7BM2IIBzFC3z-tJ01ts",
                    "_score": 4.4716845,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094119",
                        "uname": "小华",
                        "unamePing": "sijiali liu",
                        "gender": 31
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "ArBI2IIBzFC3z-tJuVt_",
                    "_score": 3.5823827,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094119",
                        "uname": "a sijiali 100",
                        "unamePing": "sijiali liu",
                        "gender": 31
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "BbBP2IIBzFC3z-tJA1sN",
                    "_score": 3.3120766,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094119",
                        "uname": "小华",
                        "unamePing": "xiao liu hua",
                        "gender": 31
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "BLBM2IIBzFC3z-tJ41s_",
                    "_score": 3.2610822,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094119",
                        "uname": "小华",
                        "unamePing": "sijiali ",
                        "gender": 31
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "_bAC2IIBzFC3z-tJB1qB",
                    "_score": 2.917797,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094199",
                        "uname": "sijiali",
                        "unamePing": "sijiali",
                        "gender": 30
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "_rAv2IIBzFC3z-tJrFrx",
                    "_score": 2.3717804,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094199",
                        "uname": "a sijiali b",
                        "unamePing": "sijiali",
                        "gender": 30
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "_7A12IIBzFC3z-tJBlrv",
                    "_score": 2.3717804,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094119",
                        "uname": "a sijiali 9",
                        "unamePing": "sijiali",
                        "gender": 30
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "ALA32IIBzFC3z-tJa1vV",
                    "_score": 2.3717804,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094119",
                        "uname": "a sijiali 99",
                        "unamePing": "sijiali",
                        "gender": 30
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "AbBE2IIBzFC3z-tJEFt2",
                    "_score": 2.3717804,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094119",
                        "uname": "a sijiali 100",
                        "unamePing": "sijiali",
                        "gender": 31
                    }
                }
            ]
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136

    完全匹配:把match改为match_phrase,相当于mysql的like “%str%”

    {
        "query":{
            "bool":{
                "must":[// must相当于mysql的and,should相当于or
                    {
                        "match_phrase":{
                         "uname" :"sijiali"
                          //"gender":26
                      }
                    }
            
                ]
                
            }
            
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    返回

    {
        "took": 2,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 2,
                "relation": "eq"
            },
            "max_score": 2.581265,
            "hits": [
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "_bAC2IIBzFC3z-tJB1qB",
                    "_score": 2.581265,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094199",
                        "uname": "sijiali",
                        "unamePing": "sijiali",
                        "gender": 30
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "_rAv2IIBzFC3z-tJrFrx",
                    "_score": 1.7744548,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094199",
                        "uname": "a sijiali b",
                        "unamePing": "sijiali",
                        "gender": 30
                    }
                }
            ]
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    分词中文和数字会分作两个词,字母和数字不会分做两个词

    中文和数字

    {
        "query":{
            "bool":{
                "must":[// must相当于mysql的and,should相当于or
                    {
                        "match":{
                         "uname" :"小9"
                          //"gender":26
                      }
                    }
            
                ]
                
            }
            
        }
    }
    
    
    
    
    返回{
        "took": 925,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 4,
                "relation": "eq"
            },
            "max_score": 2.2984395,
            "hits": [
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "_7A12IIBzFC3z-tJBlrv",
                    "_score": 2.2984395,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094119",
                        "uname": "a sijiali 9",
                        "unamePing": "sijiali",
                        "gender": 30
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "8bDn14IBzFC3z-tJ1lov",
                    "_score": 1.5314121,
                    "_source": {
                        "schoolId": "2000014494",
                        "teacherUid": "100000094187",
                        "uname": "刘小君",
                        "unamePing": "lingweijun01",
                        "gender": 23
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "8LDm14IBzFC3z-tJDVor",
                    "_score": 1.5314121,
                    "_source": {
                        "schoolId": "2000014494",
                        "teacherUid": "100000094186",
                        "uname": "凌小君",
                        "unamePing": "lingweijun01",
                        "gender": 23
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "77Dl14IBzFC3z-tJ2FrK",
                    "_score": 1.5314121,
                    "_source": {
                        "schoolId": "2000014494",
                        "teacherUid": "100000094185",
                        "uname": "凌小军",
                        "unamePing": "lingweijun01",
                        "gender": 23
                    }
                }
            ]
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92

    英文和数字

    {
        "query":{
            "bool":{
                "must":[// must相当于mysql的and,should相当于or
                    {
                        "match":{
                         "uname" :"a99"
                          //"gender":26
                      }
                    }
            
                ]
                
            }
            
        }
    }
    
    返回
    {
        "took": 1,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 0,
                "relation": "eq"
            },
            "max_score": null,
            "hits": []
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    聚合查询

    聚合查询相当于mysql的group by

    {
        "aggs":{//定义聚合
            "fenzu":{//年龄进行分组,给聚合起个名字
                "terms":{//聚合的类型是 term
                    "field":"gender"//参与聚合的字段
                    "size": 10 // 希望获取的聚合结果数量
                }
    
            }
        },
        "size":0//表示不返回文档数组列表0条,设置size为0,结果中不包含文档,只包含聚合结果
    }
    
    
    
    返回
    
    {
        "took": 25,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 21,
                "relation": "eq"
            },
            "max_score": null,
            "hits": []
        },
        "aggregations": {
            "fenzu": {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets": [
                    {
                        "key": 30,
                        "doc_count": 6
                    },
                    {
                        "key": 23,
                        "doc_count": 5
                    },
                    {
                        "key": 26,
                        "doc_count": 4
                    },
                    {
                        "key": 25,
                        "doc_count": 2
                    },
                    {
                        "key": 24,
                        "doc_count": 1
                    },
                    {
                        "key": 28,
                        "doc_count": 1
                    },
                    {
                        "key": 31,
                        "doc_count": 1
                    }
                ]
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71

    映射关系

    映射类似关系型数据库中的schema,描述了文档中有哪些字段和字段的类型,如string,data,int等。而 类型 是一组具有相关性的映射组成,然后使用"properties"来表示该类型中可能包含的字段属性。如果不设置,创建文档的时候会根据每个字段值自动匹配映射关系

    可以通过mapping字段修改索引的映射关系

    例如

     
    put    http://ip:9200/index_name
     
    {
        "mappings": {
            "properties": {
                "realname": {
                    "type": "text",
                    "index": true
                            },
                "username": {
                    "type": "keyword",
                    "index": false
                            }
            }
        }
    }
     
    text: 大的长文本,需要做分词,做倒排索引
     
    keyword: 不会被分词,也不会被做倒排索引。做精确匹配的搜索(比如订单状态,用的微信号,qq号,手机号)
     
    text , keyword 都是属于string类型。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    ES 内置分词器

    standard:默认分词,单词会被拆分,大小会转换为小写。
    simple:按照非字母分词。大写转为小写。
    whitespace:按照空格分词。忽略大小写。 s
    top:去除无意义单词,比如 the / a / an / is …
    keyword:不做分词。把整个文本作为一个单独的关键词

    索引下字段的子字段类型,这种情况相当于是一个字段是一个对象的json字符串,但是索引的一级字段没下一层级字段,会有一个默认字段keyword

    PUT index_name
    {
      "mappings": {
        "properties": {
          "title": {               # 字段名称
            "type": "text",        # 字段类型
            "analyzer": "english", # 字段分词器
            "fields": {            # 多字段域,固定写法
              "std1": {             # 子字段名称
                "type": "text",    # 子字段类型
                "analyzer": "standard"  # 子字段分词器
               },
               "ziziduan":{
                    "type": "text",    # 子字段类型
                "analyzer": "standard"  # 子字段分词器
               }
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    es嵌套字段是一个对象,第一层字段直接match搜不出来

    content是一个对象,不是一个字符串
    索引全部文档内容:
    在这里插入图片描述

    content是一个对象,不是一个字符串,直接match查嵌套字段里面内容查不出来,是一个字符串就可以,:

    在这里插入图片描述

    在这里插入图片描述

    term查询完全匹配

    进一步优化查询,因为是精准查询,不需要查询进行评分计算,只希望对文档进行包括或排除的计算,所以我们会使用 constant_score 查询以非评分模式来执行 term 查询并以一作为统一评分。推荐如下查询

    参考

    {
        "query":{
            "bool":{
                "should":[// must相当于mysql的and,should相当于or
                    {
                        "match":{
                         "uname" :"forbidden"
                          //"gender":26
                      }
                    },
                    {
                        "match":{
                         //"uname" :"forbidden"
                          "gender":0
                      }
                    }
                ],
                "filter":{//过滤
                    "term":{
                        "gender":30
                    }
                }
            }
            
        }
    }
    
    
    返回
    
    
    {
        "took": 3,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 6,
                "relation": "eq"
            },
            "max_score": 1.8449266,
            "hits": [
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "_LD714IBzFC3z-tJBFra",
                    "_score": 1.8449266,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094198",
                        "uname": "forbidden city",
                        "unamePing": "forbiddencity",
                        "gender": 30
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "-7D414IBzFC3z-tJ2Fqf",
                    "_score": 1.8449266,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094198",
                        "uname": "forbidden city",
                        "unamePing": "forbiddencity",
                        "gender": 30
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "_rAv2IIBzFC3z-tJrFrx",
                    "_score": 0.0,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094199",
                        "uname": "a sijiali b",
                        "unamePing": "sijiali",
                        "gender": 30
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "_bAC2IIBzFC3z-tJB1qB",
                    "_score": 0.0,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094199",
                        "uname": "sijiali",
                        "unamePing": "sijiali",
                        "gender": 30
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "_7A12IIBzFC3z-tJBlrv",
                    "_score": 0.0,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094119",
                        "uname": "a sijiali 9",
                        "unamePing": "sijiali",
                        "gender": 30
                    }
                },
                {
                    "_index": "user",
                    "_type": "_doc",
                    "_id": "ALA32IIBzFC3z-tJa1vV",
                    "_score": 0.0,
                    "_source": {
                        "schoolId": "2000014485",
                        "teacherUid": "100000094119",
                        "uname": "a sijiali 99",
                        "unamePing": "sijiali",
                        "gender": 30
                    }
                }
            ]
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128

    goland接入ES的api

    配置

    elastic:
      zlwenxue-cli:
        service: zlwenxue-cli
        addr: "http://127.0.0.1:9200/"
        username: "liuqinhua" #电脑的账号密码
        password: "123456"
        gzip: false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    代码

    package es
    
    import (
    	"errors"
    	"github.com/gin-gonic/gin"
    	"github.com/olivere/elastic"
    	"reflect"
    	"zlwenxue-cli/components"
    	"zlwenxue-cli/helpers"
    )
    
    /**
     * @Author: liuqinhua
     * @Date: 2022/8/29 11:03 AM
     */
    type User struct {
    	SchoolId   string `json:"schoolId"`
    	TeacherUid string `json:"teacherUid"`
    	Uname      string `json:"uname"`
    	UnamePing  string `json:"unamePing"`
    	Gender     int    `json:"gender"`
    }
    
    func AddUserEs(ctx *gin.Context, record *User) (id string, err error) {
    	if record == nil {
    		return "", errors.New("插入的数据为空")
    	}
    	responseBody, err := helpers.ElasticClient.Index().Index("user").BodyJson(record).Do(ctx)
    	if err != nil {
    		return "", components.ErrorEsInsert.Wrap(err)
    	}
    	return responseBody.Id, err
    }
    
    func BoolQueryUser(ctx *gin.Context, schoolId, uname, unameping string, gender int) (userList []*User, err error) {
    	boolquery := &elastic.BoolQuery{}
    	if len(schoolId) > 0 {
    		boolquery.Filter(elastic.NewTermQuery("schoolId", schoolId))
    	}
    	if gender > 0 {
    		boolquery.Filter(elastic.NewTermQuery("gender", gender))
    	}
    	if len(unameping) > 0 { //should下面也可以完全匹配
    		boolquery.Should(elastic.NewTermQuery("unamePing", unameping))
    	}
    	if len(uname) > 0 {
    		boolquery.Should(elastic.NewMatchQuery("uname", uname))
    	}
    	searchByMatch, err := helpers.ElasticClient.Search("user").Query(boolquery).From(0).Size(11).Do(ctx)
    	if err != nil {
    		return userList, components.ErrorEsInsert.Wrap(err)
    	}
    	var resultType User
    	for _, item := range searchByMatch.Each(reflect.TypeOf(resultType)) {
    		answerHistory := item.(User)
    		userList = append(userList, &answerHistory)
    	}
    	return
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    测试

    package es
    
    import (
    	"fmt"
    	"github.com/gin-gonic/gin"
    	"net/http/httptest"
    	"testing"
    	"zlwenxue-cli/test"
    )
    
    /**
     * @Author: liuqinhua
     * @Date: 2022/8/29 11:04 AM
     */
    
    func TestUserEs(t *testing.T) {
    	test.Init()
    	ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
    	//answerHistoryEs := createQueryAnswerHistoryEs()
    
    	user1 := &User{
    		SchoolId:   "111111",
    		TeacherUid: "123456123",
    		UnamePing:  "goland test2",
    		Uname:      "goland nihao test clients 111",
    	}
    
    	id, err := AddUserEs(ctx, user1)
    	if err != nil {
    		t.Errorf("%+v", err)
    		return
    	}
    	fmt.Println(id)
    	fmt.Println("=========")
    	list, err := BoolQueryUser(ctx, "", "goland", "", 0)
    	fmt.Println(len(list))
    	for _, V := range list {
    		fmt.Printf("%+v", V)
    		fmt.Println()
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    结果

    在这里插入图片描述

    match最小匹配度

    match的字段下加上minimum_should_match可以加百分数也可以加数字
    表示分词后必须命中的最小词数或者百分比
    参考

    book索引下的所有文档

    {
        "took": 809,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 5,
                "relation": "eq"
            },
            "max_score": 1.0,
            "hits": [
                {
                    "_index": "book",
                    "_type": "_doc",
                    "_id": "BrCJ2YIBzFC3z-tJA1vB",
                    "_score": 1.0,
                    "_source": {
                        "bookId": "2000014485",
                        "title": "标题1",
                        "content": {
                            "duanluo": "1",
                            "zhaiyao": "摘要1",
                            "内容": "啊啊啊不不不"
                        }
                    }
                },
                {
                    "_index": "book",
                    "_type": "_doc",
                    "_id": "CLCu2YIBzFC3z-tJvVsG",
                    "_score": 1.0,
                    "_source": {
                        "bookId": "2000014485",
                        "title": "标题2",
                        "alltext": "{\"duanluo\": \"2\",\"zhaiyao\": \"摘要2\",\"内容\": \"啊啊啊不不不2222\"}"
                    }
                },
                {
                    "_index": "book",
                    "_type": "_doc",
                    "_id": "SM2e7IIBO-2eYCM19Rdj",
                    "_score": 1.0,
                    "_source": {
                        "bookId": "2000014486",
                        "title": "李小龙(英文名:Bruce Lee,1940年11月27日-1973年7月20日),本名李振藩,出生于美国加利福尼亚州旧金山",
                        "alltext": "1962年李小龙开办“振藩国术馆”,1967年自创截拳道,1973年7月20日,李小龙在香港逝世,年仅32岁。1979年美国洛杉矶市政府将补拍版《死亡游戏》的开映日6月8日定为“李小龙日”(7月8日为错误翻译)。1993年美国发行李小龙逝世20周年纪念钞票,好莱坞星光大道铺上李小龙纪念星徽;同年,获香港电影金像奖大会颁发“终身成就奖”。1998年11月,获中国武术协会颁发“武术电影巨星奖”。"
                    }
                },
                {
                    "_index": "book",
                    "_type": "_doc",
                    "_id": "Sc2f7IIBO-2eYCM1bxfY",
                    "_score": 1.0,
                    "_source": {
                        "bookId": "2000014486",
                        "title": "周星驰(Stephen Chow),1962年6月22日出生于香港,祖籍浙江省宁波市,中国香港影视男演员、导演、编剧、制作人、商人,毕业于无线电视艺员训练班",
                        "alltext": "1980年成为丽的电视台的特约演员,从而进入演艺圈。1981年出演个人首部电视剧《IQ成熟时》。1988年将演艺事业的重心转向大银幕,并于同年出演电影处女作《捕风汉子》。1990年凭借喜剧片《一本漫画闯天涯》确立其无厘头的表演风格;同年,因其主演的喜剧动作片《赌圣》打破香港地区票房纪录而获得关注。1991年主演喜剧片《逃学威龙》,并再次打破香港地区票房纪录 [1]  。1995年凭借喜剧爱情片《大话西游》奠定其在华语影坛的地位。1999年自导自演的喜剧片《喜剧之王》获得香港电影年度票房冠军 [2]  。"
                    }
                },
                {
                    "_index": "book",
                    "_type": "_doc",
                    "_id": "Ss2g7IIBO-2eYCM1NBeJ",
                    "_score": 1.0,
                    "_source": {
                        "bookId": "2000014486",
                        "title": "黄家驹(1962年6月10日—1993年6月30日),出生于中国香港,祖籍广东省台山市",
                        "alltext": "中国香港男歌手、原创音乐人、吉他手,摇滚乐队Beyond的主唱及创队成员。1983年组建Beyond乐队,担任主唱,"
                    }
                }
            ]
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    查询语句

    {
        "query":{
            "bool":{
                "should":[// must相当于mysql的and,should相当于or
                    {
                        "match":{
                         "title" :{//title是字段
                             "query":"出生于相关",//全文查询内容
                             "minimum_should_match":"60%"//是指最少匹配百分比。是指将 query 分词后的词的数量 A,要求///匹配中的词的数量b,b/a 即为正数的百分比。
                            // "minimum_should_match":"3"//是指将 query 分词后的词的数量 A,要求从 A 中最少匹配中的///词的数量 B。这里匹配中的词是:出,生,于。
                         }
                          //"gender":26
                          
                      }
                      
                    }
                ]
                
            }
            
        }
    }
    查询结果
    
    
    {
        "took": 3,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 3,
                "relation": "eq"
            },
            "max_score": 1.5160279,
            "hits": [
                {
                    "_index": "book",
                    "_type": "_doc",
                    "_id": "Ss2g7IIBO-2eYCM1NBeJ",
                    "_score": 1.5160279,
                    "_source": {
                        "bookId": "2000014486",
                        "title": "黄家驹(1962年6月10日—1993年6月30日),出生于中国香港,祖籍广东省台山市",
                        "alltext": "中国香港男歌手、原创音乐人、吉他手,摇滚乐队Beyond的主唱及创队成员。1983年组建Beyond乐队,担任主唱,"
                    }
                },
                {
                    "_index": "book",
                    "_type": "_doc",
                    "_id": "SM2e7IIBO-2eYCM19Rdj",
                    "_score": 1.3371259,
                    "_source": {
                        "bookId": "2000014486",
                        "title": "李小龙(英文名:Bruce Lee,1940年11月27日-1973年7月20日),本名李振藩,出生于美国加利福尼亚州旧金山",
                        "alltext": "1962年李小龙开办“振藩国术馆”,1967年自创截拳道,1973年7月20日,李小龙在香港逝世,年仅32岁。1979年美国洛杉矶市政府将补拍版《死亡游戏》的开映日6月8日定为“李小龙日”(7月8日为错误翻译)。1993年美国发行李小龙逝世20周年纪念钞票,好莱坞星光大道铺上李小龙纪念星徽;同年,获香港电影金像奖大会颁发“终身成就奖”。1998年11月,获中国武术协会颁发“武术电影巨星奖”。"
                    }
                },
                {
                    "_index": "book",
                    "_type": "_doc",
                    "_id": "Sc2f7IIBO-2eYCM1bxfY",
                    "_score": 1.3117697,
                    "_source": {
                        "bookId": "2000014486",
                        "title": "周星驰(Stephen Chow),1962年6月22日出生于香港,祖籍浙江省宁波市,中国香港影视男演员、导演、编剧、制作人、商人,毕业于无线电视艺员训练班",
                        "alltext": "1980年成为丽的电视台的特约演员,从而进入演艺圈。1981年出演个人首部电视剧《IQ成熟时》。1988年将演艺事业的重心转向大银幕,并于同年出演电影处女作《捕风汉子》。1990年凭借喜剧片《一本漫画闯天涯》确立其无厘头的表演风格;同年,因其主演的喜剧动作片《赌圣》打破香港地区票房纪录而获得关注。1991年主演喜剧片《逃学威龙》,并再次打破香港地区票房纪录 [1]  。1995年凭借喜剧爱情片《大话西游》奠定其在华语影坛的地位。1999年自导自演的喜剧片《喜剧之王》获得香港电影年度票房冠军 [2]  。"
                    }
                }
            ]
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    goland里面api的接口演示
    在这里插入图片描述

    boost控制权重

    boost控制权重实现搜索结果排名
    参考

  • 相关阅读:
    【LeetCode-中等题】208. 实现 Trie (前缀树)
    建设一个互联网医院系统要花多少钱?
    .Net单元测试xUnit和集成测试指南(1)
    FLIP动画做拖拽排序效果
    Springboot----实现邮箱验证码登录(代码部分)
    人大女王金融硕士项目——无论在任何时候,都不必怀疑努力的意义
    Excel 导入实例
    软件系统开发|公众号都有哪些展现形式?
    怎么提高shopee店铺的销量-扬帆际海
    从0开始学c语言-33-动态内存管理
  • 原文地址:https://blog.csdn.net/weixin_42765975/article/details/126429965