zoukankan      html  css  js  c++  java
  • Elasticsearch学习(二)————搜索

    Elasticsearch
    1.query string search
    1.1.搜索全部
    // 1.
    GET http://ip:9200/test/test/_search
    结果:
    {
    "took": 86, # 耗费的时间:ms
    "timed_out": false, # 是否超时
    "_shards": { # 数据存储在5个主分片上
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
    },
    "hits": { # 匹配结果
    "total": 3, # 查询到三个document
    "max_score": 1, # 相关度的匹配分数:分数越高越相关
    "hits": [
    {
    "_index": "test", # 索引 index
    "_type": "test", # type
    "_id": "2", # id:唯一
    "_score": 1, # 相关度的匹配分数:分数越高越相关
    "_source": { # 存储的json数据
    "first_name": "小翠", # field
    "last_name": "cuicui",
    "age": 18,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "1",
    "_score": 1,
    "_source": {
    "first_name": "John",
    "last_name": "Smith",
    "age": 25,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "AWrVpGsO0WDJvaOeQOjd",
    "_score": 1,
    "_source": {
    "first_name": "小翠",
    "last_name": "cui",
    "age": 18,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    }
    }
    ]
    }
    }
    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
    1.2.以about字段中含有climbing字符查询并根据age字段降序排列 可以多个排序,用逗号分隔:
    // 2.
    GET http://ip:9200/test/test/_search?q=about:climbing&sort=age:desc,price:desc
    GET http://ip:9200/test/test/_search?q=about:climbing&sort=age:desc
    {
    "took": 10,
    "timed_out": false,
    "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
    },
    "hits": {
    "total": 3,
    "max_score": null,
    "hits": [
    {
    "_index": "test",
    "_type": "test",
    "_id": "1",
    "_score": null,
    "_source": {
    "first_name": "John",
    "last_name": "Smith",
    "age": 25,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    },
    "sort": [ #排序字段的值
    25
    ]
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "2",
    "_score": null,
    "_source": {
    "first_name": "小翠",
    "last_name": "cuicui",
    "age": 18,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    },
    "sort": [
    18
    ]
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "AWrVpGsO0WDJvaOeQOjd",
    "_score": null,
    "_source": {
    "first_name": "小翠",
    "last_name": "cui",
    "age": 18,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    },
    "sort": [
    18
    ]
    }
    ]
    }
    }
    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
    2.query DSL
    2.1.搜索全部
    // 1.
    POST http://ip:9200/test/test/_search
    语法:
    {
    "query":{
    "match_all":{}
    }
    }
    结果:
    {
    "took": 5,
    "timed_out": false,
    "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
    },
    "hits": {
    "total": 5,
    "max_score": 1,
    "hits": [
    {
    "_index": "test",
    "_type": "test",
    "_id": "2",
    "_score": 1,
    "_source": {
    "first_name": "小翠",
    "last_name": "cuicui",
    "age": 18,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "4",
    "_score": 1,
    "_source": {
    "first_name": "小翠",
    "last_name": "cui",
    "age": 18,
    "price": 15000,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "1",
    "_score": 1,
    "_source": {
    "first_name": "John",
    "last_name": "Smith",
    "age": 25,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "AWrVpGsO0WDJvaOeQOjd",
    "_score": 1,
    "_source": {
    "first_name": "小翠",
    "last_name": "cui",
    "age": 18,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "3",
    "_score": 1,
    "_source": {
    "first_name": "小雪",
    "last_name": "xiaoxue",
    "age": 20,
    "about": "climbing",
    "interests": [
    "dancing",
    "music"
    ]
    }
    }
    ]
    }
    }
    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
    2.2.以about字段中含有climbing字符查询并根据age字段降序排列 可以多个排序,用逗号分隔
    // 1.
    POST http://ip:9200/test/test/_search
    语法:
    {
    "query":{
    "match":{
    "about":"climbing"
    }
    },
    "sort":[
    {
    "age":"desc"
    }
    ]
    }

    结果:
    {
    "took": 115,
    "timed_out": false,
    "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
    },
    "hits": {
    "total": 5,
    "max_score": null,
    "hits": [
    {
    "_index": "test",
    "_type": "test",
    "_id": "1",
    "_score": null,
    "_source": {
    "first_name": "John",
    "last_name": "Smith",
    "age": 25,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    },
    "sort": [
    25
    ]
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "3",
    "_score": null,
    "_source": {
    "first_name": "小雪",
    "last_name": "xiaoxue",
    "age": 20,
    "about": "climbing",
    "interests": [
    "dancing",
    "music"
    ]
    },
    "sort": [
    20
    ]
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "2",
    "_score": null,
    "_source": {
    "first_name": "小翠",
    "last_name": "cuicui",
    "age": 18,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    },
    "sort": [
    18
    ]
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "4",
    "_score": null,
    "_source": {
    "first_name": "小翠",
    "last_name": "cui",
    "age": 18,
    "price": 15000,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    },
    "sort": [
    18
    ]
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "AWrVpGsO0WDJvaOeQOjd",
    "_score": null,
    "_source": {
    "first_name": "小翠",
    "last_name": "cui",
    "age": 18,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    },
    "sort": [
    18
    ]
    }
    ]
    }
    }
    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
    2.3. 分页数据
    // 1.
    POST http://ip:9200/test/test/_search
    语法:
    {
    "query":{
    "match_all":{} # 查询所有
    },
    "from":0, # 从第几条数据开始 0:第一条
    "size":1 # 展示几条数据
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    2.4.只展示指定的filed
    // 1.
    POST http://ip:9200/test/test/_search
    语法:
    {
    "query":{
    "match_all":{}
    },
    "_source":[
    "first_name",
    "age"
    ]
    }

    结果:
    {
    "took": 8,
    "timed_out": false,
    "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
    },
    "hits": {
    "total": 5,
    "max_score": 1,
    "hits": [
    {
    "_index": "test",
    "_type": "test",
    "_id": "3",
    "_score": 1,
    "_source": {
    "first_name": "小雪",
    "age": 20
    }
    }
    ]
    }
    }
    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
    3.query filter
    3.1.多个查询条件:about字段必须包含"climbing";age大于20岁
    // 1.
    POST http://ip:9200/test/test/_search
    语法:
    {
    "query":{
    "bool":{
    "must":{
    "match":{
    "about":"climbing"
    }
    },
    "filter":{
    "range":{
    "age":{
    "gt":20
    }
    }
    }
    }
    }
    }

    结果
    {
    "took": 7,
    "timed_out": false,
    "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
    },
    "hits": {
    "total": 1,
    "max_score": 0.26742277,
    "hits": [
    {
    "_index": "test",
    "_type": "test",
    "_id": "1",
    "_score": 0.26742277,
    "_source": {
    "first_name": "John",
    "last_name": "Smith",
    "age": 25,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    }
    }
    ]
    }
    }
    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
    3.2. 多个查询条件:must、 should、 must_not
    POST http://47.99.74.228:9200/test/test/_search
    {
    "query":{
    "bool":{
    "must":{ # 必须匹配
    "match":{
    "first_name":"小翠"
    }
    },
    "should":{ # 可以匹配,也可以不匹配
    "match":{
    "last_name": "xue"
    }
    },
    "must_not":{ # 必须不匹配
    "match":{
    "last_name": "cui"
    }
    }
    }
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    4.full-test search
    4.1.全文检索
    // 1.
    POST http://ip:9200/test/test/_search
    语法:
    {
    "query":{
    "match":{
    "about":"go climbing"
    }
    }
    }
    分析:es将about这个filed拆解成每个词(term),建立倒排索引,每个term对应相应的document_id
    结果:
    {
    "took": 8,
    "timed_out": false,
    "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
    },
    "hits": {
    "total": 7,
    "max_score": 0.7447149,
    "hits": [
    {
    "_index": "test",
    "_type": "test",
    "_id": "2",
    "_score": 0.7447149,
    "_source": {
    "first_name": "小翠",
    "last_name": "cuicui",
    "age": 18,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "4",
    "_score": 0.7447149,
    "_source": {
    "first_name": "小翠",
    "last_name": "cui",
    "age": 18,
    "price": 15000,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "1",
    "_score": 0.61562645,
    "_source": {
    "first_name": "John",
    "last_name": "Smith",
    "age": 25,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "AWrVpGsO0WDJvaOeQOjd",
    "_score": 0.61562645,
    "_source": {
    "first_name": "小翠",
    "last_name": "cui",
    "age": 18,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "5",
    "_score": 0.2876821,
    "_source": {
    "first_name": "花花",
    "last_name": "huahau",
    "age": 16,
    "price": 20000,
    "about": "climbing",
    "interests": [
    "sports",
    "music"
    ]
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "7",
    "_score": 0.25759193,
    "_source": {
    "about": "go"
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "3",
    "_score": 0.25759193,
    "_source": {
    "first_name": "小雪",
    "last_name": "xiaoxue",
    "age": 20,
    "about": "climbing",
    "interests": [
    "dancing",
    "music"
    ]
    }
    }
    ]
    }
    }
    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
    4.2.短语搜索:匹配短语
    // 1.
    POST http://ip:9200/test/test/_search
    语法:
    {
    "query":{
    "match_phrase":{
    "about":"rock climbing"
    }
    }
    }

    结果:
    {
    "took": 20,
    "timed_out": false,
    "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
    },
    "hits": {
    "total": 4,
    "max_score": 0.9748371,
    "hits": [
    {
    "_index": "test",
    "_type": "test",
    "_id": "1",
    "_score": 0.9748371,
    "_source": {
    "first_name": "John",
    "last_name": "Smith",
    "age": 25,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "2",
    "_score": 0.7447149,
    "_source": {
    "first_name": "小翠",
    "last_name": "cuicui",
    "age": 18,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "4",
    "_score": 0.7447149,
    "_source": {
    "first_name": "小翠",
    "last_name": "cui",
    "age": 18,
    "price": 15000,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "AWrVpGsO0WDJvaOeQOjd",
    "_score": 0.6156264,
    "_source": {
    "first_name": "小翠",
    "last_name": "cui",
    "age": 18,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    }
    }
    ]
    }
    }
    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
    5.highlight search
    5.1.关键字高亮
    // 1.
    语法:
    {
    "query":{
    "match":{
    "about":"climbing" # 关键字
    }
    },
    "highlight":{
    "fields":{
    "about":{http://www.my516.com} # 字段
    }
    }
    }

    结果:
    {
    "took": 7,
    "timed_out": false,
    "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
    },
    "hits": {
    "total": 6,
    "max_score": 0.48741856,
    "hits": [
    {
    "_index": "test",
    "_type": "test",
    "_id": "1",
    "_score": 0.48741856,
    "_source": {
    "first_name": "John",
    "last_name": "Smith",
    "age": 25,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    },
    "highlight": {
    "about": [
    "I love to go rock <em>climbing</em>" # <em> 标签html中高亮显示
    ]
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "2",
    "_score": 0.37235746,
    "_source": {
    "first_name": "小翠",
    "last_name": "cuicui",
    "age": 18,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    },
    "highlight": {
    "about": [
    "I love to go rock <em>climbing</em>"
    ]
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "4",
    "_score": 0.37235746,
    "_source": {
    "first_name": "小翠",
    "last_name": "cui",
    "age": 18,
    "price": 15000,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    },
    "highlight": {
    "about": [
    "I love to go rock <em>climbing</em>"
    ]
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "5",
    "_score": 0.2876821,
    "_source": {
    "first_name": "花花",
    "last_name": "huahau",
    "age": 16,
    "price": 20000,
    "about": "climbing",
    "interests": [
    "sports",
    "music"
    ]
    },
    "highlight": {
    "about": [
    "<em>climbing</em>"
    ]
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "3",
    "_score": 0.25759193,
    "_source": {
    "first_name": "小雪",
    "last_name": "xiaoxue",
    "age": 20,
    "about": "climbing",
    "interests": [
    "dancing",
    "music"
    ]
    },
    "highlight": {
    "about": [
    "<em>climbing</em>"
    ]
    }
    },
    {
    "_index": "test",
    "_type": "test",
    "_id": "AWrVpGsO0WDJvaOeQOjd",
    "_score": 0.12820786,
    "_source": {
    "first_name": "小翠",
    "last_name": "cui",
    "age": 18,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    },
    "highlight": {
    "about": [
    "I love to go rock <em>climbing</em>"
    ]
    }
    }
    ]
    }
    }
    --------------------- 

  • 相关阅读:
    axis2 WebService 请求参数xml格式
    钉钉扫码登录第三方,appSecret签名算法(附包名)
    win10 IE浏览器中,设置指定程序查看源文件,设置查看源默认程序
    myeclipse CI 2018.8.0和 myeclipse 10 禁止空格自动上屏,自动补全插件
    开发必备网站记录
    二叉树的前序中序后序遍历(简洁)
    贪心算法入门
    Java-二分查找与二叉树关系详解-2021-7-20
    Visual Studio Code快速创建模板(html等)
    java 面向对象1之继承
  • 原文地址:https://www.cnblogs.com/hyhy904/p/10992425.html
Copyright © 2011-2022 走看看