missing参数
missing参数用于指定,文档缺乏指定字段时的处理方式,missing参数值可以设置为_last(默认值,即位于最下方)、_first(位于最上方)、或者其它自定义值,该参数值将用于排序。
修改上述例中,文档3如下
PUT /product/myfruit/3?pretty
{
"product": "apple",
"offer": [
{}
]
}
POST /product/myfruit/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"offer.price": {
"order": "asc",
"mode": "avg",
"missing":"_first",
"nested_path": "offer"
}
}
]
}
返回结果部分截图:
更多参考:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html
例.不返回查询结果中的字段
POST /customer/external/_search?
{
"query": {
"match_all": {}
},
"_source":false
}
返回结果:
例.仅返回查询结果中指定的字段,firstname,account_number
POST /customer/external/_search?
{
"query": {
"match_all": {}
},
"_source": [
"firstname",
"account_number"
]
}
返回结果部分截图
使用通配符
例.仅返回查询结果中以em,或者字母a开头字段
POST /customer/external/_search?
{
"query": {
"match_all": {}
},
"_source": [
"a*",
"em*"
]
}
返回结果部分截图
includes和excludes
例.仅返回查询结果中字段名以字符a开头,但不以em开头的字段
POST /customer/external/_search?
{
"query": {
"match_all": {}
},
"_source": {
"includes": [
"a*"
],
"excludes": [
"em*"
]
}
}
参考链接:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html
返回脚本计算值(基于不同字段)
例.
POST /customer/external/_search?
{
"query": {
"match_all": {}
},
"script_fields": {
"test1": {
"script": {
"inline": "doc['account_number'].value * 2"
}
},
"test2": {
"script": {
"inline": "doc['account_number'].value * params.factor",
"params": {
"factor": 3
}
}
}
}
}
注意:这里,account_number为文档中已存在的字段名
返回结果
例.
POST /customer/external/_search?
{
"script_fields": {
"test1": {
"script": {
"inline": "params.factor * params.factor",
"params": {
"factor": 3
}
}
}
}
}
返回结果
例.访问_source,返回firstname的值
POST /customer/external/_search?
{
"query": {
"match_all": {}
},
"script_fields": {
"test1": {
"script": "params['_source']['firstname']"
}
}
}
返回结果部分截图
注意:使用doc['my_field_name'].value比使用arams['_source']['my_field_name']更快更效率,推荐使用