How to assign custom score in Atlas Search? - mongodb

So, I am working on implementing autocomplete feature on MongoDB atlas search, and I have a field in my document which has the score of the particular search-keyword in the document.
Like, This is one of the field
Primary_Search Hello
Relevant_Meaning Hi
Search_Score 10.66
and this is another
Primary_Search Hi
Relevant_Meaning Hi
Search_Score 12.66
So both are different searches but have same relevant meaning so, in this case I will show the result with my Score-System which has higher Score.
P.S: I have indexed the collection with Primary_Search and Relevant Meaning as autocomplete type and Search_Score as Number type

Related

MongoDB get all documents, sort by a field and add ordering field based on the sorting

I am trying to sort the result of my mongoDB query and add a ranking based on that sorting. Currently I only call .find().sort({total: 1}) and this gives me the correct ordering of the documents. But is it possible to "add a field" based on that sorting (basically a ranking field, starting from 1 and counting up)? I tried googling but didnt found anything that suits for this purpose.
Thanks in advance.

How to check if firestore document field CONTAINS a string [duplicate]

This question already has answers here:
How to search text in Flutter using Firestore
(3 answers)
Google Firestore: Query on substring of a property value (text search)
(25 answers)
Closed 6 months ago.
Basically, I want to use UISearchBar to filter out documents in a collection in a tableview. When the user finishes putting text inside the search bar, the code should fire a query to get the documents only if it contains given text.
For example, User searches: "Th"
What could output in the uitableview:
this
the
them
one thing
breath
This should work no matter where "Th" is within the document field, however I am unsure how to implement this. Like e.g. should I use .whereField(field, isGreaterThan: searchedText)? I don't really know.
Cloud Firestore currently doesn't support native indexing or search for text fields in documents. To enable full text search of your Cloud Firestore data, use a dedicated third-party search service such as Algolia
However, As mentioned in similar thread it supports two query operators for searching text fields which starts substring
I.e. Using isEqualTo, isGreaterThanOrEqualTo and isLessThanOrEqualTo
For example, to search for all documents where name starts with “Th”, you'd use collectionRef.where("name", isGreaterThanOrEqualTo: "Th").where("name", isLessThanOrEqualTo: "Th\uf7ff"). The \uf7ff here is just the last known Unicode character, so that the query stops returning results after dealing with every “Th”
Note: There is no way to search for documents which contain a substring, nor for those whose text ends with a substring.
For other alternative solution you can refer this link

How to skip elements before certain document

Background:
I am writing a mobile application which has lazy loading page. My backend is using go and mongodb with mongo-go driver. There are 10 elements on that page and i want to get next ten when i scroll to the bottom. I am planning to send ObjectID (_id) as request query parameter and get next ten elements starting from the index of id + 1.
I write what i want in mongo shell "language" so that more people understand what i want and can help in shell syntax.
Is there a way to get index of the document by it's _id or may be i can get skip until it in skip().
something like db.collection.find().skip(idOfDocument+1).limit(10)
I found the answer here.
nextDocuments = db.collection.find({'_id'> last_id}).limit(10)

Searching a value in a specific field in MongoDB Full Text Search

(MongoDB Full Text Search)
Hello,
I have put some fields in index and this is how I could search for a search_keyword.
BasicDBObject search = new BasicDBObject("$search", "search_keyword");
BasicDBObject textSearch = new BasicDBObject("$text", search);
DBCursor cursor = users.find(textSearch);
I don't want search_keyword to be searched in all the fields specified in the index. *Is there any method to specify search_keyword to be searched in specific fields from the index??*
If so, please give me some idea how to do it in Java.
Thank you.
If you want to index a single field and search for it then it is the way it works by default. Lets say you want to index the field companyName. When you perform $text search on this collection, only the data from the companyName field will be used because you only included that field in your index.
Now the second scenario, your $text index includes more than one field. In this case you cannot limit the search to only look for values indexed from a specific field. The $text index is constructed on the collection level and a collection can have at most one $text index. Your option to limit search on specific field in this case may be to use regex instead.
MongoDB has the flexibility to fulfil requirements of other scenarios, but you can also evaluate using other technologies if your application is heavily search-driven and you are primarily after a full-text search engine for locating documents by keyword with a rich query syntax. ElasticSearch might be an alternative here. It really depends on the type of the application and your needs.

MongoEngine search index

I'm trying to implement an inverted-index search engine with MongoDb (MongoEngine) where terms in Posts are assigned weights and then used as embedded documents like such:
class Term(db.EmbeddedDocument):
t = db.StringField()
weight = db.FloatField()
class Post(db.Document):
terms = db.ListField(db.EmbeddedDocumentField(Term))
Then given a term, I can find the Posts that contain the term using this query:
post_list = Post.objects(terms__t=term)
However, this returns a list of Posts, but how can I find the weight of the term for each returned Post without having to iterate through the list of embedded terms looking for the term? Is there a way to query the Posts to automatically return the weight for any returned Posts as well?
Also would appreciate if anyone has any better methods of implementing a search engine in MongoDB?
Thanks!
MongoDB supports a basic text index see: http://docs.mongodb.org/manual/core/index-text/ This is a better way to store and search against documents, especially if you want a score for the match.
You'd have to call the command manually as its not currently implemented in MongoEngine.