Reputation: 13
I am trying to create an index in RethinkDB. The documents look like this:
{ "pinyin" : "a1 ai3"}
To make searching easier, I would like to preprocess the index entries and remove spaces and numbers, the entry thus should simply be "aai" in this case. What I tried are various variants of the following:
r.index_create('pinyin', lambda doc: doc['pinyin'].replace("1", "")).run()
This is a most simple case to build from, but even here I get an error
Expected 2 arguments but found 3 in:
r.table('collection').index_create('pinyin', lambda var_7: var_7['pinyin'].replace('1', ''))
It's obvious that I do not understand what's going on. Can anybody help? I gather that the lambda expression has to follow python syntax, but since it will be used on the server has to be JavaScript??
Upvotes: 0
Views: 40
Reputation: 13
OK, I figured it out and am posting here for others who might run into the same problem. This is how the index can be created in the data explorer of the rethinkdb admin console:
r.db("dics").table("collection").indexCreate('pinyin', r.row("pinyin").split("").filter(function(char) { return r.expr(["1", "2", "3", "4", " "]).contains(char).not(); }).reduce(function(a, b) { return a.add(b); }))
A corresponding python version would use an anonymous function with lambda in the filter part.
Upvotes: 0
Reputation: 82785
I do not have experience using rethinkdb. But if you would like to remove all numbers and space from a string value the below snippet might help.
import re
doc = { "pinyin" : "a1 ai3"}
removeIntSpace = lambda doc: re.sub("\d", "x", doc['pinyin'].replace(" ", ""))
print removeIntSpace(doc)
#r.index_create('pinyin', removeIntSpace(doc)).run()
Output:
axaix
Upvotes: 0