ElasticSearch 7 搜索(8)布尔查询

type description
must 必须出现在匹配文档中
filter 必须出现在文档中,但是不打分
must_not 不能出现在文档中
should 应该出现在文档中

must查询 GET/POST请求

  • 请求
1
localhost:9200/nba/_search
  • 请求体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
// 语义: 查询名字叫做james的球员
"query": {
"bool": { // 布尔查询
"must":[ //匹配查询方式
{
"match": { // 全文搜索
"displayNameEn": "james"
}
}
]
}
},
"from": 0,
"size": 3
}
  • 响应
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
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 4.699642,
"hits" : [
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "214",
"_score" : 4.699642,
"_source" : {
"countryEn" : "United States",
"teamName" : "火箭",
"birthDay" : 620107200000,
"country" : "美国",
"teamCityEn" : "Houston",
"code" : "james_harden",
"displayAffiliation" : "Arizona State/United States",
"displayName" : "詹姆斯 哈登",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "99.8 公斤",
"teamCity" : "休斯顿",
"playYear" : 10,
"jerseyNo" : "13",
"teamNameEn" : "Rockets",
"draft" : 2009,
"displayNameEn" : "James Harden",
"heightValue" : 1.96,
"birthDayStr" : "1989-08-26",
"position" : "后卫",
"age" : 30,
"playerId" : "201935"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "266",
"_score" : 4.699642,
"_source" : {
"countryEn" : "United States",
"teamName" : "国王",
"birthDay" : 854082000000,
"country" : "美国",
"teamCityEn" : "Sacramento",
"code" : "justin_james",
"displayAffiliation" : "United States",
"displayName" : "贾斯汀 詹姆斯",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "86.2 公斤",
"teamCity" : "萨克拉门托",
"playYear" : 0,
"jerseyNo" : "",
"teamNameEn" : "Kings",
"draft" : 2019,
"displayNameEn" : "Justin James",
"heightValue" : 2.01,
"birthDayStr" : "1997-01-24",
"position" : "后卫-前锋",
"age" : 22,
"playerId" : "1629713"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "267",
"_score" : 4.699642,
"_source" : {
"countryEn" : "United States",
"teamName" : "湖人",
"birthDay" : 473230800000,
"country" : "美国",
"teamCityEn" : "Los Angeles",
"code" : "lebron_james",
"displayAffiliation" : "No College/United States",
"displayName" : "勒布朗 詹姆斯",
"schoolType" : "High School",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "113.4 公斤",
"teamCity" : "洛杉矶",
"playYear" : 16,
"jerseyNo" : "23",
"teamNameEn" : "Lakers",
"draft" : 2003,
"displayNameEn" : "LeBron James",
"heightValue" : 2.03,
"birthDayStr" : "1984-12-30",
"position" : "前锋",
"age" : 35,
"playerId" : "2544"
}
}
]
}
}

filter查询 GET/POST请求

  • 请求
1
localhost:9200/nba/_search
  • 请求体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
