我有一个包含整数数组的prop:
_source: {
counts: [
11,
7,
18,
3,
22
]
}
从另一篇文章中我知道我可以使用
按范围过滤{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"range": {
"counts": {
"gte": 10,
"lte": 20
}
}
}
}
}
}
但是,我还需要知道范围匹配计数是否大于某个数字。例如,我只想要返回在10和20之间具有小于3个counts
匹配的记录。
使用的映射:
{
"properties" : {
"counts" : {
"type" : "integer"
}
}
}
这些是我索引的文档:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "test",
"_id": "2",
"_score": 1,
"_source": {
"counts": [
13,
17
]
}
},
{
"_index": "test_index",
"_type": "test",
"_id": "1",
"_score": 1,
"_source": {
"counts": [
11,
7,
18,
3,
22
]
}
},
{
"_index": "test_index",
"_type": "test",
"_id": "3",
"_score": 1,
"_source": {
"counts": [
11,
19
]
}
}
]
}
}
现在试试这个查询:
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": [
{"script" : { "script" : "doc['counts'].values.size() < 4" }},
{"range": { "counts": { "gte": 10, "lte": 20 } }}
]
}
}
}
结果:只返回文档id 2和3。文档1未返回。
{
"took": 29,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "test",
"_id": "2",
"_score": 1,
"_source": {
"counts": [
13,
17
]
}
},
{
"_index": "test_index",
"_type": "test",
"_id": "3",
"_score": 1,
"_source": {
"counts": [
11,
19
]
}
}
]
}
}
这是你想要做的吗?