I have these product documents:
curl -XPOST -H 'Content-Type: application/json' "http://localhost:9200/test/_bulk?pretty" -d'
{"index":{"_index":"test","_type":"product"}}
{"id":"1", "price": "260" }
{"index":{"_index":"test","_type":"product"}}
{"id":"2", "price": "420" }
{"index":{"_index":"test","_type":"product"}}
{"id":"3", "price": "250" }
And these discount documents:
curl -XPOST -H 'Content-Type: application/json' "http://localhost:9200/test/_bulk?pretty" -d'
{"index":{"_index":"test","_type":"discount"}}
{"product_id":"1", "group_id": 12, "percent": "50" }
{"index":{"_index":"test","_type":"discount"}}
{"product_id":"1", "group_id": 15, "percent": "20" }
{"index":{"_index":"test","_type":"discount"}}
{"product_id":"3", "group_id": 12, "percent": "10" }
I need to join these two documents via the product_id
and group_id
key.
In the mysql database would look like this:
SELECT p.id, IF(d.percent > 0, p.price - (d.percent*p.price/100), p.price) price
FROM product p
LEFT JOIN discount d ON p.id=d.product_id AND d.group_id=12
WHERE IF(d.percent > 0, p.price - (d.percent*p.price/100), p.price) < 210
The result of that query will be only one row:
id => 1
price => 130
Is it possible to do in elasticsearch?