Rethinkdb multiple fields with different ordering - rethinkdb

Is it possible to do order_by on multiple fields with different ordering?
r.table("users").order_by(r.desc("age") & r.asc("name"))

Of course. Just take a look at the RethinkDB orderBy documentation:
table.orderBy([key | function...], {index: index_name}) → table_slice
selection.orderBy(key | function[, ...]) → selection<array>
sequence.orderBy(key | function[, ...]) → array
Note the ellipsis (...) that marks a variadic method allowing arbitrary number of arguments. Therefore your query becomes as following:
r.table("users")
.order_by(r.desc("age"), r.asc("name"))
Despite the document I've referenced above describes the JavaScript driver implementation, it will go also for other standard drivers (you're probably using Python or Ruby).

Related

Filter integer list in Thymeleaf

I am learning about Thymeleaf in Spring, and I am struggling with list fitlering.
The official Tutorial: Using Thymeleaf does not talk about collection filtering and projection, but I found out that Thymeleaf on Spring uses the Spring Expression Language.
This guide states the following:
The syntax of the selection (filtering) operator is : ${collection.?[property == value]}
The syntax of the projection (mapping) operator is : ${collection.![property]}
This is fine if I have a list of objects, for example a list of persons. Then I can perform things like that:
Selection (filtering): e.g., ${persons.?[age >= 18]} selects all persons of at least 18 years
Projection (mapping): e.g., ${persons.![name]} selects the name of every person
Question:
What if I do not have a list of objects (such as a list of persons) but a list of numbers or list of Strings? How can I perform selection (filtering) then? Things like numbers.?[>10] does not work.
After some more search, I found the answer in the Spring Expression Language documentation.
In 10.5.11 Variables the documentation states the #this and #root variables.
The variable #this is always defined and refers to the current evaluation object (against which unqualified references are resolved).
So, assuming I have a list numbers filled with integers, ${numbers.?[#this >= 10]} creates a new list that contains all numbers that are at least 10.

imap search syntax with multiple OR arguments

I am using Ruby 2.5.3 & the mail gem (2.7.1). I am structuring the IMAP search command to retrieve emails given a list of email addresses and various since dates. It is a logical OR of the search email addresses.
I am using this email_filter:
(OR (FROM a1#b.com SINCE 1-Oct-2018) (OR (FROM a2#b.com SINCE 10-Oct-2018) (OR (FROM a3#b.com SINCE 19-Oct-2018))))
which seems to be consistent with the RFC 3501 ABNR form.
The ruby code: to structure the search:
search_options = { count: no_emails_to_process, what: :first, order: :asc, keys: email_filter}
Mail.find(search_options) do |mail, imap, uid, attrs|
etc ...
It raised an error:
Error in IMAP command UID SEARCH: Missing argument
I assume the syntax isn't right because limiting the search to just one email address works fine.
I need some help.
OR takes two arguments, neither more nor less. OR a b works, (OR a b) works but(OR a) won't work. That would be a single-argument OR inside a single-argument AND. The parser is looking for the second argument to OR when it runs up against the ) that ends the list of arguments to AND. The last part of your query is (OR (FROM a3#b.com SINCE 19-Oct-2018)).
What you mean is probably OR (FROM a1#b.com SINCE 1-Oct-2018) OR (FROM a2#b.com SINCE 10-Oct-2018) (FROM a3#b.com SINCE 19-Oct-2018). In that expression, the first OR takes two arguments, which are an AND and another OR, and the second OR takes two arguments, both of which are ANDs.
(I agree that this difference between OR and AND is a bit strange.)

Pig:FLATTEN keyword

I am a little confused with the use of FLATTEN keyword in PIG.
Consider the below dataset:
tuple_record: {details: (firstname: chararray,lastname: chararray,age: int,sex: chararray)}
Without using the FLATTEN I can access a field (suppose firstname) like this:
display_firstname = FOREACH tuple_record GENERATE details.firstname;
Now, using the FLATTEN keyword:
flatten_record = FOREACH tuple_record GENERATE FLATTEN(details);
DESCRIBE gives me this:
flatten_record: {details::firstname: chararray,details::lastname: chararray,details::age: int,details::sex: chararray}
And hence I can access the fields present directly without dereferencing like this:
display_record = FOREACH flatten_record GENERATE firstname;
My questions related to this FLATTEN keyword is:
1) Which way among the two (i.e. with or without using FLATTEN) is the optimized way of achieving the same output?
2) Any special scenarios where without using the FLATTEN keywords, the desired output cant be achieved?
Totally confused; please clarify its use and in which all scenarios I shall use it.
Sometimes you have data in a bag or a tuple and you want to remove that level of nesting.
when you want to switch around your data on the fly and group by a particular field, you need a way to pull those entries out of the bag.
As per Pig documentation:
The FLATTEN operator looks like a UDF syntactically, but it is
actually an operator that changes the structure of tuples and bags in
a way that a UDF cannot. Flatten un-nests tuples as well as bags. The
idea is the same, but the operation and result is different for each
type of structure.
For more details check this link they have explained the usage of FLATTEN clearly with examples

Selecting multiple results from XQUERY query

I am trying to select multiple columns from a query, but so far, I can only manage to select one. So I'm basically stuck with either selecting one, or all of them.
Here's my expression, what I got so far, which select only (1) column:
let $y := doc("http://en.wikipedia.org/wiki/List_of_deaths_on_eight-thousanders")//table[preceding-sibling::h2//span[string() = "K2"]][1]
return $y/tr/td[2]/string()
I would love some explanation of how one would go about doing this, since there's almost no documentation of this lovely language.
How would you like the result to be returned? You could construct new elements, or concatenate strings. There are many ways that this could be accomplished.
Here's one way to get comma-separated values:
return $y/tr/fn:string-join( (td[2] | td[4]), ", " )
You can try it on zorb.io.
Update
(td[2] | td[4]) selects both elements, and passes them, as a sequence, to fn:string-join(). | is the XQuery union operator (and can be substituted for the keyword).
As far as documention, the functx site documents the standard library (all fn-prefixed functions), and has useful examples. And the specs are surprisingly readable.

using xpath to obtain complex values

Given the following, I'd like to extract VarVal1, VarVa5 and VarText where FixedVals are, well, fixed :)
<TypeA Attr1="VarVal1">
<TypeB Attr2="FixedVal2">
<TypeC Attr3="FixedVal3">
<TypeD Attr4="FixedVal4" Attr5="VarVal5">
VarText
</TypeD>
</TypeC>
</TypeB>
</TypeA>
Notice that the big problem for me is that the context is important. I want the complete pattern. There may be other TypeA nodes, but I'm not interested in their values unless they're followed by
<TypeB Attr2="FixedVal2">
<TypeC Attr3="FixedVal3">
<TypeD Attr4="FixedVal4" Attr5="VarVal5">
VarText
</TypeD>
</TypeC>
</TypeB>
In other words, what I'm interested in is a set of tripletts, each of them in the form of (VarVal1, VarVal5, VarText)
These XPath expressions:
//TypeA
[TypeB[#Attr2="FixedVal2"]
/TypeC[#Attr3="FixedVal3"]
/TypeD[#Attr4="FixedVal4"]]
/#Attr1
Then those already posted:
//TypeA
/TypeB[#Attr2="FixedVal2"]
/TypeC[#Attr3="FixedVal3"]
/TypeD[#Attr4="FixedVal4"]
/#Attr5
And
//TypeA
/TypeB[#Attr2="FixedVal2"]
/TypeC[#Attr3="FixedVal3"]
/TypeD[#Attr4="FixedVal4"]
You could also combine them with | union set operator. But depending on the host language, you should better select the TypeA elements you want (first expression with out last /#Attr1 part) and then query each of those to extract the remaining values.
I think you need a couple of queries for this (could be wrong though)
for VarVal1
//TypeA/#Attr1
for VarVal5
//TypeA
/TypeB[#Attr2="FixedVal2"]
/TypeC[#Attr3="FixedVal3"]
/TypeD[#Attr4="FixedVal4"]
/#Attr5
Think these should do the trick
EDIT - missed VarText!
//TypeA
/TypeB[#Attr2="FixedVal2"]
/TypeC[#Attr3="FixedVal3"]
/TypeD[#Attr4="FixedVal4"]

Resources