Flattening objects in ReQL - rethinkdb

I have a table with records like this
{
a:{aa:"aa1",aaa:"aaa1"},
b:"b1",
...
},
{
a:{aaa:"aaa2"},
b:"b2",
...
},
{
a:{aa:"aa3"},
b:"b3",
...
},
and I want to extract aa and b values, i.e. to something like
{aa:"aa1",b:"b1"},
{aa:undefined,b:"b2"},
{aa:"aa3",b:"b3"},

You can write something like:
o = {a: {aa: "aa1", aaa: "aaa1"}, b: "b1"}
r.expr(o).do({a: r.row('a')('aa').default(null), b: r.row('b').default(null)})

Related

Elasticsearch with query => 1 Doc with array of 2 values Return 2 hits

Hi I need help writing query.
doc in ES is
{ A: "A", B: ["1","2"] }
result from query should be: hits(2x)
{ A:"A" , B: "1"},
{ A:"A" , B: "2" }
is this possible?

Group list of objects/AR relation by user_id

I have a list of objects which is actually AR Relation. My object has these fields :
{
agreement_id: 1,
app_user_id: 1,
agency_name: 'Small business 1'
..etc..
},
{
agreement_id: 2,
app_user_id: 1,
agency_name: 'Small business 2'
..etc..
}
I m representing my object as a Hash for easier understanding. I need to map my list of objects to format like this :
{
1 => [1,2]
}
This represents a list of agreement_ids grouped by the user. I always know which user I m grouping on. Here is what I've tried so far :
where(app_user_id: user_id).where('...').select('app_user_id, agreement_id').group_by(&:app_user_id)
This gives me the structure what I want but not exactly the data that I want, here is an output of this :
{1=>
[#<Agreement:0x6340fdbb agreement_id: 1, app_user_id: 1>,
#<Agreement:0x91bd4dd agreement_id: 2, app_user_id: 1>]
}
I've also thought I was going to be able to do this with map method, and here is what I tried :
where(app_user_id: user_id).where('....').select('app_user_id, agreement_id').map do |ag|
{ ag.app_user_id => ag.agreement_id }
end.reduce(&:merge)
But it only produces the mapping with the last agreement_id like this :
{1=>2}
I've tried some other things not worth mentioning. Can anyone suggest a way that would make this work?
This might work :
where(app_user_id: user_id)
.where('...')
.select('app_user_id, agreement_id')
.group_by(&:app_user_id).map{|k,v| Hash[k, v.map(&:agreement_id)]}
Try this one
where(app_user_id: user_id).
where('...').
select('app_user_id, agreement_id').
map { |a| [a.app_user_id, a.agreement_id] }.
group_by(&:first)

ElasticSearch query

Id Field Data
--------------------------------------------------
1 A Data1
2 B Data2
2 C Data3
3 C Data4
4 G Data5
5 F Data6
5 B Data7
I want with a single query (for pagination) to fetch all fields "Data" with an Id containing at least one Field with a value of A, B, C. In the previous example I should retrieve all except "Data5" because Id 4 is the only one that does not contain A, B or C . Expected result: Data1, Data2, Data3, Data4, Data6, Data7.
Thanks, I am newbie with ES and I made a mess with must, should, filters, ....
The following might be helpful to you.
{
"query": {
"bool": {
"must_not": [
{
"terms": {
"Field": ["G"]
}
}
]
}
}
}
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html

Rethinkdb execute multiple avg in one query

I have a review table with multiple number columns. I would like to count he avg of all columns in one query.
So if the table looks like:
{
foo : 2,
bar : 5,
foobar : 10
},
{
foo : 4,
bar : 3,
foobar : 12
}
then i would like to get the avg for each column in one query. I know I can do:
r.table('stats' ).avg( 'foo' )
on each column but I would like to do this in just one query and map into into just one object.
Any ideas on how to do this?
You can use map with reduce (if every record in table has all 3 fields):
r.table("stats").map(function(row){
return {foo : row("foo"), bar : row("bar") , foobar : row("foobar"), count : 1};
}).reduce(function(left, right){
return {foo : left("foo").add(right("foo")), bar : left("bar").add(right("bar")), foobar : left("foobar").add(right("foobar")), count : left("count").add(right("count"))};
}).do(function (res) {
return {
foo: res('foo').div(res("count")),
bar: res('bar').div(res("count")),
foobar: res('foobar').div(res("count"))
};
})
If record can have not all fields, you can separate count in map operation for each field, and then in do use it depending on field.

Lua table access efficiency

I have a question about accessing data in a Lua table.
Say, there is a large Lua table like following:
tbl = {
{
blockIdx = 5,
key1 = "val1",
key2 = "val2",
...
},
{
blockIdx = 30,
key1 = "val11",
key2 = "val12",
...
},
{
blockIdx = 83,
key1 = "val21",
key2 = "val22",
...
},
...
}
And now I want to find one of the block that blockIdx is , for example, 38.
So normally, I would like to use for to find the block:
for k,v in pairs(tbl) do
if v.blockIdx == 38 then
blahFunction(v)
end
end
But I don't think it is a good idea especially for large table.
So I modify the table a bit:
tbl = {
[5] = {
key1 = "val1",
key2 = "val2",
...
},
[30] = {
key1 = "val11",
key2 = "val12",
...
},
[83] = {
key1 = "val21",
key2 = "val22",
...
},
...
}
Then I can easily access my block with one line:
blahFunction(tbl[38])
So my question is, is there any performance different between two method?
Maybe doing tbl[38] actually did a for loop inside Lua?
Or just like an array in C/C++ we can direct access memory using [ ] without for loop,
witch obviously has much better performance.
The performance is different, the second method is more efficient.
Internally, a Lua table contains an array part and a hash part, if the table is a sequence, then the sequence part is implemented by the array part. But the table in your second example is not a sequence, it's probably implemented by the hash part. The performance in this case is not like accessing arrays in C/C++, but like accessing a hash, which is still pretty fast.
So in conclusion, the second piece of code is faster, because it doesn't iterate through the elements like in your first example.

Resources