1.准备数据
PUT /website/article/1
{
"post_date":"2018-01-01",
"title":"my first article",
"content":"this is my first article in this website",
"author_id":11400
}
PUT /website/article/2
{
"post_date":"2018-01-02",
"title":"my second article",
"content":"this is my second article in this website",
"author_id":11400
}
PUT /website/article/3
{
"post_date":"2018-01-03",
"title":"my third article",
"content":"this is my third article in this website",
"author_id":11400
}
2.尝试各种搜索
GET /website/article/_search?q=2018
{
"took": 7,
"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": "2018-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": "2018-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": "2018-01-03",
"title": "my third article",
"content": "this is my third article in this website",
"author_id": 11400
}
}
]
}
}
GET /website/article/_search?q=2018-01-01
搜索出了3条数据
{
"took": 16,
"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": "2018-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": "2018-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": "2018-01-03",
"title": "my third article",
"content": "this is my third article in this website",
"author_id": 11400
}
}
]
}
}
GET /website/article/_search?q=post_date:2018-01-01
搜索结果1条
{
"took": 5,
"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": "2018-01-01",
"title": "my first article",
"content": "this is my first article in this website",
"author_id": 11400
}
}
]
}
}
GET /website/article/_search?q=post_date:2018
搜索结果1条
{
"took": 5,
"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": "2018-01-01",
"title": "my first article",
"content": "this is my first article in this website",
"author_id": 11400
}
}
]
}
}
3.为什么会出现上面的结果?认识mapping
自动或手动为index中的type建立一种数据结构和相关配置,简称mapping
dynamic mapping:自动为我们建立index,创建type,以及type对应的mapping,mapping中包含了每个field对应的数据类型,以及如何分词等设置。
当然,我们也可以手动在创建数据之前,先创建index和type,以及type对应的mapping
查看mapping的命令
格式:GET /index/_mapping/type
例子:
{
"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
}
}
}
}
}
}
}
}
那么搜索结果为什么不一致?因为es自动建立mapping的时候,设置了不同的field不同的data type。不同data type的分词、搜索等行为不一样的,所以出现了_all field和post_date field的搜索表现完全不一样。