Ex_treme's blog.

elasticsearch查询

2018/05/01 Share

elasticsearch的简单查询

查询

elasticsearch是功能非常强大的搜索引擎,使用它的目的就是为了快速的查询需要的数据。
查询分类:
基本查询:使用elasticsearch内置查询条件进行查询
组合查询:把多个查询组合在一起进行复合查询
过滤:查询同时,通过filter条件在不影响打分的情况下筛选数据

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

#match查询
GET qa/_search
{
"query": {
"match": {
"question": "机器"
}
}
}

#terms查询
GET qa/_search
{
"query": {
"term": {
"topic": {
"value": "会议"
}
}
}
}

#terms查询
GET qa/_search
{
"query": {
"terms": {
"question": [
"机器",
"会议"
]
}
}
}

# 控制查询返回数量
GET qa/_search
{
"query": {
"match": {
"question": "什么"
},
"from":1,
"size":2
}
}

# match_all 查询
GET qa/_search
{
"query": {
"match_all": {}
}
}

# match_parse 查询
# 短语查询
GET qa/_search
{
"query": {
"match_phrase": {
"question":{
"query": "什么学习",
"slop":1
}
}
}
}

#multi_match查询
#可以指定多个字段
GET qa/_search
{
"query": {
"multi_match": {
"query": "介绍",
"fields": ["question","answer"]
}
}
}
#指定返回字段
GET qa/_search
{
"stored_fields": ["question","answer"],
"query": {
"match": {
"question": "调试"
}
}
}

#通过sort把结果排序
GET qa/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"url": {
"order": "desc"
}
}
]
}

#范围查询
#range查询
GET qa/_search
{
"query": {
"range": {
"FIELD": {
"gte": 10,
"lte": 20
}
}
}
}

#wildcard查询
GET qa/_search
{
"query": {
"wildcard": {
"question": {
"value": "机*器"
}
}
}
}

bool查询

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#bool查询
#老版本的filtered已经被bool替换
#用bool must should must_not来完成,格式如下:
#bool: {
#"filter":[],
# "must":[],
# "should":[],
# "must_not":[],
#}

#建立测试数据
POST qa/test/_bulk
{"index":{"_id":1}}
{"salary":10,"title":"Python"}
{"index":{"_id":2}}
{"salary":20,"title":"Scrapy"}
{"index":{"_id":3}}
{"salary":30,"title":"Django"}
{"index":{"_id":4}}
{"salary":30,"title":"Elasticsearch"}

DELETE qa/test
#简单过滤查询

#最简单的filter查询
#select * from test where salary = 20
#filtered 薪资为20k的工作
GET qa/test/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"term": {
"salary": 20
}
}
}
}
}

#指定多个值
GET qa/test/_search
{
"query": {
"bool": {
"must":{
"match_all":{}
},
"filter": {
"terms": {
"salary": [
10,
20
]
}
}
}
}
}

#select * from test where title="Python"
GET qa/test/_search
{
"query": {
"bool": {
"must":{
"match_all":{}
},
"filter": {
"term": {
"title": "python"
}
}
}
}
}

#查看分析器解析结果
GET _analyze
{
"analyzer": "ik_max_word",
"text": "Python网络"
}

#bool过滤查询,可以做组合过滤查询
#select * from test where(salary = 20 OR title = Pyhon) AND (salary != 30)
#查询薪资等于20k或者工作为python的工作,排除价格为30K的
GET qa/test/_search
{
"query": {
"bool": {
"should": [
{"term": {"salary":20}},
{"term":{"title":"Python"}}
],
"must_not": {
"term":{"salary":30}
}
}
}
}

#嵌套查询
#select * from test where title="Python" or (title="elasticsearch" AND salary=30)

GET qa/test/_search
{
"query": {
"bool": {
"should": [
{"term":{"title":"Python"}},
{"bool": {
"must": [
{"term":{"title":"elasticsearch"}},
{"term":{"salary":30}}
]}
}
]
}
}
}

#过滤空和非空
#建立测试数据
POST qa/test/_bulk
{"index":{"_id":"1"}}
{"tags":["search"]}
{"index":{"_id":"2"}}
{"tags":["search","python"]}
{"index":{"_id":"3"}}
{"tags":["some data"]}
{"index":{"_id":"4"}}
{"tags":null}
{"index":{"_id":"5"}}
{"tags":["search",null]}

#处理null空值的方法
#selact tags from test where tags is not NULL
GET qa/test/_search
{
"query": {
"bool": {
"filter": {
"exists": {
"field": "tags"
}
}
}
}
}
CATALOG
  1. 1. elasticsearch的简单查询
    1. 1.1. 查询
    2. 1.2. bool查询