how do I create a “like” filter view in couchdb

前端 未结 7 815
北海茫月
北海茫月 2020-12-30 03:25

Here\'s an example of what I need in sql:

SELECT name FROM employ WHERE name LIKE %bro%

How do I create view li

7条回答
  •  悲&欢浪女
    2020-12-30 04:09

    You could emit your documents like normal. emit(doc.name, null); I would throw a toLowerCase() on that name to remove case sensitivity.

    and then query the view with a slew of keys to see if something "like" the query shows up.

    keys = differentVersions("bro"); // returns ["bro", "br", "bo", "ro", "cro", "dro", ..., "zro"]
    $.couch("db").view("employeesByName", { keys: keys, success: dealWithIt } )
    

    Some considerations

    1. Obviously that array can get really big really fast depending on what differentVersions returns. You might hit a post data limit at some point or conceivably get slow lookups.

    2. The results are only as good as differentVersions is at giving you guesses for what the person meant to spell. Obviously this function can be as simple or complex as you like. In this example I tried two strategies, a) removed a letter and pushed that, and b) replaced the letter at position n with all other letters. So if someone had been looking for "bro" but typed in "gro" or "bri" or even "bgro", differentVersions would have permuted that to "bro" at some point.

    3. While not ideal, it's still pretty fast since a look up in Couch's b-trees is fast.

提交回复
热议问题