首先,我们先插入几条数据,让ES为我们自动建立一个索引1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23PUT /website/article/1
{
"post_date": "2017-01-01",
"title": "my first article",
"content": "this is my first article in this website",
"author_id": 11400
}
PUT /website/article/2
{
"post_date": "2017-01-02",
"title": "my second article",
"content": "this is my second article in this website",
"author_id": 11400
}
PUT /website/article/3
{
"post_date": "2017-01-03",
"title": "my third article",
"content": "this is my third article in this website",
"author_id": 11400
}
对这些数据进行几次搜索
_all 搜索 2017
1 | GET /website/article/_search?q=2017 |
返回值: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{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.28004453,
"hits": [
{
"_index": "website",
"_type": "article",
"_id": "2",
"_score": 0.28004453,
"_source": {
"post_date": "2017-01-02",
"title": "my second article",
"content": "this is my second article in this website",
"author_id": 11400
}
},
{
"_index": "website",
"_type": "article",
"_id": "1",
"_score": 0.28004453,
"_source": {
"post_date": "2017-01-01",
"title": "my first article",
"content": "this is my first article in this website",
"author_id": 11400
}
},
{
"_index": "website",
"_type": "article",
"_id": "3",
"_score": 0.28004453,
"_source": {
"post_date": "2017-01-03",
"title": "my third article",
"content": "this is my third article in this website",
"author_id": 11400
}
}
]
}
}
一共查询出来3条结果
指定 post_date 搜索2017
1 | GET /website/article/_search?q=post_date:2017 |
返回值: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{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "website",
"_type": "article",
"_id": "1",
"_score": 1,
"_source": {
"post_date": "2017-01-01",
"title": "my first article",
"content": "this is my first article in this website",
"author_id": 11400
}
}
]
}
}
一共查询出来1条结果
_all 搜索 2017-01-01
1 | GET /website/article/_search?q=2017-01-01 |
返回值: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{
"took": 9,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1.0566096,
"hits": [
{
"_index": "website",
"_type": "article",
"_id": "1",
"_score": 1.0566096,
"_source": {
"post_date": "2017-01-01",
"title": "my first article",
"content": "this is my first article in this website",
"author_id": 11400
}
},
{
"_index": "website",
"_type": "article",
"_id": "2",
"_score": 0.84013355,
"_source": {
"post_date": "2017-01-02",
"title": "my second article",
"content": "this is my second article in this website",
"author_id": 11400
}
},
{
"_index": "website",
"_type": "article",
"_id": "3",
"_score": 0.84013355,
"_source": {
"post_date": "2017-01-03",
"title": "my third article",
"content": "this is my third article in this website",
"author_id": 11400
}
}
]
}
}
一共查询出来3条结果
指定 post_date 搜索2017-01-01
1 | GET /website/article/_search?q=post_date:2017-01-01 |
返回值: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{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "website",
"_type": "article",
"_id": "1",
"_score": 1,
"_source": {
"post_date": "2017-01-01",
"title": "my first article",
"content": "this is my first article in this website",
"author_id": 11400
}
}
]
}
}
一共查询出来1条结果
总体结果:1
2
3
4GET /website/article/_search?q=2017 3条结果
GET /website/article/_search?q=2017-01-01 3条结果
GET /website/article/_search?q=post_date:2017-01-01 1条结果
GET /website/article/_search?q=post_date:2017 1条结果
mapping 概念
自动或手动为index中的type建立的一种数据结构相关的配置,简称为mapping
dynamic mapping自动为我们建立index,创建type,以及type对应的mapping,mapping中包含了每个field对应的数据类型,以及如何分词等设置
查询mapping
查询语法:1
GET /index/_mapping/type
比如查询我们上面例子中的mapping1
GET /website/_mapping/article
返回值: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{
"website": {
"mappings": {
"article": {
"properties": {
"author_id": {
"type": "long"
},
"content": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"post_date": {
"type": "date"
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
可以看到 里面包含了我们每个field的数据类型等信息.
搜索结果为什么不一致?
因为es自动建立mapping的时候,设置了各个filed的数据类型,不同的数据类型的分词 搜索等行为是不一样的,所以出现了_all 搜索和指定field搜索时的数据不一致的情况