[英]Elasticsearch - Script filter on array
I'm a new bie in ES and I want to use script filter to get all match that the array has at least one element less than max and greater than min (max and min are param in the script). 我是ES中的新bie,我想使用脚本过滤器来获得所有匹配,即数组至少有一个小于max且大于min的元素(max和min是脚本中的param)。
The document like: 该文件如:
{
"number": "5",
"array": {
"key": [
10,
5,
9,
20
]
}
}
I tried the script but it does not work 我尝试了脚本,但它不起作用
{
"script": {
"lang": "groovy",
"params": {
"max": 64,
"min": 6
},
"script": "for(element in doc['array.key'].values){element>= min + doc['number'].value && element <=max + doc['number'].value}"
}
}
There is no error message but the search result is wrong.Is there a way to iterate array field? 没有错误消息,但搜索结果是错误的。有没有办法迭代数组字段?
Thank you all. 谢谢你们。
Yes it's doable, your script is not doing that, though. 是的,这是可行的,但是你的脚本并没有这样做。 Try using Groovy's any() method instead:
尝试使用Groovy的any()方法代替:
doc['array.key'].values.any{ it -> it >= min + doc['number'] && it <= max + doc['number'] }
A few things: 一些东西:
number
into an integer
type number
为integer
类型 array
and inside it a nested field key
. array
,里面有一个嵌套的字段key
。 Couldn't you just have a field array
that would be... and array? array
......和数组吗? ;-) conf/elasticsearch.yml
but I'm guessing you've done it, otherwise you'd be getting exceptions. conf/elasticsearch.yml
elasticsearch.yml中启用动态脚本,但我猜你已经完成了,否则你会得到异常。 A very simple mapping like this should work: 像这样的非常简单的映射应该有效:
{
"mappings": {
"document": {
"properties": {
"value": {
"type": "integer"
},
"key": {
"type": "integer"
}
}
}
}
}
Example: 例:
POST /documents/document/1
{
"number": 5,
"key": [
10,
5,
9,
20
]
}
POST /documents/document/2
{
"number": 5,
"key": [
70,
72
]
}
Query: 查询:
GET /documents/document/_search
{
"query": {
"bool": {
"filter": {
"script": {
"lang": "groovy",
"params": {
"max": 64,
"min": 6
},
"script": "doc['key'].values.any{ it -> it >= min + doc['number'] && it <= max + doc['number'] }"
}
}
}
}
}
Result: 结果:
{
"took": 22,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0,
"hits": [
{
"_index": "documents",
"_type": "document",
"_id": "1",
"_score": 0,
"_source": {
"number": 5,
"key": [
10,
5,
9,
20
]
}
}
]
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.