I have a Flask app that queries my self-managed MongoDB.
I've created an index on two fields so that I can perform a text search on it.
The search is working fine, but it is only matching full words.
For example, for the phrase "i am halfminded", if I perform a search with the keyword halfminded it works but doesn't work for half.
This is what i am using:
def get_paginated_data(self, page, query):
skip = (page - 1) * self.PER_PAGE
data = (
self.collection.find({"$text": {"$search": query}})
.skip(skip)
.limit(self.PER_PAGE)
)
return list(data)
I have tried using the below code as well:
def get_paginated_data(self, page, query):
skip = (page - 1) * self.PER_PAGE
data = (
self.collection.find({"$text": {"$regex": f"/.*{query}/"}})
.skip(skip)
.limit(self.PER_PAGE)
)
return list(data)
but this throws the following error:
pymongo.errors.OperationFailure: Missing expected field "$search"
Can anyone advise on how i can get the partial text search to work.
searchkeyword.$textand text index? You can simply use a normal index and do the regex search like this, if a substring search is what you are looking for