// 语义: 查询名字叫做james的球员 不包含打分
"query": {
"bool": {
"filter":[
{
"match": {
"displayNameEn": "james"
}
}
]
}
},
"from": 0,
"size": 3
}
  • 响应
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
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 0.0,
"hits" : [
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "158",
"_score" : 0.0, // 不包含打分
"_source" : {
"countryEn" : "United States",
"teamName" : "76人",
"birthDay" : 646804800000,
"country" : "美国",
"teamCityEn" : "Philadelphia",
"code" : "james_ennis iii",
"displayAffiliation" : "Cal State-Long Beach/United States",
"displayName" : "詹姆斯 恩尼斯三世",
"schoolType" : "College",
"teamConference" : "东部",
"teamConferenceEn" : "Eastern",
"weight" : "95.3 公斤",
"teamCity" : "费城",
"playYear" : 5,
"jerseyNo" : "11",
"teamNameEn" : "76ers",
"draft" : 2013,
"displayNameEn" : "James Ennis III",
"heightValue" : 2.01,
"birthDayStr" : "1990-07-01",
"position" : "前锋",
"age" : 29,
"playerId" : "203516"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "214",
"_score" : 0.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "火箭",
"birthDay" : 620107200000,
"country" : "美国",
"teamCityEn" : "Houston",
"code" : "james_harden",
"displayAffiliation" : "Arizona State/United States",
"displayName" : "詹姆斯 哈登",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "99.8 公斤",
"teamCity" : "休斯顿",
"playYear" : 10,
"jerseyNo" : "13",
"teamNameEn" : "Rockets",
"draft" : 2009,
"displayNameEn" : "James Harden",
"heightValue" : 1.96,
"birthDayStr" : "1989-08-26",
"position" : "后卫",
"age" : 30,
"playerId" : "201935"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "266",
"_score" : 0.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "国王",
"birthDay" : 854082000000,
"country" : "美国",
"teamCityEn" : "Sacramento",
"code" : "justin_james",
"displayAffiliation" : "United States",
"displayName" : "贾斯汀 詹姆斯",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "86.2 公斤",
"teamCity" : "萨克拉门托",
"playYear" : 0,
"jerseyNo" : "",
"teamNameEn" : "Kings",
"draft" : 2019,
"displayNameEn" : "Justin James",
"heightValue" : 2.01,
"birthDayStr" : "1997-01-24",
"position" : "后卫-前锋",
"age" : 22,
"playerId" : "1629713"
}
}
]
}
}

must_not查询 GET/POST请求

  • 请求
1
localhost:9200/nba/_search
  • 请求体
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
{
// 语义: 查询名字叫做james的球员,一定不在东部的
"query": {
"bool": {
"filter":[
{
"match": {
"displayNameEn": "james"
}
}
],
"must_not": [
{
"term": {
"teamConferenceEn": {
"value": "eastern"
}
}
}
]
}
},
"from": 0,
"size": 3
}
  • 响应
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
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 0.0,
"hits" : [
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "158",
"_score" : 0.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "76人",
"birthDay" : 646804800000,
"country" : "美国",
"teamCityEn" : "Philadelphia",
"code" : "james_ennis iii",
"displayAffiliation" : "Cal State-Long Beach/United States",
"displayName" : "詹姆斯 恩尼斯三世",
"schoolType" : "College",
"teamConference" : "东部",
"teamConferenceEn" : "Eastern",
"weight" : "95.3 公斤",
"teamCity" : "费城",
"playYear" : 5,
"jerseyNo" : "11",
"teamNameEn" : "76ers",
"draft" : 2013,
"displayNameEn" : "James Ennis III",
"heightValue" : 2.01,
"birthDayStr" : "1990-07-01",
"position" : "前锋",
"age" : 29,
"playerId" : "203516"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "214",
"_score" : 0.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "火箭",
"birthDay" : 620107200000,
"country" : "美国",
"teamCityEn" : "Houston",
"code" : "james_harden",
"displayAffiliation" : "Arizona State/United States",
"displayName" : "詹姆斯 哈登",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "99.8 公斤",
"teamCity" : "休斯顿",
"playYear" : 10,
"jerseyNo" : "13",
"teamNameEn" : "Rockets",
"draft" : 2009,
"displayNameEn" : "James Harden",
"heightValue" : 1.96,
"birthDayStr" : "1989-08-26",
"position" : "后卫",
"age" : 30,
"playerId" : "201935"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "266",
"_score" : 0.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "国王",
"birthDay" : 854082000000,
"country" : "美国",
"teamCityEn" : "Sacramento",
"code" : "justin_james",
"displayAffiliation" : "United States",
"displayName" : "贾斯汀 詹姆斯",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "86.2 公斤",
"teamCity" : "萨克拉门托",
"playYear" : 0,
"jerseyNo" : "",
"teamNameEn" : "Kings",
"draft" : 2019,
"displayNameEn" : "Justin James",
"heightValue" : 2.01,
"birthDayStr" : "1997-01-24",
"position" : "后卫-前锋",
"age" : 22,
"playerId" : "1629713"
}
}
]
}
}

should 第一种查询 GET/POST请求

  • 请求
