Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Invalid operator: $regex #184

Closed
thatshailesh opened this issue Oct 1, 2019 · 7 comments
Closed

Error: Invalid operator: $regex #184

thatshailesh opened this issue Oct 1, 2019 · 7 comments

Comments

@thatshailesh
Copy link

thatshailesh commented Oct 1, 2019

Facing issue while doing something like

await test.find(
        { 
            selector: { name: {"$regex": /cat/}}
        }
    )

Expected Behavior

it should return names containing cat

Current Behavior

Current error stack

 Error: Invalid operator: $regex
    at Request._callback (/node_modules/nano/lib/nano.js:154:15)
    at Request.self.callback (node_modules/request/request.js:185:22)
    at Request.emit (events.js:198:13)
    at Request.<anonymous> (/node_modules/request/request.js:1161:10)
    at Request.emit (events.js:198:13)
    at IncomingMessage.<anonymous> (/node_modules/request/request.js:1083:12)
    at Object.onceWrapper (events.js:286:20)
    at IncomingMessage.emit (events.js:203:15)
    at endReadableNT (_stream_readable.js:1129:12)

Possible Solution

Steps to Reproduce (for bugs)

1.Just use $regex, it will throw error

Context

I am trying to do wildcard search based on name in CouchDB

Your Environment

  • Version used: 8.0.1
  • Browser Name and version: Chrome
  • Operating System and version (desktop or mobile): OSX
@glynnbird
Copy link
Contributor

glynnbird commented Oct 1, 2019

Try expressing your regular expression as a string. Nano doesn't understand JavaScript regular expressions - it passes a regular expression string to CouchDB for processing e.g.

selector: { name: {"$regex": "^cat" }}

should find names beginning with "cat".

@thatshailesh
Copy link
Author

thatshailesh commented Oct 2, 2019

Thank you very much, it worked :)

Can you please help me with indexing, I am working on search functionality where I will be doing a fuzzy search over name of an entity so i need to search for occurrences

I tried adding the index and i keep getting this error Reason: (required index service unavailable) text
And I am able to create an index only for json type but the field is text type
Using $regex operator and it seems very slow

What would be the best practice to implement a search in CouchDB ?
Should I use views? but its range based only

@glynnbird
Copy link
Contributor

First of all the regex operator is very slow and an index can't be used to speed it up. It will inevitable cause CouchDB to do a full database scan (looking through each document body in turn) to get your answer. If you are looking for "all documents containing 'cat'" then you need to index each word in the string in question, which is what type: 'text' indexes are for. Unfortunately they are not baked into CouchDB today (stay tuned for CouchDB 3.0 which should have free-text search baked in).

Without the Lucene-based free-text search index, you're a bit stuck. You could roll your own index with a MapReduce index like so?:

function(doc) { 
  // break "str" into words
  var words = doc.str.split(/\W/); 
  for(var i in words) { 
     if (words[i]) { 
        // create an index with the lower-cased word as the key
       emit(words[i].toLowerCase(), null)
      }
   }
}

@thatshailesh
Copy link
Author

Thanks for sharing

So after adding this view I can use find directly ?

@ghost
Copy link

ghost commented Nov 19, 2020

How to pass regex flags then? Suppose I need case insensitive search.

@EddyTheDove
Copy link

@Cherviakov you can do it like this:

{ "firstName" : { "$regex": `${ RegExp(firstName.toLowerCase(), "i") }` }}

@ghost
Copy link

ghost commented Mar 13, 2021

Thanks @EddyTheDove

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants