Groovy/Grails : How to groupBy multiple keys combined? - grails

I have a list and i want to group by all three keys, i refer to How to group a list of list.
def given = [
[Country:'Japan',Flag:'Yes',Event:'New Year'],
[Country:'china',Flag:'No',Event:'Spring Festival'],
[Country:'uk',Flag:'No',Event:'National Holiday'],
[Country:'us',Flag:'Yes',Event:'Labour Day'],
[Country:'us',Flag:'Yes',Event:'New Year'],
[Country:'uk',Flag:'Yes',Event:'Memorial Day']
]
We can group by:
def mapped = given.groupBy {
[(it["Country"]) : it["Flag"] ] }
How can I group by [(it["Country"]) : it["Flag"] : it["Event"] ] ?
expected results : [['Japan':['Yes':[NewYear]]]:[['Country':'Japan', 'Flag':'Yes', 'Event':'New Year']] , ..

What this is good for, I don't understand. #dmahapatro 's solution gives a much more handlable result. In your example you just want to have a recursive map as key for the group by. I have my strongest doubts, that this will handle the actual grouping case well.
def given = [
[Country:'Japan',Flag:'Yes',Event:'New Year'],
[Country:'china',Flag:'No',Event:'Spring Festival'],
[Country:'uk',Flag:'No',Event:'National Holiday'],
[Country:'us',Flag:'Yes',Event:'Labour Day'],
[Country:'us',Flag:'Yes',Event:'New Year'],
[Country:'uk',Flag:'Yes',Event:'Memorial Day']
]
println given.groupBy{ [(it.Country): [(it.Flag): [it.Event]]] }.inspect()
//=> [['Japan':['Yes':['New Year']]]:[['Country':'Japan', 'Flag':'Yes', 'Event':'New Year']], ...

given.groupBy( { it.Country }, { it.Flag }, { it.Event } )
A method taking 3 closures as arguments.

Related

How to do CosmosDB JOIN to get an array of Column values

I have following data in cosmosDb
[
{
accountId : "12453"
tag : "abc"
},
{
accountId : "12453"
tag : "bcd"
},
{
accountId : "34567"
tag : "qwe"
},
{
accountId : "34567"
tag : "xcx"
}
]
And desired output is something like this
[
{
accountId : "123453"
tag : {
"abc",
"bcd"
},
{
accountId : "34567"
tag : {
"qwe",
"xcx"
}
]
I tried Join, array and group by in multiple ways but didn't work.
Below query gives similar output but rather than count I am looking for an array of tags
select c.accountId, count(c.tag) from c where c.accountId = "12453" group BY c.accountId
Also tried below query
select c.accountId, ARRAY(select c.tag from c IN f where f.accountId = "12453") as tags FROM f
This doesn't return any tag values and I get below output but I need distinct accountId and when I try DISTINCT on accountId, it gives an error.
[
{
accountId : "12,
tag : []
}
........
]
Can someone please help with correct query syntax
As M0 B said, this is not supported. Cosmos DB can't combine arrays across documents. You need to handle this on your client side.

Rails array style nested parms

So I have objects that I'm trying to pass to a json-acccepting restful interface using parms and #connection, but the source object and the target are in different formats, so I need to rename them.
So I'm trying to do something like this:
parms = {
:foo => anBunchOfBars.bar[
:barID => bar.Identifier
:bartab => bar.finances.owed
:barbell => bar.equipment.first
]
}
... to generate an outgoing JSON similar to the following:
{
"foo": [
{
"barID": "Irish Pub",
"bartab": "30 Yen",
"barbell": "Of the ball."
},
{
"barID": "One Gold Bar",
"bartab": "100 cents",
"barbell": "fry."
}
]
}
But I can't find the proper syntax. Examples I've found seem to only cycle through a core list and leave out naming items on nested traits (or skip nested traits entirely), and I haven't seen anything that shows how to pull values from a looped item.
What's the proper syntax to format the parms value?
Assuming that anBunchOfBars is an array of bars:
bars = anBunchOfBars.map do |bar|
{
barID: bar.Identifier
bartab: bar.finances.owed
barbell: bar.equipment.first
}
end
params = { foo: bars }

Groovy domain class find with only provided filter

Hello
I need to perform Domain class filtering in Groovy for provided filter fields.
Here is code sample:
User.findAll(name: filter.name, age: filter.age, department: filter.department)
Is there exists some syntax sugar to help me to validate if for example filter.name is not provided e.g. null or empty - do not filter by this field. Thanks.
Abs has right. Here is an example with if-statements for not provided filter fields:
User.createCriteria().list {
if (filter.name) eq("name", filter.name)
if (filter.age) eq("age", filter.age)
if (filter.department) eq("department", filter.department)
}
you can apply the sugar right on the arguments map (assuming filter is a Map):
def args = filter.collectEntries{ k, v ->
[ 'age', 'name', 'department' ].contains( k ) && v ? [ (k):v ] : Collections.EMPTY_MAP
}
def users = User.findAll args

ElasticSearch writing query for priority search

I am new to elastisearch and I just set it up and tried default search. I am using elasticsearch rails gem. I need to write custom query with priority search (some fields in table are more important then others, etc. title, updated_at in last 6 months...). I tried to find explanation or tutorial for how to do this but nothing seems understandable. Can anyone help me with this, soon better.
Never having used the ruby/elasticsearch integration, it doesn't seem too hard... The docs here show that you'd want to do something like this:
client.search index: 'my-index', body: { query: { match: { title: 'test' } } }
To do a basic search.
The ES documentation here shows how to do a field boosted query:
{
"multi_match" : {
"query" : "this is a test",
"fields" : [ "subject^3", "message" ]
}
}
Putting it all together, you'd do something like this:
client.search index: 'my-index', body: { query: { multi_match : {
query : "this is a test",
fields : [ "subject^3", "message" ]
} } }
That will allow you to search/boost on fields -- in the above case, the subject field is given 3 times the score of the message field.
There is a very good blog post about how to do advanced scoring. Part of it shows an example of adjusting the score based on a date:
...
"filter": {
"exists": {
"field": "date"
}
},
"script": "(0.08 / ((3.16*pow(10,-11)) * abs(now - doc['date'].date.getMillis()) + 0.05)) + 1.0"
...
I have done in php, Never used the gem from Ruby on rails. Here you can give the priority for the fields using the caret (^) notation.
Example:- Suppose if we have fields namely name, email, message and address in table and the priority should be given for the name and message then you can write as below
> { "multi_match" : {
> "query" : "this is a test",
> "fields" : [ "name^3", "message^2".... ] } }
Here name has 3 times higher priority than other fields and message has got 2 times higher priority than other fields.

Neo4j and json creating multiple nodes with multiple params

I tried many things but of no use.
I have already raised a question on stackoverflow earlier but I am still facing the same issue.
Here is the link to old stackoverflow question
creating multiple nodes with properties in json in neo4j
Let me try out explaining with a small example
This is the query I want to execute
{
"params" : {
"props" : [
{
"LocalAsNumber" : 0,
"NodeDescription" : "10TiMOS-B-4.0.R2 ",
"NodeId" : "10.227.28.95",
"NodeName" : "BLR_WAO_SARF7"
}
]
},
"query" : "MATCH (n:Router) where n.NodeId = {props}.NodeId RETURN n"}
For simplicity I have added only 1 props array otherwise there are around 5000 props. Now I want to execute the query above but it fails.
I tried using (props.NodeId}, {props[NodeID]} but everything fails.
Is it possbile to access a individual property in neo4j?
My prog is in c++ and I am using jsoncpp and curl to fire my queries.
If you do {props}.nodeId in the query then the props parameter must be a map, but you pass in an array. Do
"props" : {
"LocalAsNumber" : 0,
"NodeDescription" : "10TiMOS-B-4.0.R2 ",
"NodeId" : "10.227.28.95",
"NodeName" : "BLR_WAO_SARF7"
}
You can use an array of maps for parameter either with a simple CREATE statement.
CREATE ({props})
or if you loop through the array to access the individual maps
FOREACH (prop IN {props} |
MERGE (i:Interface {nodeId:prop.nodeId})
ON CREATE SET i = prop
)
Does this query string work for you?
"MATCH (n:Router) RETURN [p IN {props} WHERE n.NodeId = p.NodeId | n];"

Resources