模板查询
此模板可以用于search_after 和from查询: 上传ES模板操作请留言
模板一:
// 此模板查询代码在:
https://blog.csdn.net/Chat_FJ/article/details/123873739?spm=1001.2014.3001.5501
https://blog.csdn.net/Chat_FJ/article/details/123873739?spm=1001.2014.3001.5501
{
"track_total_hits": true,
"query" : {
"bool" : {
"filter" : [
{{!文件名称}}
{{#name}}
{
"bool" : {
"should" : [{
"match" : {
"attributes.name" : "{{name}}"
}
}, {
"term" : {
"attributes.name.raw" : "{{name}}"
}
}],
"minimum_should_match" : 1
}
},
{{/name}}
{{!最后访问时间}}
{{#lastAccessed}}
{
"range": {
"lastAccessed" : {
{{#start}}
"gte" : "{{start}}",
{{/start}}
{{#end}}
"lte" : "{{end}}",
{{/end}}
"boost" : 1.0
}
}
},
{{/lastAccessed}}
{{!自定义查询}}
{{#customized}}
{{#toJson}}customized{{/toJson}}
{{/customized}}{{^customized}}
{
"match_all" : { }
}
{{/customized}}
]
}
},
"sort": [
{{#desc}}
{ "{{desc}}" : {"order" : "desc"}},
{{/desc}}
{{#descnullfirst}}
{ "{{descnullfirst}}" : {"order" : "desc", "missing" : "_first"}},
{{/descnullfirst}}
{{#asc}}
{ "{{asc}}" : {"order" : "asc"}},
{{/asc}}
{ "_id" : {"order" : "asc"}}
],
{{#from}}
"from" : {{from}},
{{/from}}{{^from}}
"search_after" : {{#toJson}}search_after{{/toJson}},
{{/from}}
"size" : {{size}}
}
search-after查询--------Postman操作测试
POST
http://192.168.12.148:19200/filemetadata/_search/template
{
"id": "filemetadata01",
"params": {
"customized": {"term":{"attributes.name":"test0331"}},
"lastAccessed": {"start":1648540192929},
"search_after":[1648601617449,"e6590332010c44aca252e130fe418250"],
"size":1,"desc":"lastAccessed"
}
}
结果展示:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "filemetadata",
"_type": "_doc",
"_id": "10c0c28bcc194edc848d5683790bffd6",
"_score": null,
"_source": {
"attributes": {
"name": "test0331",
"size": 140,
"mime": "string",
"lastModified": 0
},
"storage": {
"hdfs": {
"path": "10c0c28bcc194edc848d5683790bffd6",
"state": "pending"
}
},
"metadata": {
"description": "test0331"
},
"created": 1648540241746,
"lastModified": 1648540241746,
"lastAccessed": 1648540241746
},
"sort": [
1648540241746,
"10c0c28bcc194edc848d5683790bffd6"
]
}
]
}
}
from查询 --------Postman操作测试
POST
http://192.168.12.148:19200/filemetadata/_search/template
{
"id": "filemetadata01",
"params": {
"customized": {"term":{"attributes.name":"test0331"}},
"lastAccessed": {"start":1648540192929},
"from":0,"size":5,"desc":"lastAccessed"
}
}
结果展示
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "filemetadata",
"_type": "_doc",
"_id": "e6590332010c44aca252e130fe418250",
"_score": null,
"_source": {
"attributes": {
"name": "test0331",
"size": 240,
"mime": "string",
"lastModified": 0
},
"storage": {
"hdfs": {
"path": "e6590332010c44aca252e130fe418250",
"state": "pending"
}
},
"metadata": {
"description": "test0331"
},
"created": 1648601617449,
"lastModified": 1648601617449,
"lastAccessed": 1648601617449
},
"sort": [
1648601617449,
"e6590332010c44aca252e130fe418250"
]
}
]
}
}
模板二:
{
"track_total_hits": true,
"query" : {
"bool" : {
"filter" : [
{
"match_all" : {}
}
{{#SrcIp}}
,{
"match" : {
"SrcIp" : "{{SrcIp}}"
}
}
{{/SrcIp}}
{{#DstIp}}
,{
"match" : {
"DstIp" : "{{DstIp}}"
}
}
{{/DstIp}}
{{#ip}}
,{
"match" : {
"ip" : "{{ip}}"
}
}
{{/ip}}
{{#Others}}
,{
{{#match}}
"match" : { "{{key}}" : "{{match}}" }
{{/match}}
{{#term}}
"term" : { "{{key}}" : "{{term}}" }
{{/term}}
{{#terms}}
"terms" : { "{{key}}.keyword" : {{#toJson}}terms{{/toJson}} }
{{/terms}}
{{#wildcard}}
"wildcard" : { "{{key}}.keyword" : "{{wildcard}}" }
{{/wildcard}}
{{#prefix}}
"prefix" : { "{{key}}.keyword" : "{{prefix}}" }
{{/prefix}}
{{^match}}{{^term}}{{^terms}}{{^wildcard}}{{^prefix}}
"range": {
"{{key}}" : {
"boost" : 1.0
{{#lt}}
,"lt" : "{{lt}}"
{{/lt}}
{{#lte}}
,"lte" : "{{lte}}"
{{/lte}}
{{#gt}}
,"gt" : "{{gt}}"
{{/gt}}
{{#gte}}
,"gte" : "{{gte}}"
{{/gte}}
}
}
{{/prefix}}{{/wildcard}}{{/terms}}{{/term}}{{/match}}
}
{{/Others}}
]
}
},
"sort": [
{{#desc}}
{ "{{desc}}" : {"order" : "desc"}},
{{/desc}}
{{#descnullfirst}}
{ "{{descnullfirst}}" : {"order" : "desc", "missing" : "_first"}},
{{/descnullfirst}}
{{#asc}}
{ "{{asc}}" : {"order" : "asc"}},
{{/asc}}
{ "_id" : {"order" : "asc"}}
],
"from" : "{{from}}",
"size" : "{{size}}"
}
{"id":"query","params":{"Others":[{"key":"Tag","terms":["521016688636858467"]},{"key":"componentID","term":-1}],"from":0,"size":10,"desc":"recvBytes","asc":"","descnullfirst":""}}
|