elasticsearch 三种日期查询方式

mapping:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
"mappings": {
"xxx_profile": {
"dynamic": "false",
"_all": {
"enabled": true
},
"properties": {
"xxx_time": {
"type": "date",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
}

查询方式1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"query": {
"bool": {
"must": [
{
"range": {
"xxx_time": {
"gte": "2018-09-16 00:00:00",
"lt": "2018-09-17 00:00:00",
"format": "yyyy-MM-dd HH:mm:ss",
"time_zone": "+08:00"
}
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 10,
"sort": []
}

查询方式2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"query": {
"bool": {
"must": [
{
"range": {
"xxx_time.keyword": {
"gte": "2018-09-16",
"lt": "2018-09-17"
}
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 10,
"sort": []
}

查询方式3:

1
2
3
4
5
6
7
8
9
10
11

{
"query": {
"wildcard": {
"xxx_time.keyword": "*2018-09-16*"
}
},
"from": 0,
"size": 10,
"sort": []
}

对应 mongodb 查询方式:

方式1:

1
2
3
4
5
6
{
"xxx_time": {
$gte: "2018-09-16 00:00:00",
$lte: "2018-09-16 20:00:00"
}
}

方式2:

1
2
3
{
"xxx_time":/2018-09-16/
}