4

I have a question regarding a request to retrieve data from Google Cloud Firestore with specific parameters in my Flutter project.

I use the following code to check if a string in Firebase equals to a search query performed by a user:

var snapshot = await firestoreInstance.collection('categories/subcategory/items')
.where("parameter", isEqualTo: searchQuery).get()

This works if the user types exactly the name of the parameter stored in Firestore. But what I want is that it also works if only part of the searchQuery string is stored in the Firestore parameter.

I found a solution on https://medium.com/flutterdevs/implement-searching-with-firebase-firestore-flutter-de7ebd53c8c9 for this. In the article an array is created with all possibile searches for the parameter. But I think that is a bit complex and you have to generate a lot of new data in Firestore just to search for the article.

Is there a way to do this in an easier way so that you can use an operator as "contains" instead of "isEqualTo" in Flutter with Firebase for the request?

Tim B.
  • 88
  • 2
  • 11
  • 1
    You can use a third-party application if you want, like [Algolia](https://firebase.google.com/docs/firestore/solutions/search) for [example](https://itnext.io/full-text-search-in-flutter-with-algolia-firestore-cloud-functions-with-optimization-54004d727ad1). – JM Gelilio Apr 12 '21 at 07:41

2 Answers2

4
var snapshot = await firestoreInstance
  .collection('categories/subcategory/items')
  .where(
    'parameter',
    isGreaterThanOrEqualTo: searchQuery,
    isLessThan: searchQuery + 'z'
  )
  .get();
nexdev
  • 195
  • 11
1

Try this:

var snapshot = await firestoreInstance
      .collection('categories/subcategory/items')
      .where(
        'parameter',
        isGreaterThanOrEqualTo: searchQuery,
        isLessThan: searchQuery.substring(0, searchQuery.length - 1) +
            String.fromCharCode(searchQuery.codeUnitAt(searchQuery.length - 1) + 1),
      )
      .get();

I based the variable names on your example.

  • 1
    Hello, I tried your code but the problem is that now too many also non-matching results are shown. For example after entering the search query "Verlierer" the document with the name "Der mächtigste Verlierer der Welt" is shown which is correct. But there are also other posts as e.g. "Die neuen Spitzenkandidaten" shown which have nothing to do with the original post. Do you know why that happens? – Tim B. Apr 11 '21 at 19:20
  • To be honest, I even wonder why you get "Der mächtigste Verlierer der Welt" as a result, because I only get documents as results where the first letters of the field perfectly match the query. For instance, if the field's value is "example", it only works if the query is "examp" or something like that, but not if it's "xample" or "examplexx". Would you please provide the according snippet of your source code? – Nicolas Ebeling Apr 11 '21 at 20:15
  • I have to admit that my approach is far from perfect. Though, I don't think there is a better solution than the one explained in the Medium article. Maybe you could use some serverside function to handle queries like these. Sorry that it didn't work :/ – Nicolas Ebeling Apr 11 '21 at 20:19
  • 1
    You'll find some interesting approaches under [this question](https://stackoverflow.com/questions/50870652/flutter-firebase-basic-query-or-basic-search-code)! – Nicolas Ebeling Apr 11 '21 at 20:28
  • Thank you very much for your effort. I will take a look at the other question :) – Tim B. Apr 13 '21 at 06:00