Elasticsearch多个过滤器,sum和group by

时间:2016-02-04 21:13:43

标签: elasticsearch

我对Elasticsearch很新,目前正试图将MySQL查询转换为Elasticsearch。这些文件存储在" Json_logs" ES中的索引。

文件:

{
    "bid_id": "16613393",
    "user_id": "f63edcf2-e353-4c50-8a6e-290f973b320e",
    "date": "2016-01-03",
    "timestamp1": 1451861391,
    "timestamp2": null,
    "zone": {
        "zone_id": 124519,
        "tag_type": null,
        "in_iframe": null,
        "document_referrer": "http%3A%2F%2Fwww.celebritynetworth.com%2Frichest-celebrities%2Fsingers%2Fmichael-jackson-net-worth%2F",
        "cost": 0.4,
        "parent_zone_id": null
    },
    "device": {
        "ip": "174.52.96.91",
        "ua": "Mozilla/5.0 (iPad; CPU OS 9_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13C75 Safari/601.1",
        "device_code": 5,
        "device_type": "Tablet"
    },
    "bid": {
        "page_url": null,
        "floor_price": 0.3,
        "buyers": []
    },
    "impression": {
        "buyer_id": null,
        "win_price": 0.3,
        "banner_id": 1029526
    }
}

MySQL查询:

SELECT SUM(floor_price) 
FROM Json_logs
WHERE zone_id=124519 AND floor_price=0.2
GROUP BY zone_id;

Elasticsearch中针对上述MySQL查询的等效查询是什么?谢谢!

1 个答案:

答案 0 :(得分:1)

尝试使用以下查询:

{
"query": {
    "bool": {
        "must": [
           {
               "term": {
                     "zone.zone_id": 124519
               }
           },
           {
               "term": {
                     "bid.floor_price": 0.2
               }
           }
        ]
    }
},
"aggs" : {
    "group_by_zone" : {

     "terms" : {
      "field"    : "zone.zone_id"
     },
     "aggs" : {

     "sum_floor_price"   : {
            "sum" :  {
             "field"    : "bid.floor_price"       
            }

        }   
      }

     }

    }
  }