1
votes

Iam building an application of places with Firestore database ,

PLaces:
  doc1:
   name: "Machin"
   category: "Pharmacy"
   city: "Paris"
 doc2:
   name: "Paris"
   category: "Pharmacy
   city: "New york"

  doc3:
   name: "Paris"
   category: "School
   city: "New york"

The search is by an input, I want tha the user can search by name of place (with autocomplete) or catgeory with name or city with name .... (in the same input), so here we talk about search by meany fields with The No Exact value.

Is this even possible in Cloud Firestore? without third-party integration?

And what is The cheapest between ElastikSearch and Algolia

And what do you think about this approach here

2

2 Answers

0
votes

you can try with where and add as many parameter you want in your query see my example snip :-

List<DocumentSnapshot> documentList;
documentList = (await Firestore.instance
    .collection("PLaces")
    .document(await firestoreProvider.getUid())
    .collection(caseCategory)
    .where("category, name, city", isEqualTo: query)
    .getDocuments())
    .documents;
-1
votes

[Based on MD Jamal's Firestore Like Query (Video on YouTube)]
If you want to do something like this--
Suppose in Firestore , You have Documents with a field tag
Sample Image

The tag has value - "i am cold like lava"
[Here I am assuming you have implemented searchview and ready to search]

Now if you type something in searchview like "cold" or "like java", and want to get those documents, where the tag has those words(queries), then you have to -

  1. First pass a method searchData(s) inside onQueryTextSubmit and pass the word(query) in it(which is String s, or by default it may be String query)

       searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
             @Override
             public boolean onQueryTextSubmit(String s) {          
                 searchData(s); 
                 return false;
             }
             @Override
             public boolean onQueryTextChange(String newText) {
                 return false;
             }
         });
    
  2. Create the searchData(s) method:

      private void searchData(String s) {
           db.collection("collection")
                   .get()
                   .addOnSuccessListener(queryDocumentSnapshots -> {
                     for (QueryDocumentSnapshot document : queryDocumentSnapshots) {
                         if (Objects.requireNonNull(document.getString("tags")).contains(s.toLowerCase()))
                             dataList.add(document.toObject(MainModel.class));
                     }
                     // update Adapter
                     Collections.shuffle(dataList);
                     adapter.notifyDataSetChanged();
                 });
     }
    

In the above code,
we are getting the tag with document.getString("tags"). And also converting the word(query) to lowercase with s.toLowerCase() in case you type a capital.

After that, from the line
if (Objects.requireNonNull(document.getString("tags")).contains(s.toLowerCase())),
we are checking if the tag has the word s(means the word you typed), and if it has, then we are adding the document into our dataList, by ,
dataList.add(document.toObject(MainModel.class));

After that we are calling Collections.shuffle(dataList); to randomize the data and then updating the adapter , by
adapter.notifyDataSetChanged();

Now if you search, and if the word matches, you will get that data in your recyclerview/listview/anywhere.
Let me know if it works or not
[Sorry for my English]