Start a new topic

Creating a search function

Hi there everyone,



I'm creating a search function right now, but I can't get it to work.

In the end I'd like to check if a search term is part of multiple fields and do a full text search in a description but right now I can't get it to work on one field.



For example I have a table called people and I want to search for a person with the name Glenn.

So I do a request that looks like this: /people?query={"name":"^/Glenn/"} but it returns an empty array.

I looked into the MongoDB documentation but all the functions I find there end up giving me errors in my query so the statement above is really just a try out that happens to not give an error.



Can anyone help me out? Because right now I can't seem to find the proper syntax.



Thanks in advance!

Hi, I'm glad you figured it out!



One thing I wanted to mention is that you should think carefully about implementing this type of query, the frequency in which it will be executed, and its potential implications. As you've discovered, Kinvey does not yet support "proper" full text search (due in a large part to the limitations of our storage solution, mongodb, and the lack of maturity in its relatively recent support for full text search). The query you outlined will work (given that using anchored regex searches fits your use case), but since it is performing a fairly intensive query (regex) using an `$or` over multiple properties, the query may slow down as the size of your collection grows, especially without careful indexing.



For this reason, I recommend that you carefully consider your use case, and think about whether there is room for a more efficient query, or even another approach. It may be that you eventually decide that this is indeed the best approach, but I do recommend that you take the time to think about it first.
Fixed it by using the following query:



?query={"$or":[{"name":{"$regex":"^SEARCHTERM"}},{"description":{"$regex":"^SEARCHTERM"}},{"tags":{"$regex":"^SEARCHTERM"}}]}
Login or Signup to post a comment