不再使用拼接的query-string参数(GET query?xx=xx&xx=xx),而是一个json请求体替代。
查询
查询所有古诗
GET article/poems/_search
{
"query": {
"match_all": {}
}
}
查询有"春"字的古诗
GET article/poems/_search
{
"query":{
"match":{
"content":"春"
}
}
}
根据id倒序排序
GET article/poems/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"_id": {
"order": "desc"
}
}
]
}
根据诗的字数倒序排序
GET article/poems/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"words": {
"order": "desc"
}
}
]
}
分页
GET article/poems/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 2
}
指定要查那些字段
GET article/poems/_search
{
"query": {
"match_all": {}
},
"_source": ["title", "content"]
}
should和must
查找诗中有"春"和"雨"的,相当于and
GET article/poems/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"content": {
"value": "春"
}
}
},
{
"term": {
"content": {
"value": "雨"
}
}
}
]
}
}
}
查找诗中有"春"或"雨"的,相当于or
GET article/poems/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"content": {
"value": "春"
}
}
},
{
"term": {
"content": {
"value": "雨"
}
}
}
]
}
}
}
过滤条件(query filter)
查找含"春"字的诗并且字数在10-30之间的
GET article/poems/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"content": "春"
}
}
],
"filter": {
"range": {
"words.keyword": {
"gte": 10,
"lte": 30
}
}
}
}
}
}
全文检索(full-text search)
查找带诗里带"春"和"月"的
GET article/poems/_search
{
"query": {
"match": {
"content": "春 月"
}
}
}
短语搜索(phrase search)
搜索content中包含"好雨"的诗
GET article/poems/_search
{
"query": {
"match_phrase": {
"content": "好雨"
}
}
}
Q&A
- Q:term和match的区别
https://www.jianshu.com/p/d5583dff4157