status:(active OR pending) title:(full text search)
创建索引,生成测试文档
PUT my_index_search
{"settings":{"number_of_shards":"5",
"number_of_replicas":"0"}}
POST my_index_search/doc/_bulk
{"index":{"_id":"1"}}{"username":"alfred way","job":"java engineer","age": 18,"birth":"1990-01-02","isMarried":false}{"index":{"_id":"2"}}{"username":"alfred","job":"java senior and java specialist","age": 28,"birth":"1980-05-07","isMarried":true}{"index":{"_id":"3"}}{"username":"lee","job":"java and ruby engineer","age": 22,"birth":"1985-08-07","isMarried":false}{"index":{"_id":"4"}}{"username":"alfred junior way","job":"ruby engineer","age": 23,"birth":"1989-08-02","isMarried":false}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 查询所有字段中有alfred的文档
GET my_index_search/_search?q=alfred
# 设置profile可以看具体的查询语句
GET my_index_search/_search?q=alfred
{"profile":true}
GET my_index_search/_search?q=username:alfred
GET my_index_search/_search?q=username:alfred
{"profile":true}# username:alfred和way是OR的关系
GET my_index_search/_search?q=username:alfred way
{"profile":true}# PhraseQuery词语的查询
GET my_index_search/_search?q=username:"alfred way"{"profile":true}# "description": "username:alfred username:way" 下面描述
GET my_index_search/_search?q=username:(alfred way){"profile":true}
GET my_index_search/_search?q=username:alfred AND way
{"profile":true}
GET my_index_search/_search?q=username:(alfred AND way){"profile":true}
GET my_index_search/_search?q=username:(alfred NOT way){"profile":true}
GET my_index_search/_search?q=username:(alfred +way){"profile":true}
GET my_index_search/_search?q=username:(alfred %2Bway){"profile":true}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
范围查询,支持数值和日期
区间写法,闭区间用[],开区间{}
age:[1 TO 10]意为 1<=age<=10
age:[1 TO 10}意为 1<=age<10
age:[1 TO]意为 age>=1
age:{* TO 10]意为age<=10
算术符号写法
age:>=1
age:(>=1 && <=10)或者 age:(+>=1 +<=10)
GET my_index_search/_search?q=username:alfred age:>20
GET my_index_search/_search?q=username:alfred AND age:>20
GET my_index_search/_search?q=birth:(>1980 AND <1990)
1
2
3
通配符查询
? 代表1个字符, * 代表0或多个字符
name:t?m
name:tom*
name:t*m
通配符匹配执行效率低,且占用较多内存,不建议使用
如无特殊需求,不要将?/ * 放在最前面
GET my_index_search/_search?q=username:alf*
1
正则表达式匹配
GET my_index_search/_search?q=username:/[a]?l.*/
1
模糊匹配fuzzy query
name:roam~1
匹配roam差1个character的词,比如foam、roams等
近似度查询proximity search
“fox quick”~5
以term为单位进行差异比较,比如“quick fox” “quick brown fox”都会被匹配
GET my_index_search/_search?q=username:alfed~1
GET my_index_search/_search?q=job:"java engineer"~2
GET my_index_search/_search
{"profile": true,
"query":{"match_phrase":{"job":{"query":"java engineer"}}}}
GET my_index_search/_search
{"profile": true,
"query":{"match_phrase":{"job":{"query":"engineer java"}}}}
GET my_index_search/_search
{"profile": true,
"query":{"match_phrase":{"job":{"query":"java engineer",
"slop": 1
}}}}
GET my_index_search/_search
{"profile": true,
"query":{"match_phrase":{"job":{"query":"java engineer",
"slop": 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
7 query-string-query
GET my_index_search/_search
{"query":{"query_string":{"default_field":"username",
"query":"alfred AND way"}}}
GET my_index_search/_search
{"profile": true,
"query":{"query_string":{"fields":["username",
"job"],
"query":"alfred OR (java AND ruby)"}}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
8 simple-query-string-query
类似Query String,但是会忽略错误的查询语法,并且仅支持部分查询语法
其常用的逻辑符号如下、不能使用AND、OR、NOT等关键词:
+ 代指AND
| 代指OR
- 代指NOT
# 必须包含away,可以包含alfred
GET my_index_search/_search
{"profile": true,
"query":{"simple_query_string":{"query":"alfred +way",
"fields":["username"]}}}
GET my_index_search/_search
{"profile": true,
"query":{"simple_query_string":{"query":"alfred +way AND java",
"fields":["username"]}}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
query_string和simple_query_string的对比
GET my_index_search/_search
{"profile": true,
"query":{"query_string":{"fields":["username"],
"query":"alfred OR (\"java AND ruby)"}}}
GET my_index_search/_search
{"profile": true,
"query":{"simple_query_string":{"query":"alfred +way AND \"java",
"fields":["username"]}}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
9 term/terms-query
term-query将查询语句作为整个单词进行查询,即不对查询语句做分词处理,如下所示:
terms-query一次传入多个单词进行查询,如下所示:
# term query
GET my_index_search/_search
{"profile": true,
"query":{"term":{"username":"alfred"}}}
GET my_index_search/_search
{"profile": true,
"query":{"term":{"username":"alfred way"}}}# terms query
GET my_index_search/_search
{"profile": true,
"query":{"terms":{"username":["alfred",
"way"]}}}
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
10 range-query
GET my_index_search/_search
{"query":{"range":{"age":{"gte": 10,
"lte": 20
}}}}
GET my_index_search/_search
{"query":{"range":{"birth":{"gte":"1980-01-01"}}}}
GET my_index_search/_search
{"query":{"range":{"birth":{"gte":"now-35y"}}}}