1

I'm very new to Elasticsearch and currently trying to translate a MySQL query into Elasticsearch one. The documents are stored under "Json_logs" index in ES.

Document:

{
    "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 query:

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

What would be the equivalent query in Elasticsearch for the above MySQL query? Thank you!

2
  • WHERE zone_id=124519 AND floor_price=0.2 isn't GROUP BY zone_id redundant ? Commented Feb 4, 2016 at 22:53
  • you're right. it's redundant. can you help me with translate the query into elasticsearch query? Commented Feb 4, 2016 at 23:22

1 Answer 1

1

Try using following query:

{
"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"       
            }

        }   
      }

     }

    }
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.