I need to make a query with a list of Incidents and his nested events ordered DESC by his startedAt and timestamp dates. By default ReQL give the dates with a ASC order. I've got the folowing structure:
{
"id": "87e14db8-1e15-4718-baac-f1c785e985cb" ,
"title": "Connection Error"
"startedAt": Mon Oct 26 2015 14:33:00 GMT+00:00 ,
"events": [{
"message": "Cannot connect to theserver.com",
"timestamp": Mon Oct 26 2015 14:33:00 GMT+00:00
},{
"message": "Cannot connect to theserver.com,"
"timestamp": Mon Oct 26 2015 14:33:20 GMT+00:00
},{
"message": "Cannot connect to theserver.com",
"timestamp": Mon Oct 26 2015 14:33:40 GMT+00:00
}]
},{
"id": "87e14db8-1e15-4718-baac-f1c785e985cb" ,
"title": "Other Connection Error"
"startedAt": Mon Oct 26 2015 14:34:20 GMT+00:00 ,
"events": [{
"message": "Connection rejected",
"timestamp": Mon Oct 26 2015 14:34:20 GMT+00:00
},{
"message": "Connection rejected",
"timestamp": Mon Oct 26 2015 14:34:41 GMT+00:00
}]
},{
... (several more)
}
If I run r.db('mydb').table('Incident').orderBy(r.desc('createdAt')), the Incident's are ordered by createdAt as espected. But the nested eventsare still ordered ASC.
How can I make a query in order to get the nested events with a DESC order by timestamp?
Something like this should do it:
r.table('Incident').orderBy(r.desc('createdAt')).merge(function(row) {
return {events: row('events').orderBy(r.desc('timestamp'))};
})
I think this is what you're looking for. Just took a little wizardy with the .map(...) method.
r.db("test").table("stackoverflow").orderBy(r.desc('startedAt')).map(function(d){
return {
"startedAt":d("startedAt"),
"title": d("title"),
"id": d("id"),
"events": d("events").orderBy(r.desc("timestamp"))
}
})
Related
Given a table called alerts and a database called database with an array
of objects with a date attribute called History how can I pluck based
on a date range on that date attribute?
with the following query,
r.db("database").table("alerts").pluck("history").limit(10000)
I get back something like the following
{
"history": [
{
"text": "text1" ,
"updateTime": Thu Jun 20 2019 01:29:47 GMT+00:00 ,
},
{
"text": "text2" ,
"updateTime": Thu Jun 20 2019 01:24:59 GMT+00:00 ,
},
]
}
{
"history": [
{
"text": "text3" ,
"updateTime": Thu Jun 20 2018 01:29:47 GMT+00:00 ,
},
{
"text": "text4" ,
"updateTime": Thu Jun 20 2018 01:24:59 GMT+00:00 ,
},
]
}
how can I pluck the sub object called history and only return histories that are in a specific range on the updateTime attribute.
for example between jan/2/2009 to jan/3/2009
You need to filter based on a time range and use pluck on a nested object. Here are some examples about how to do that from the official documentation
r.table("users").filter(function (user) {
return user("subscriptionDate").during(
r.time(2012, 1, 1, 'Z'), r.time(2013, 1, 1, 'Z'));
}).run(conn, callback);
Source: https://www.rethinkdb.com/api/javascript/filter/
r.table('marvel').pluck({'abilities' : {'damage' : true, 'mana_cost' : true}, 'weapons' : true}).run(conn, callback)
Source: https://www.rethinkdb.com/api/javascript/pluck/
I am unable to migrate date in Elasticsearch 6. My date example is:
Fri, 21 Apr 2017 01:58:20 GMT
I have tried this without success:
"date": {
"type": "date",
"format": "E, d MMM Y H:m:s z"
}
Please help.
I'm enjoying learning reQL so far but I stumbled upon a problem.
This is the data that I have stored in a table called events
[{
"date": "Tue Mar 17 2015 00:00:00 GMT+00:00" ,
"id": "00dacebd-b27e-49b5-be4b-42c2578db4bb" ,
"event_name": "View page" ,
"total": 4 ,
"unique": 4
},
{
"date": "Mon Mar 16 2015 00:00:00 GMT+00:00" ,
"id": "09ac3579-960b-4a2b-95be-8e018d683494" ,
"event_name": "View page" ,
"total": 68 ,
"unique": 35
},
{
"date": "Tue Mar 17 2015 00:00:00 GMT+00:00" ,
"id": "0bb01050-e93d-4845-94aa-b86b1198338d" ,
"event_name": "Click" ,
"total": 17 ,
"unique": 8
},
{
"date": "Mon Mar 16 2015 00:00:00 GMT+00:00" ,
"id": "174dcf3e-7c77-47b6-a05d-b875c9f7e563" ,
"event_name": "Click" ,
"total": 113 ,
"unique": 35
}]
And I would like the end result to look like this
[{
"date": "Mon Mar 16 2015 00:00:00 GMT+00:00",
"Click": 113,
"View Page": 68
},
{
"date": "Tue Mar 17 2015 00:00:00 GMT+00:00",
"Click": 17,
"View Page": 4
}]
The closet I got was with this query:
r.table("events").orderBy({index: r.desc('date')}).group('date').map(function(event) {
return r.object(event('repo_name'), event('unique'));
}).reduce(function(a, b) {
return a.merge(b.keys().map(function(key) {
return [key, a(key).default(0).add(b(key))];}).coerceTo('object'));
})
The results is:
[{
"date": "Mon Mar 16 2015 00:00:00 GMT+00:00",
"reduction": {
"Click": 113,
"View page": 68
}
},
{
"group": "Tue Mar 17 2015 00:00:00 GMT+00:00",
"reduction": {
"Click": 17,
"View page": 4
}
}]
However as you can see the events are nested under reduction and changefeeds won't work on this query either :(
Anyone can point me in the right direction?
Cheers,
You can change the group/reduction format like this:
query.ungroup().map(function(row){
return r.expr({date: row('group')}).merge(row('reduction'));
})
Unfortunately changefeeds on aggregations aren't supported as of 1.6, but that should be possible in 2.2 or 2.3 (so in a few months).
I'm trying out rethinkDB and playing around with some query to see if it could fit by use case. So far, so good. However, I have a question regarding reQL.
For example in this case I store analytics events in rethinkDB such as:
[{
"userId": "abdf213",
"timestamp": "Sat Jan 17 2015 00:32:20 GMT+00:00",
"action": "Page"
},
{
"userId": "123abc",
"timestamp": "Sat Jan 17 2015 00:42:20 GMT+00:00",
"action": "Track"
},
{
"userId": "abdf213",
"timestamp": "Sat Jan 17 2015 00:45:20 GMT+00:00",
"action": "Track"
},
{
"userId": "123abc",
"timestamp": "Sat Jan 17 2015 00:44:20 GMT+00:00",
"action": "Page"
},
{
"userId": "123abc",
"timestamp": "Sat Jan 17 2015 00:48:20 GMT+00:00",
"action": "Page"
}]
I'd like the end result of my query to look like this:
{
"group": "123abc",
"reduction": {
"Page": 2,
"Track": 1
}
},
{
"group": "abdf213",
"reduction": {
"Page": 1,
"Track": 1
}
}
Bear in mind that the action name are not known in advance.
TBH, I'm not quite sure how to achieve this with ReQL.
Right now I have this query (using the data explorer):
r.db('test').table('events').group('userId').map(function(event) {
return event('action')
})
which return doc like this one:
{
"group": "-71omc5zdgdimpuveheqs6dvt5q6xlwenjg7m" ,
"reduction": [
"Identify" ,
"Page" ,
"Track"
]
}
Anyone can point me in the right direction here?
Cheers,
S
Try:
r.table('events').group('userId').map(function(event) {
return r.object(event('action'), 1);
}).reduce(function(a, b) {
return a.merge(b.keys().map(function(key) {
return [key, a(key).default(0).add(b(key))];}).coerceTo('object'));
})
Here's my solution:
r.table("events").group("userId", "action").count().ungroup()
.group(r.row("group")(0))
.map([r.row("group")(1), r.row("reduction")])
.coerceTo("object")
ReQL doesn't support nesting groups, but you can group by multiple fields at the same time and then performing further grouping on the output.
does somebody know if there is a way to build a elasticsearch query which sorts an array by date and queries only the youngest item? To demonstrate what I mean, here is an example of my data:
[{
_id: "51cd3ae45b99bd960ee0b425",
name: "Example entry",
documents: [
{
title: "Document Title 1",
subDocuments: [
{
date: "Mon Jul 08 2013 12:14:04 GMT+0200 (CEST)",
title: "start working"
},
{
date: "Fri Jul 11 2013 12:14:04 GMT+0200 (CEST)",
title: "finished"
},
{
date: "Thu Jul 10 2013 12:14:04 GMT+0200 (CEST)",
title: "working"
}
]
}
]
}, {
_id: "51cd3ae45b99bd960ee0b426",
name: "Example entry 2",
documents: [
{
title: "Document Title 1",
subDocuments: [
{
date: "Mon Jul 08 2013 12:14:04 GMT+0200 (CEST)",
title: "start working"
},
{
date: "Fri Jul 11 2013 12:14:04 GMT+0200 (CEST)",
title: "working"
}
]
}
]
}]
In this data I want to query for the value "working". As you can see the first document has the sub document title finished in his last status so this document is not to be allowed in the result set.
Could anybody give me a hint how to build up the query so that my requirements are met? Or is this impossible at this time and has to be developed in the client code?
Thanks and Greetings Kersten
I've managed this with editing my data. I just copy the last status to a own property. But I think this might be the solution: https://github.com/elasticsearch/elasticsearch/issues/3022