在弹性搜索中过滤嵌套对象

时间:2016-09-28 08:18:37

标签: elasticsearch

我的索引具有以下映射

{
    "mappings": {
        "data": {
            "date_detection": false,
            "_all": {
                "enabled": false
            },
            "properties": {
                "DocumentId": {
                    "type": "string"
                },
                "SubscriptionId": {
                    "type": "long"
                },
                "AccountId": {
                    "type": "long"
                },
                "SubscriptionStartDateTime": {
                    "type": "date",
                    "format": "yyyy-MM-dd HH:mm:ss"
                },
                "SubscriptionEndDateTime": {
                    "type": "date",
                    "format": "yyyy-MM-dd HH:mm:ss"
                },
                "DateWiseMetrics": {
                    "type": "nested",
                    "properties": {
                        "Date": {
                            "type": "date",
                            "format": "yyyy-MM-dd"
                        },
                        "Usage": {
                            "type": "long"
                        }
                    }
                }
            }
        }
    }
}

我想做的是

  1. 对于给定的帐户ID,开始日期和结束日期查找该期间内的所有有效订阅
  2. 查找步骤1中找到的所有有效订阅的给定时间段的使用情况。
  3. 例如,

    如果开始日期=“2017-08-01 00:00:00”并且结束日期=“2017-09-01 00:00:00”,我将在该期间找到所有有效订阅然后我想要查看2017-08-01至2017-09-01期间每日订阅的使用情况。

    我能够使活动订阅(步骤1)正确,但是当我在嵌套对象中进一步按日期过滤时,得不到正确的值(步骤2)。

    {
       "_source" : false,
       "query": {
          "filtered": {
             "query": {
                "match_all": {}
             },
             "filter": {
                "bool": {
                   "must": [
                      { "term" : { "AccountId" : 290804220029 } },
                      {
                         "nested": {
                            "path": "DateWiseMetrics",
                            "filter":
    
                                  { "range" : { "DateWiseMetrics.Date": { "gte": "2017-01-01", "lte": "2017-08-01" }  }
    
                            }, "inner_hits" : {}
                         }
                      }
                   ],
                   "must_not" : [
                       { "range" : { "SubscriptionStartDateTime": { "gt": "2017-08-01 00:00:00" }  }},
                       { "range" : { "SubscriptionEndDateTime": { "lt": "2017-01-01 00:00:00" }  }}
    
                   ]
                }
             }
          }
       }
    
    }
    

    我认为我过滤嵌套对象的方式是错误的。请帮忙。

    此外,有没有办法显示少数父项的值(例如:accountid,subscriptionid)以及内部命中结果?我观察到的是当你添加“_ source”:false 时,父元素不再出现了。

    由于

0 个答案:

没有答案
相关问题