sql - Joins in Elasticsearch - can a nested or parent query handle this join case? -
i have data in elastic (translating table format, how picture it)
|record id | userid | experiment condition | action taken | |----------|--------|----------------------|--------------| |1 | 1234 | b | | |2 | 1234 | | search | |3 | 1234 | | click | |4 | 5678 | | | |5 |5678 | | search | |6 |5678 | | search | and on
what want able find instances action_taken='search' , user in experiment_condition='b'
that's pretty trivial in sql-land, i'm struggling figure out how in elastic.
i've looked @ parent/child queries here: https://www.elastic.co/guide/en/elasticsearch/guide/current/children-agg.html doesn't seem that's want, maybe i'm having trouble wrapping brain around it.
thanks!
edit: here json records in es
{ "_index": "04-06-2017", "_type": "logs", "_id": "record_id_123", "_score": null, "_source": { "@version": "1", "@timestamp": "2017-04-06t03:59:42.000z", "datetime": "05/apr/2017:23:59:42 -0400", "duration": "05/apr/2017:23:59:42 -0400", "sessionid": "session_hash_1234", "un": "user_id_1234", "ev": "login", "experimentgroup": "b" }, "fields": { "@timestamp": [ 1491451182000 ] } }, { "_index": "04-06-2017", "_type": "logs", "_id": "record_id_567", "_score": null, "_source": { "@version": "1", "@timestamp": "2017-04-06t04:00:22.000z", "datetime": "06/apr/2017:00:00:22 -0400", "rc": "200", "pcd": "85", "duration": "06/apr/2017:00:00:22 -0400", "un": "user_id_1234", "sessionid": "session_hash_1234", "rtid": "query_hash_1234", "js": "1", "rs": "1422x889", "cd": "16", "ln": "en", "tz": "gmt%20-04%3a00", "action": "toc", "event": "click", "node": "button", "ev": "search", "query":"query_terms_here" }, "fields": { "@timestamp": [ 1491451222000 ] }, } what want able find records ev='search' users experimentgroup='b'
so after fighting while, decided 2 queries. using elastic plugin python, data needed issue terms query contained list of users.
get users:
blob=es.search(index='my-index', body={ "size": 0, "query": { "query_string": { "query": "settings.experimentgroup:\"a\"" } } }) then turn list
users_in_group_a = json_normalize(blob['hits']['hits']) unique_users_in_a=list(set(users_in_group_a['_id'])) then results users:
blob = es.search(index='my-index', body={ "size":0, 'query': { 'filtered': { 'filter': { 'bool':{ 'must':[ {'terms': { 'userid': [unique_users]}}, {'range':{'@timestamp':{'gte':1489280900154,'lte':1491005332057}}} ] } } } })
Comments
Post a Comment