0

I have below json in RethinkDB table

[{"pid": 0,
  "sk": [
  {
    "sid": 30,
    "et": 3
  },
  {
    "sid": 22,
    "et": 10
  },
  {
    "sid": 30,
    "et": 10
  }
  ],
"wc": [
  {
    "wid": 29,
    "et": 8
  },
  {
    "wid": 30,
    "et": 2
  },
],
"dom": [
  {
    "did": 7,
    "et": 2
  },
  {
    "did": 6,
    "et": 3
  }
],
"ex": 17,
"av": 12,
"lc": "FRA"
  }

Like this there are several thousands of rows in RethinkDB table.

My Objective is to search the data of sk, wc

For example: Input could be

"sk":[{"sid":21,"et":5},{"sid":21,"et":5}] Once filtered on above condition, the resultant dataset should again be filtered for wc field "wc":[{"wid":1,"et":7},{"wid":4,"et":5},{"wid":0,"et":7}]

I need the output records which were contained in the given input like in the table for example, sk:[{sid:2,et:8},{sid:3,et:6},{sid:3,et:7},{sid:4,et:9}] should be shown in output dataset if the input fields are below [{sid:3,et:7},{sid:4,et:9}]

I used below query when I have {sid:et} in one tuple:

 r.db('testdb').table('f_tab').
 filter(
 {
 "sk": [{"0":"8"},{"1":"5"},{"8":"5"},{"3":"8"},{"12":"4"}]
 }).filter(
 {
  "wc": [{"0":"7"},{"7":"9"},{"2":"6"},{"8":"4"},{"4":"7"}]
 }).getField('pid')

Now I have split the sid and et values for better management in server side code

Tried using r.row inside filter, but it doesn't work How can I filter based on my requirement in python ?

What is the best approach for performing nested fields search this way in perspective of performance ?

Satheesh Panduga
  • 779
  • 1
  • 8
  • 28

1 Answers1

1

Does this do what you want?

r.table('f_tab').filter(
  lambda row: r.expr([{'sid': 21, 'et': 5}, ...]).set_difference(row['sk']).is_empty()
).filter(
  lambda row: r.expr([{'wid': 22, 'et': 6}, ...]).set_difference(row['wc']).is_empty()
)['pid']
mlucy
  • 5,046
  • 1
  • 14
  • 20
  • Thanks a lot mlucy! It works.. I think lamba is not being rendered in Rethinkdb Data explorer.. But works within my python code (Django) – Satheesh Panduga Nov 19 '15 at 07:10
  • How can I check for et greater than or equal to to its value ? – Satheesh Panduga Nov 19 '15 at 07:27
  • For example: I want to check my table which has all the records that are having (et ge 5) -> if my table record has {'sid':21,'et':7} should also be returned along with {'sid':21,'et':5} if it is there.. Could you pls help me on how to use ge inside expression for et values – Satheesh Panduga Nov 19 '15 at 07:29
  • You can use `contains` for that. Something like `r.table('f_tab').filter(lambda row: row['sk'].contains(lambda x: x['et'] >= 5))`. (In the data explorer you have to write JS instead of Python, so `r.table('f_tab').filter(function(row) { return row('sk').contains(function(x) { return x('et').ge(5); }); })` – mlucy Nov 19 '15 at 10:08
  • Thanks a lot.. but it throws error when I try to use both sid and et like this one.. r.table('f_tab').filter(lambda row: row['sk'].contains((lambda x: x['et'] >= 7)&(lambda x: x['sid'] == 1)))['pid'].run(t) – Satheesh Panduga Nov 19 '15 at 10:58
  • Also tried:.. r.table('f_tab').filter(lambda row: row['sk'].contains([{lambda x: x['et'] == 7,lambda x: x['sid'] == 1}]))['pid'].run(t) .. so, that I can add multiple such conditions for different sid's.. this also throws error - data type – Satheesh Panduga Nov 19 '15 at 10:59
  • You want `lambda x: (x['et'] >= 7) & (x['sid'] == 1)` (or `x('et').ge(y).and(x('sid').eq(1))` in JS). – mlucy Nov 19 '15 at 20:08
  • Thanks a lot mlucy :).. I'm checking it now – Satheesh Panduga Nov 19 '15 at 20:11
  • Hi mlucy, How can I use greater than equal condition for et values for each set of sid from (sid,et) in this condition ? I have created another question for this, http://stackoverflow.com/questions/33977395/how-to-apply-greater-than-equal-in-python-lambda-expression-in-rethinkdb . Could you kindly check it please ? – Satheesh Panduga Nov 28 '15 at 23:02