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

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

Related

How to assign custom score in Atlas Search?

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

Autocomplete and text search memory issues in apostrophe-cms: need ideas

I’m having trouble to use the text search and the autocomplete because I have a piece with +87k documents, some of them being big (~3.4MB of text).
I already:
Removed every field from the text index, except title , searchBoost and seoDescription ; these are the only fields copied to highSearchText and the field lowSearchText is always set to an empty string.
Modified the standard text index, including the fields type, published and trash in the beginning of it. I'm also modified the queries to have equality conditions on these fields. The result returned by the command db.aposDocs.stats() shows:
type_1_published_1_trash_1_highSearchText_text_lowSearchText_text_title_text_searchBoost_text: 12201984 (~11 MB, fits nicely in memory)
Verified that this index is being used, both in ‘toDistinc’ query as well in the final ‘toArray’ query.
What I think is the biggest problem
The documents have many repeated words in the title, so if the user types a word present in 5k document titles, the server suffers.
Idea I'm testing
The MongoDB docs says that to improve performance the entire collection must fit in RAM (https://docs.mongodb.com/manual/core/index-text/#storage-requirements-and-performance-costs, last bullet).
So, I created a separate collection named “search” with just the fields highSearchText (string, indexed as text) and highSearchWords (array, also indexed), which result in total size of ~ 19 MB.
By doing the same operations of the standard apostrophe autocomplete in this collection, I achieved much faster, but similar results.
I had to write events to automatically update the search collection when the piece changes, but it seems to work until now.
Issues
I'm testing this search collection with the autocomplete. For the simple text search, I’m just limiting the sorted response to 50 results. Maybe I'll have to use the search collection as well, because the search could still breaks.
Is there some easier approach I'm missing? Please, any ideas are welcome.

How do I make a mongodb find where a field(object) includes all properties from my query (names and values)? [duplicate]

This question already has answers here:
MongoDB - how to query for a nested item inside a collection?
(3 answers)
Closed 9 years ago.
Consider my query to be: {cheese:"Cheddar"} and I have the following collections:
{vegetable:"Lettuce", cheese:"Cheddar"}, {cheese:"Blue"}, {milk:"Chocolate}, {cheese:"Cheddar"}
How do I make a find that returns me all collections that include cheese:Cheddar?
The result would be {vegetable:"Lettuce", cheese:"Cheddar"}, {cheese:"Cheddar"} but right now it fives me just {cheese:"Cheddar"}. From what I investigated I only found tokens to work with arrays.
I do NOT know the name of the property is cheese, nor do I know if there are any other ingredients.
I am looking for a way to get documents from a collection, where the query is included in a field, by the names of the properties in the query and the respective values.
Using db.collection.findOne({cheese:"Cheddar"}) you will get as a result only one document, maybe {cheese:"Cheddar"} or maybe {vegetable:"Lettuce", cheese:"Cheddar"}, the first one that MongoDB finds depending on the _id field. If what you want is getting both, you should use db.collection.find({cheese:"Cheddar"}).

Setting up a keyword query [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Full-text search in NoSQL databases
I am somewhat new to database querying and i was wondering what the best way to do this would be. I have a database of articles and want my users to be able to search them by keywords in the tittle, i.e allowing them to type a string and all the tittles containing this string would be selected by the query.
What would the most efficient way to do this be?
And if i want to avoid strings such as "the" or "it" from being selected?
I am using mongoid in case that helps.
Thanks in advance
If your title is stored as a string you could use the regular expression search supported by mongodb. For example:
db.articles.find( { title : /acme.*corp/i } );
Mongodb use PCRE for regular expression. To exclude certain words from the search I would recommend an application side check or you can use the $nin operator. For more info have a look here.

'SQL 'like' statement in mongodb [duplicate]

This question already has answers here:
How to query MongoDB with "like"
(45 answers)
Closed 7 years ago.
Is there in MongoDB/mongoose 'like' statement such as in SQL language?
The single reason of using is a implementation of full-text searching.
MongoDB supports RegularExpressions.
Two ways we can implement regular expression using java API for mongodb:
Expression 1:
I need to search start with that string in document field
String strpattern ="your regular expression";
Pattern p = Pattern.compile(strpattern);
BasicDBObject obj = new BasicDBObject()
obj.put("key",p);
Example : (consider I want to search wherever name start with 'a')
String strpattern ="^a";
Pattern p = Pattern.compile(strpattern);
BasicDBObject obj = new BasicDBObject()
obj.put("name",p);
Expression 2:
I need to search multiple words in one field , you can use as follows
String pattern = "\\b(one|how many)\\b";
BasicDBObject obj = new BasicDBObject();
//if you want to check case sensitive
obj.put("message", Pattern.compile(pattern,Pattern.CASE_INSENSITIVE));
Although you can use regular expressions, you won't get good performance on full text search because a contains query such as /text/ cannot use an index.
A begins-with query such as /^text/ can, however, use an index.
If you are considering full text search on any large scale please consider MongoDB's Multi Key Search feature.
Update
As of MongoDB v3.2 you can also use a text index.
Consider making a script at application level that transforms your data to tokens (words). Then treat tokens as tags, build an index on those tokens, and then search the tokens like searching for tags. This is like creating an inverted index.
For way better search capabilities on text consider using Lucene instead of MongoDB.
Use regex: /^textforesarc$/i. Just don't forget to say goodbye to performance :).
Simulating regex search with like will not hit custom defined index. Only starts with is supported for now.