0

Query to find users revenue on 5 apr belongs to delhi and had done sign up event on 5th april.

SELECT sum(revenue) FROM order WHERE purchased_date = '2017-04-05 00:00:00' and userid in ( select a.userId from user a, event b where a.userid=b.userid and a.city='delhi' and b.time='2017-04-05 00:00:00' and b.name='signup')

Tables

Order
id  purchasedDate        revenue  userId
  1  2017-04-05 00:00:00    30       1
  2  2017-04-06 00:00:00    30       1
  3  2017-04-05 00:00:00    80       2
  1  2017-04-06 00:00:00    70       2
  1  2017-04-05 00:00:00    60       3
  1  2017-04-06 00:00:00    40       3
  1  2017-04-07 00:00:00    50       3
  1  2017-04-07 00:00:00    30       3
User 
 id  city 
  1  delhi
  2  noida
  3  delhi
Event
 userid    name         time
  1      signup  2017-04-05 00:00:00
  2      signup  2017-04-05 00:00:00
  3      signup  2017-04-05 00:00:00

1 Answer 1

1

yeah this is doable with elastic, but first of all you have to model your data from SQL table to document based no-sql data.

Nested document approach

1)You can model one instance of data as follows

{
    "purchasedDate": "2017-04-05",
    "revenue": 30,
    "user": {
        "id": 1,
        "city": "delhi",
        "event": [{
            "name": "signup",
            "time": "2017-04-05"
        }]
    }
}

For this data model you will also need to add supporting mappings

{
    "mappings": {
        "type_name": {
            "properties": {
                "purchasedDate": {
                    "type": "date"
                },
                "revenue": {
                    "type": "integer"
                },
                "user": {
                    "type": "object",
                    "properties": {
                        "id": {
                            "type": "integer"
                        },
                        "city": {
                            "type": "text"
                        },
                        "event": {
                            "type": "nested",
                            "properties": {
                                "name": {
                                    "type": "text"
                                },
                                "time": {
                                    "type": "date"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

and finally you can fire the following query to get the desired results

{
    "size": 0,
    "aggs": {
        "revenue_sum": {
            "sum": {
                "field": "revenue"
            }
        }
    },
    "query": {
        "bool": {
            "must": [{
                "term": {
                    "purchasedDate": {
                        "value": "2017-04-05"
                    }
                }
            }, {
                "term": {
                    "user.city": {
                        "value": "delhi"
                    }
                }
            }, {
                "nested": {
                    "path": "user.event",
                    "query": {
                        "bool": {
                            "must": [{
                                "term": {
                                    "user.event.name": {
                                        "value": "signup"
                                    }
                                }
                            }, {
                                "term": {
                                    "user.event.time": {
                                        "value": "2017-04-05"
                                    }
                                }
                            }]
                        }
                    }
                }
            }]
        }
    }
}

for furthur reading you can refer to following links

mappings nested datatype

Parent Child approach

Mappings

{
    "mappings": {
        "user": {
            "properties": {
                "city": {
                    "type": "text"
                }
            }
        },
        "event": {
            "_parent": {
                "type": "user"
            },
            "properties": {
                "name": {
                    "type": "text"
                },
                "time": {
                    "type": "date"
                }
            }
        },
        "order": {
            "_parent": {
                "type": "user"
            },
            "properties": {
                "purchasedDate": {
                    "type": "date"
                },
                "revenue": {
                    "type": "integer"
                }
            }
        }
    }
}

Index user, order and event documents

POST data_play2/user/1
{
  "city":"london"
}

POST data_play2/order/10?parent=1
{
  "purchasedDate":"2017-04-05",
  "revenue": 100
}

POST data_play2/event/1?parent=1
{
  "userid" : 1,
  "name" : "signup",
  "time" : "2017-04-05"
}

Query

    {
    "size": 0,
    "aggs": {
        "revenue": {
            "children": {
                "type": "order"
            },
            "aggs": {
                "filtered_order": {
                    "filter": {
                        "bool": {
                            "must": [{
                                "term": {
                                    "purchasedDate": {
                                        "value": "2017-04-05"
                                    }
                                }
                            }]
                        }
                    },
                    "aggs": {
                        "revenue_sum": {
                            "sum": {
                                "field": "revenue"
                            }
                        }
                    }
                }
            }
        }
    },
    "query": {
        "bool": {
            "must": [{
                    "term": {
                        "city": {
                            "value": "london"
                        }
                    }
                }, {
                    "has_child": {
                        "type": "order",
                        "query": {
                            "bool": {
                                "must": [{
                                    "term": {
                                        "purchasedDate": {
                                            "value": "2017-04-05"
                                        }
                                    }
                                }]
                            }
                        }
                    }
                }, {
                    "has_child": {
                        "type": "event",
                        "query": {
                            "bool": {
                                "must": [{
                                    "term": {
                                        "name": {
                                            "value": "signup"
                                        }
                                    }
                                }, {
                                    "term": {
                                        "time": {
                                            "value": "2017-04-05"
                                        }
                                    }
                                }]
                            }
                        }
                    }
                }

            ]
        }
    }
}

Refer this link for furthur reading

Hope this helps

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

7 Comments

Can we solve it parent child relationship.Where user as a parent,order and event table is child of user and schema is same of all table.
yeah you can, parent-child will be better if you have large number of orders for each user as the size of the document will be in limit
can u please share query if i have user as as parent and order and event as a child and all have same schema as mysql.
sure, i will share in sometime after food
hey, i have updated my answer for parent-child approach, let me know
|

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.