弹性搜索查询,用于聚合数组的子字段

时间:2018-04-12 04:22:11

标签: elasticsearch

我的弹性搜索索引包含这种格式的数据,数据是一个对象数组,其中包含该日期上该日期的日期,顺序和访问: -

  {
"term": "ふるさと納税",    
  "data": [

        {
          "date": "2018-01-25",
          "visits": 17670,
          "ranking": 1,
          "orders": 154
        },
        {
          "date": "2018-02-14",
          "visits": 13758,
          "ranking": 1,
          "orders": 116
        },
        {
          "date": "2017-12-24",
          "visits": 142578,
          "ranking": 1,
          "orders": 2565
        },
        {
          "date": "2018-03-08",
          "visits": 21799,
          "ranking": 1,
          "orders": 312
        }
    ]
 },{
      "term": "帯 中古 振袖",
      "data": [
        {
          "date": "2018-01-30",
          "ranking": 2966,
          "orders": 0,
          "visits": 345
        }
      ]
    }

我想在规定的日期范围内总结该期限的所有访问和订单

我创建了这个查询: -

{
 "_source": [],
  "query": {
"bool": {
  "filter": [
    {"range": {"data.date": {"gte" : "2018-03-21"}}},
    {"range": {"data.date": {"lte" : "2018-03-21"}}}
  ]
}
   },
   "aggs" : {
"by_term": {
  "terms": {
    "field": "term",
    "order":{"sum_ranking":"desc"},
    "size":100
     },"aggs": {
      "sum_ranking":  {
        "sum": {
          "field" : "data.visits"
       }
    }
  }
}
  },
"from" : 0, 
"size" : 0
 }

似乎过滤器无法正常工作。 任何人都可以提供帮助。

映射是: -

{

"settings" : {

    "number_of_shards" : 1

},

"mappings" : {

    "keyword" : {

        "properties" : {

            "term" : { "type" : "keyword" }

        }

    }

}

}

0 个答案:

没有答案