1.准备工作,添加数据
PUT /zoo/product/1
{
"name":"monkey",
"age":10,
"content":"xiao small but ke ai"
}
PUT /zoo/product/2
{
"name":"monkey",
"age":13,
"content":"xiao small but very big"
}
PUT /zoo/product/3
{
"name":"dog",
"age":13,
"content":"xiao big but very small"
}
PUT /zoo/product/4
{
"name":"cat",
"age":18,
"content":"xiao big but very small"
}
PUT /zoo/product/5
{
"name":"tig",
"age":30,
"content":"xiao big but very big"
}
2.搜索zoo下面product的所有document
GET /zoo/product/_search
took:耗费了几毫秒
timed_out:是否超时,这里是没有
_shards:数据拆成了5个分片,所以对于搜索请求,会打到所有的primary shard(或者是它的某个replica shard也可以)
hits.total:查询结果的数量,3个document
hits.max_score:score的含义,就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也高
hits.hits:包含了匹配搜索的document的详细数据
3.查询所有动物
GET /zoo/product/_search
{
"query": {
"match_all": {}
}
}
4.查询包含big的内容,并且按照age降序(desc是降序,asc是升序)
GET /zoo/product/_search
{
"query": {
"match": {
"content": "big"
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
5.从0下标开始查询出2包含big的动物
GET /zoo/product/_search
{
"query": {
"match": {
"content": "big"
}
},
"size": 2,
"from": 0
}
6.查询出来的数据只要age和content属性
GET /zoo/product/_search
{
"_source": ["age","content"]
}
7.查询出包含big but整段短语
GET /zoo/product/_search
{
"query": {
"match_phrase": {
"content": "big but"
}
}
}
8.查询出来的数据进行高亮处理
GET /zoo/product/_search
{
"query": {
"match": {
"content": "big but"
}
},
"highlight": {
"fields": {
"content": {}
}
}
}