参考链接:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html
例.
POST /customer/external/_search?
{
"query": {
"match_all": {}
},
"docvalue_fields" : ["account_number", "test2"]
}
返回结果部分截图
filter在aggregation完成后才被执行。
PUT /shirts
{
"mappings": {
"item": {
"properties": {
"brand": { "type": "keyword"},
"color": { "type": "keyword"},
"model": { "type": "keyword"}
}
}
}
}
PUT /shirts/item/1?refresh
{
"brand": "gucci",
"color": "red",
"model": "slim"
}
例.仅返回搜索结果中包含color为red,brand为gucci的文档记录
POST /shirts/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"color": "red"
}
},
{
"term": {
"brand": "gucci"
}
}
]
}
}
}
例.仅返回搜索结果中包含color为red,brand为gucci的shirt,按model分组,按分组统计数降序排序
POST /shirts/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"color": "red"
}
},
{
"term": {
"brand": "gucci"
}
}
]
}
},
"aggs": {
"models": {
"terms": {
"field": "model"
}
}
}
}
返回结果部分截图
例.
例.仅搜索brand值为gucci的shirt,按color分组,降序展示每种color的shirt数量,同时,针对color为red的shirt商品,按model分组统计,降序展示每种model的数量
POST /shirts/_search
{
"query": {
"bool": {
"filter": {
"term": {
"brand": "gucci"
}
}
}
},
"aggs": {
"group_by_colors": {
"terms": {
"field": "color"
}
},
"color_red": {
"filter": {
"term": {
"color": "red"
}
},
"aggs": {
"group_by_models": {
"terms": {
"field": "model"
}
}
}
}
},
"post_filter": {
"term": {
"color": "red"
}
}
}
说明: "post_filter",作用于最后,不展示color不为red的shirt记录
返回结果部分截图
例.如下,每页只显示5条记录,按leve_vale降序排序,如果leve_vale相同则按_uid降序排序
POST /fenxi/fenxishuj/_search?
{
"query": {
"match_all": {}
},
"sort": [
{
"leve_vale":"desc",
"_uid": "desc"
}
],
"size":5
}
返回结果部分截图
这时,在不改变页size值的情况下,我们想查看下一页的记录,咋办?
方案:把sort中的参数值,按出现顺序,依次传递给search_after
POST /fenxi/fenxishuj/_search?
{
"query": {
"match_all": {}
},
"search_after":[31,"fenxishuj#9"],
"sort": [
{"leve_vale":"desc",
"_uid": "desc"
}
],
"size":5
}
注意:
1、sort中的参数值要和search_after一一对应(数量&顺序的对应)。
2、使用了search_after的情况下,如果要使用from参数,参数值只能为0、-1
参考资料:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-search-after.html
更多资料参考:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html