1,multi-index和multi-type搜索模式
/_search:所有所有,所有type下的所有数据都搜索出来
/index1/_search:指定一个index,搜索其下所有的type的数据
/index1,index2/_search:同时搜索两个index下的数据
/*1,*2/_search: 按照通配符去匹配多个索引
/index1/type1/_search:搜索一个index下指定的唐渝鹏的数据
/index1/type1,type2/_search:可以搜索一个index下多个type的数据
/index1,index2/type1,type2/_search:搜索多个index下的多个type的数据
/_all/type1,type2/_search:可以代表搜索所有index下的指定type的数据
使用分页语法:
来自第一页,每页大小为3
GET /test_index/test_type?from=1&size=3
2.query string 基础语法
//(查询test_field包含test的字眼)
GET /test_index/test_type/_search?q=test_field:test
// 必须包含
GET /_all/type1/_search?q=+content:elasticsearch
//必须不包含
GET /_all/type1/_search?q=-content:elasticsearch
//直接可以搜索所有的field。任意一个field包含指定的关键字就可以搜索出来。
GET /test_index/test_type/_searcj?q=test
后面如果在搜索的时候,没有对某个field指定搜索,就默认搜索_all field,其中是包含了所有field的值的。
用一个例子讲解mapping
首先插入几条数据:
PUT /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/13
{
"post_date":"2017-01-03",
"title":"my third article",
"content":"this is my third article in this website",
"author_id":11400
}
尝试各种搜索:
GET 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条数据
当我们在插入数据的时候,synamic mapping ,自动为我们建立index,创建tyoe以及type对应的mapping,mapping中包含了每个field对应的数据类型,以及如何分词等设置,我们当然,后面会讲解,也可以手动在创建数据之前,先创建index和type,体积type对应的mapping
查看mapping
GET website/_mapping/article
结果:
{
"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的搜索表现完全不一样。
精准匹配与全文检索
1.exact value
2017-01-01.exact value,搜索的时候,必须输入2017-01-01,才能搜索出来,如果你输入一个01,是搜不出来的
2.full text
- 缩写vs,全程:cn,vs. china
- 格式转换:like liked likes
- 大小写:Tom vs tom
- 同义词:like vs love
2017-01-01,2017 01 01,搜索2017,或者01,都可以搜索出来
china,搜索cn,也可以将china搜索出来
likes,搜索like,也可以将likes搜索出来
Tom,宋苏轼tom,也可以将Tom搜索出来
like,搜索love,同义词,也可以将like搜索出来