Elastic Search按嵌套文档的数量过滤
我有一个针对 公司 的elasticsearch索引,该索引具有一个称为 事务 的嵌套对象。交易至少有一个 日期 字段。这是一个示例:
firms: [
{
"name": "abc",
"address" : "xyz",
"transactions": [
{
"date" : "2014-12-20"
"side" : "buyer"
},
...
]
},
...
]
有了这些数据,我想查询过去6或12个月内所有(例如)交易超过3次的公司。
以下查询返回在过去12个月中至少有一笔交易的公司:
POST firms/firm/_search
{
"query": {
"nested": {
"path": "transactions",
"query": {
"bool": {
"must": [
{
"match": {
"transactions.side": "buyer"
}
},
{
"range": {
"transactions.date": {
"from": "2014-10-24",
"to": "2015-10-24"
}
}
}
]
}
}
}
}
}
我不确定如何扩展此查询以匹配在y +个月内进行x +交易的公司。任何帮助将不胜感激。谢谢
-
除了使用,我认为您别无选择
script
。像这样:{ "query": { "bool": { "must": [ { "nested": { "path": "transactions", "query": { "bool": { "must": [ { "match": { "transactions.side": "buyer" } }, { "range": { "transactions.date": { "from": "2014-10-24", "to": "2015-10-24" } } } ] } } } }, { "filtered": { "filter": { "script": { "script": "if(_source.transactions.size<3) return false;fromDate=Date.parse('yyyy-MM-dd',fromDateParam);toDate=Date.parse('yyyy-MM-dd',toDateParam);count=0;for(d in _source.transactions){docsDate=Date.parse('yyyy-MM-dd',d.get('date'));if(docsDate>=fromDate && docsDate<=toDate){count++};if(count==3){return true;}};return false;", "params": { "fromDateParam":"2014-10-24", "toDateParam":"2015-10-24" } } } } } ] } } }
range
对于没有日期匹配的那些文档,实际的过滤器是“优化”的。因此,此文档(范围内没有日期)将不会到达费用更高的script
过滤器。在
script
本身首先检查是否交易的数量少于3
。如果是这样,请不要打扰所有日期检查并返回false
。如果超过,3
则取每个日期并与参数进行比较。一旦3
达到计数,请停止查看其余日期并返回true
。
-
Elastic Search嵌套对象查询
2021-02-01 关注 0 浏览122 1答案
-
Elastic search,是否可以在不更新整个文档的情况下更新嵌套对象?
2021-02-01 关注 0 浏览267 1答案
-
Elastic Search 6嵌套查询聚合
2021-02-01 关注 0 浏览129 1答案
-
Elastic search DSL:用通配符过滤吗?
2021-02-01 关注 0 浏览156 1答案
-
Elastic Search中嵌套字段的术语聚合
2021-02-01 关注 0 浏览93 1答案
-
Elastic Search GET没有提供最新文档
2021-02-01 关注 0 浏览101 1答案
-
从Shell脚本调用Elastic search索引pdf文档
2021-02-01 关注 0 浏览102 1答案
-
Elastic Search 5.x嵌套多个查询C#
2021-02-01 关注 0 浏览98 1答案
-
匹配完整的复杂嵌套集合项,而不是通过Elastic Search匹配单独的成员
2021-02-01 关注 0 浏览65 1答案
-
在Elastic Search中模拟字段折叠/按字段分组
2021-02-01 关注 0 浏览124 1答案