1、什么是query DSL
GET /_search
{
"query": {
"match_all": {}
}
}
1
2
3
4
5
6
query 中有哪些query_name
全部查询出来 match all
GET /web/info/_search --查询所有的文档
{
"query": {
"match_all": {}
}
}
1
2
3
4
5
6
匹配查询match
GET /web/info/_search --查询出content中包含 second的文档
{
"query": {
"match": {
"content": "second"
}
}
}
1
2
3
4
5
6
7
8
查询出多个filed中包含某某的文档multi_match
GET /web/info/_search--查询出content和author_id中包含 xlucas的文档
{
"query": {
"multi_match": {
"query": "xlucas",
"fields": ["content","author_id"]
}
}
}
1
2
3
4
5
6
7
8
9
范围查询 range
GET /web/info/_search --查询post_date大于等于2018-11-02的文档
{
"query": {
"range": {
"post_date": {
"gte": "2018-11-02"
}
}
}
}
1
2
3
4
5
6
7
8
9
10
精准查询term
GET /web/info/_search--查询post_date全匹配上2018-11-02的文档
{
"query": {
"term": {
"post_date": {
"value": "2018-11-02"
}
}
}
}
1
2
3
4
5
6
7
8
9
10
精准查询term3
GET /web/info/_search --查询post_date匹配上2018-11-02或者2018-11-03的文档
{
"query": {
"terms": {
"post_date": [
"2018-11-02",
"2018-11-03"
]
}
}
}
1
2
3
4
5
6
7
8
9
10
11
组合查询 bool
bool里面可以带上这些must,must_not,should,filter
每个子查询都会计算一个document针对它的相关度分数,然后bool综合所有分数,合并为一个分数,filter是不会计算分数的
案例:查询author_id必须包含xlucas的,content中不包含third的,title中可以有info的,并且post_date要大于2018-11-02且小于2018-11-03的文档
GET /web/info/_search
{
"query": {
"bool": {
"must": [
{"match": {
"author_id": "xlucas"
}}
],
"must_not": [
{"match": {
"content": "third"
}}
],
"should": [
{"match": {
"title": "info"
}}
],
"filter": {
"bool": {
"must":[{
"range":{
"post_date":{
"gte":"2018-11-02"
}
}},{
"range":{
"post_date":{
"lte":"2018-11-03"
}
}}
]
}
}
}
}
}
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
单纯查询过滤出来的数据
GET /web/info/_search
{
"query": {
"constant_score": {
"filter": {
"range": {
"post_date": {
"gte": "2018-11-02"
}
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ES中的查询代码检查器
GET /web/info/_validate/query?explain --故意将author_id写错成author_i
{
"query": {
"match": {
"author_i": "xlucas"
}
}
}
1
2
3
4
5
6
7
8
报错信息
{
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"valid": true,
"explanations": [
{
"index": "web",
"valid": true,
"explanation": """+MatchNoDocsQuery("unmapped field [author_i]") #*:*"""
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ES中排序
默认情况下,是按照_score降序排序的,如果_score是一样的,那么想自定义排序方法。
如下:按照post_date的降序排序
GET /web/info/_search
{
"query": {
"match": {
"author_id": "xlucas"
}
},
"sort": [
{
"post_date": {
"order": "desc"
}
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
filter与query对比
filter,仅仅只是按照搜索条件过滤出需要的数据而已,不计算任何相关度分数,对相关度没有任何影响
query,会去计算每个document相对于搜索条件的相关度,并按照相关度进行排序
一般来说,如果你是在进行搜索,需要将最匹配搜索条件的数据先返回,那么用query;如果你只是要根据一些条件筛选出一部分数据,不关注其排序,那么用filter除非是你的这些搜索条件,你希望越符合这些搜索条件的document越排在前面返回,那么这些搜索条件要放在query中;如果你不希望一些搜索条件来影响你的document排序,那么就放在filter中即可
filter与query性能
filter,不需要计算相关度分数,不需要按照相关度分数进行排序,同时还有内置的自动cache最常使用filter的数据
query,相反,要计算相关度分数,按照分数进行排序,而且无法cache结果