1
localhost:9200/nba/_search
  • 请求体
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
{
// 语义: 查找名字叫做james的打球时间应该在11到20年的西部球员
// should应该 但是不是必须
"query": {
"bool": {
"filter":[
{
"match": {
"displayNameEn": "james"
}
}
],
"must_not": [
{
"term": {
"teamConferenceEn": {
"value": "eastern"
}
}
}
],
"should": [
{
"range": {
"playYear": {
"gte": 11,
"lte": 20
}
}
}
]
}
},
"from": 0,
"size": 3
}
  • 响应
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
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "267",
"_score" : 1.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "湖人",
"birthDay" : 473230800000,
"country" : "美国",
"teamCityEn" : "Los Angeles",
"code" : "lebron_james",
"displayAffiliation" : "No College/United States",
"displayName" : "勒布朗 詹姆斯",
"schoolType" : "High School",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "113.4 公斤",
"teamCity" : "洛杉矶",
"playYear" : 16,
"jerseyNo" : "23",
"teamNameEn" : "Lakers",
"draft" : 2003,
"displayNameEn" : "LeBron James",
"heightValue" : 2.03,
"birthDayStr" : "1984-12-30",
"position" : "前锋",
"age" : 35,
"playerId" : "2544"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "158",
"_score" : 0.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "76人",
"birthDay" : 646804800000,
"country" : "美国",
"teamCityEn" : "Philadelphia",
"code" : "james_ennis iii",
"displayAffiliation" : "Cal State-Long Beach/United States",
"displayName" : "詹姆斯 恩尼斯三世",
"schoolType" : "College",
"teamConference" : "东部",
"teamConferenceEn" : "Eastern",
"weight" : "95.3 公斤",
"teamCity" : "费城",
"playYear" : 5,
"jerseyNo" : "11",
"teamNameEn" : "76ers",
"draft" : 2013,
"displayNameEn" : "James Ennis III",
"heightValue" : 2.01,
"birthDayStr" : "1990-07-01",
"position" : "前锋",
"age" : 29,
"playerId" : "203516"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "214",
"_score" : 0.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "火箭",
"birthDay" : 620107200000,
"country" : "美国",
"teamCityEn" : "Houston",
"code" : "james_harden",
"displayAffiliation" : "Arizona State/United States",
"displayName" : "詹姆斯 哈登",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "99.8 公斤",
"teamCity" : "休斯顿",
"playYear" : 10,
"jerseyNo" : "13",
"teamNameEn" : "Rockets",
"draft" : 2009,
"displayNameEn" : "James Harden",
"heightValue" : 1.96,
"birthDayStr" : "1989-08-26",
"position" : "后卫",
"age" : 30,
"playerId" : "201935"
}
}
]
}
}

should 第二种查询 GET/POST请求

  • 请求
1
localhost:9200/nba/_search
  • 请求体
    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
    {
    // 语义: 查找名字叫做james的打球时间应该在11到20年的西部球员
    // minimum_should_match = 1 最小匹配精度,至少有一个条件满足
    "query": {
    "bool": {
    "filter":[
    {
    "match": {
    "displayNameEn": "james"
    }
    }
    ],
    "must_not": [
    {
    "term": {
    "teamConferenceEn": {
    "value": "eastern"
    }
    }
    }
    ],
    "should": [
    {
    "range": {
    "playYear": {
    "gte": 11,
    "lte": 20
    }
    }
    }
    ]
    , "minimum_should_match": 1
    }
    },
    "from": 0,
    "size": 3
    }
  • 响应
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
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "267",
"_score" : 1.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "湖人",
"birthDay" : 473230800000,
"country" : "美国",
"teamCityEn" : "Los Angeles",
"code" : "lebron_james",
"displayAffiliation" : "No College/United States",
"displayName" : "勒布朗 詹姆斯",
"schoolType" : "High School",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "113.4 公斤",
"teamCity" : "洛杉矶",
"playYear" : 16,
"jerseyNo" : "23",
"teamNameEn" : "Lakers",
"draft" : 2003,
"displayNameEn" : "LeBron James",
"heightValue" : 2.03,
"birthDayStr" : "1984-12-30",
"position" : "前锋",
"age" : 35,
"playerId" : "2544"
}
}
]
}
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2021 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :