[英]remove first element from array and return the array minus the first element
[英]Return first n element from array in elasticsearch query
我在名为 IP 的文档中有一个数组字段,其中包含 10000 ips 以上的元素。
例如
IP:["192.168.a:A","192.168.a:B","192.168.a:C","192.168.A:b"...........]
现在我用一些过滤器进行了搜索查询,我得到了结果,但问题是由于上述字段,结果的大小非常大。
现在我只想从数组中获取 N ips 假设只有 10 个订单无关紧要。
那我该怎么做...
更新:
除了 IP 字段,还有其他字段,我在该字段上应用了过滤器,而不是在 IP 上。我想要满足过滤器的整个文档。我只想限制单个 IP 字段中的元素数量。(如果有,请告诉我除了使用脚本之外的其他方式)。
这种请求可以解决您的问题:
GET ips/_search
{
"query": {
"match_all": {}
},
"script_fields": {
"truncate_ip": {
"script": {
"source": """
String[] trunc_ip = new String[10];
for (int i = 0; i < 10; ++i) {
trunc_ip[i]= params['_source']['IP'][i];
}
return trunc_ip;
"""
}
}
}
}
默认情况下,ES 只返回 10 个匹配的结果,所以我不确定您的搜索查询是什么以及您到底想限制什么
请在上面澄清并提供您的搜索查询以提供进一步帮助。
您可以使用scriptedFields从 Elastic Search 中的现有字段生成新字段。 作为评论添加的详细信息。
GET indexName/_search
{
"_source": {
"excludes": "ips" //<======= Exclude from source the IP field (change the name based on your document)
},
"query": {
"match_all": {} // <========== Define relevant filters
},
"script_fields": {
"limited_ips": { // <========= add a new scipted field
"script": {
"source": "params['_source'].ips.stream().limit(2).collect(Collectors.toList())" // <==== Replace 2 with the number of i.ps you want in result.
}
}
}
}
笔记:
到目前为止,从脚本访问字段值最快最有效的方法是使用 doc['field_name'] 语法,它从 doc 值中检索字段值。 Doc 值是一个列式字段值存储,默认情况下在所有字段上启用,分析的文本字段除外
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.