Hi I'm trying to retrieve data from FireBase using the input by the user in the search bar.
The db struture is like this:
DB struture
Where every doc inside the collection "catalog" have one aditional collection named "recipes" and inside "recipes" are recipes with titles.
Example DB struture
How I can compare the input of the user with all recipes inside of all docs?
Currently using this code it works for 1 doc:
Code
If you want true full text search capabilities you will need to use a tool designed for that problem like Elastic Search, Typesense or Algolia.
Each of these are standalone services that can be used, but have very seamless integrations when using them as part of Firebase Extensions. Extensions will however need a credit card on file to be enabled.
Note that each of these may have cost implications, and limitations on how to structure your database. Algolia for example requires your text searchable entries to be in one collection.
You might then use a Firestore structure as follows:
foodTypes > meat > recipes > <recipe Id>
where meat and recipes are sub collections.
Related
I am new to MongoDB. And I have the following issue on currently developing web application.
We have an application where we use mongoDB to store data.
And we have an API where we search for the document via text search.
As an example: if the user type “New York” then the request should send the all the available data in the collection to the keyword “New York". (Here we call the API for each letter typed.) We have nearly 200000 data in the DB. Once the user searches for a document then it returns nearly 4000 data for some keywords. We tried with limiting the data to 5 – so it returns the top 5 data, and not the other available data. And we tried without limiting data now it returns hundreds and thousands of data as I mentioned. And it causes the request to slow down.
At Frontend we Bind search results to a dropdown. (NextJs)
My question:
Is there an optimizing way to search a document?
Are there any suggestions of a suitable way that I can implement this requirement using mongoDB and net5.0?
Or any other Implementation methods regarding this requirement?
Following code segment shows the query to retrieve the data to the incomming keyword.
var hotels = await _hotelsCollection
.Find(Builders<HotelDocument>.Filter.Text(keyword))
.Project<HotelDocument>(hotelFields)
.ToListAsync();
var terminals = await _terminalsCollection
.Find(Builders<TerminalDocument>.Filter.Text(keyword))
.Project<TerminalDocument>(terminalFeilds)
.ToListAsync();
var destinations = await _destinationsCollection
.Find(Builders<DestinationDocument>.Filter.Text(keyword))
.Project<DestinationDocument>(destinationFields)
.ToListAsync();
So this is a classic "autocomplete" feature, there are some known best practices you should follow:
On the client side you should use a debounce feature, this is a most. there is no reason to execute a request for each letter. This is most critical for an autocomplete feature.
On the backend things can get a bit more complicated, naturally you want to be using a db that is suited for this task, specifically MongoDB have a service called Atlas search that is a lucene based text search engine.
This will get you autocomplete support out of the box, however if you don't want to make big changes to your infra here are some suggestions:
Make sure the field your searching on is indexed.
I see your executing 3 separate requests, consider using something like Task.WhenAll to execute all of them at once instead of 1 by 1, I am not sure how the client side is built but if all 3 entities are shown in the same list then ideally you merge the labels into 1 collection so you could paginate the search properly.
As mentioned in #2 you must add server side pagination, no search engine can exist without one. I can't give specifics on how you should implement it as you have 3 separate entities and this could potentially make pagination implementation harder, i'd consider wether or not you need all 3 of these in the same API route.
I'm using Algolia to power search in my app. I have an index called prod_COACHES in which I have some records with an object key called speciality1.
The data structure for speciality1 looks like this:
I have enabled speciality1.itemName as an Algolia 'facet' so that I can filter on it. All good so far and working nicely. Now, in my Algolia dashboard I can see a nice bit of UI that shows me every unique facet (in this case my specialisations) along with the number of records for each facet:
As it happens, I want to show exactly this information on my own UI in my app but I'm not sure how to get this data from Algolia in the most efficient way. I'm using the client side AlgoliaSearch Javascript SDK. How do I run a search to retrieve every unique speciality1.itemName and the number of records for each unique speciality1.itemName so I can build my own UI just like the above?
I have gone through the docs and followed the examples but my question is really about finding the most efficient way to do this from someone who really knows Algolia well, rather than hack my own solution together. Thanks!
It looks like you've enabled attributesForFaceting on the attribute speciality1.itemName. You can retrieve the facet values for the given attribue with the search parameter facets. The Algolia response will now contain a map with value:count. Here is an example with the JavaScript client:
import algoliasearch from 'algoliasearch';
const client = algoliasearch('XXX', 'XXX');
const index = client.initIndex('XXX');
index.search('', {
facets: ['speciality1.itemName']
}).then(result => {
console.log(result.facets)
});
If you want to easily build a search UI, you should take a look at the InstantSearch libraries. It's built on top of Algolia to ease the state/ui management for such UI. Many flavours are available e.g. Vanilla, React.
To help me better understand Flutter and Firebase, I'm making a list sharing app. I'm working on the list home screen that will show a reorderable list tile view with a tile for each of the users' lists, I have not began to work on whats inside of these lists. I have firestore set up so that each list is a sub collection, now I want to create a tile for each sub collection in that user's document ( a list of that user's lists). I'm having a tough time telling flutter to do something for each sub collection without using a specific sub collection's name. I wanted to pass in a list title to each tile by making a collection reference for each sub collection( list) and calling .id on each one. Using the collection ID as the title, I'm not yet sure if I can do this or if ill have to make the list title a field inside of each list. Either way, I need to find out how to do something for each subcollection inside a particular document. .forEach seems to only work on document fields, not subcollections? What am I doing wrong? I'm sure there is a better way to go about this. I have not included any code as this is a big picture kind of question.
There is no method in the Firestore client-side SDKs to get all collections (or subcollections under a specific document). Such API does exist in the server-side SDKs, but not in the client-side SDKs.
See:
Fetching all collections in Firestore
How to get all of the collection ids from document on Firestore?
So you'll need to know the collections already, typically by changing your data model. For example by creating a document for each list's metadata, and then storing the list items in a subcollection with a known name under that document. That way you can get all lists by querying for the documents, which is possible within the API.
I am working on a project and choosen mongodb for database. On it I have a form with following structure
Here it will save the details in database and will be checked on frontend if form 1 is enabled or not for Home cleaning etc. for this I have planned to save form ids in embedded document but I am not sure if its a good method to save it, I have selected embedded document because we can filter or compare directly. Other way is to save ids comma separated or as json object but then I will not be able to filter it directly and will have to fetch all ids and compare in code.
I have gone through the articles and they recommend it but my concern is, is it good to use embedded document for single value like here ?
Any help or explanation is appreciable.
example
{
id:1,
industry_id:1,
status:1,
forms:[{form_id:1},
{form_id:2}
]
}
Thanks
Same situation like this question, but with current DerbyJS (version 0.6):
Using imported docs from MongoDB in DerbyJS
I have a MongoDB collection with data that was not saved through my
Derby app. I want to query against that and pull it into my Derby app.
Is this still possible?
The accepted answer there links to a dead link. The newest working link would be this: https://github.com/derbyjs/racer/blob/0.3/lib/descriptor/query/README.md
Which refers to the 0.3 branch for Racer (current master version is 0.6).
What I tried
Searching the internets
The naïve way:
var query = model.query('projects-legacy', { public: true });
model.fetch(query, function() {
query.ref('_page.projects');
})
(doesn't work)
A utility was written for this purpose: https://github.com/share/igor
You may need to modify it to only run against a single collection instead of the whole database, but it essentially goes through every document in the database and modifies it with the necessary livedb metadata and creates a default operation for it as well.
In livedb every collection has a corresponding operations collection, for example profiles will have a profiles_ops collection which holds all the operations for the profiles.
You will have to convert the collection to use it with Racer/livedb because of the metadata on the document itself.
An alternative if you dont want to convert is to use traditional AJAX/REST to get the data from your mongo database and then just put it in your local model. This will not be real-time or synced to the server but it will allow you to drive your templates from data that you dont want to convert for some reason.