0

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??

2 Answers 2

0

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
Sign up to request clarification or add additional context in comments.

1 Comment

Well, thanks for taking the time for this, but since the expression is processed on the rethinkdb server, this is not helpful here.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.