Query runes in Firestore

Is there a way we can query the runes from a string field in firestore documents in Flutter? E.g.

import 'package:cloud_firestore/cloud_firestore.dart';

class SearchService{

 searchByName(String searchField){
   return Firestore.instance.collection('firstCollection')
       .where('name'.runes, isEqualTo: searchField.runes)
       .getDocuments();
 }

}
1 Like

Hi @james666, basically no, the way you are doing it. But you can do it yourself in a couple of ways depending on what you are trying to achieve.

The most straight forward approach is to use an array in the Firestore field instead of a string. Or if you want the string for practical reasons store both. Then you query the collection to match the field with the array of the runes.

For explaining purposes I’ll change the info in Firestore to the one shown bellow so it is easier to understand:

import 'package:cloud_firestore/cloud_firestore.dart';

class SearchService{

 searchByName(){
   return Firestore.instance.collection('test')
       .where('runes', isEqualTo: ('ABC€').codeUnits)
       .get();
 }

}

codeUnits is a getter that returns UTF-16.

This will return all docs with the exact same array from the string. But for that you could simply do .where('string', isEqualTo: 'name').

I really don’t know why you would need to look for the runes instead of a string and the change to runes on the device if it is needed.

If you want to achieve a more “full text” search like functionality. You can use a couple of other solutions. For names and fields like that you can query for higher and lower values like:

import 'package:cloud_firestore/cloud_firestore.dart';

class SearchService{

int getLetter(String letter) {
    int nextChar;

    if (letter == 'z')
      nextChar = 'a'.codeUnits[0];
    else if (letter == 'Z')
      nextChar = 'A'.codeUnits[0];
    else
      nextChar = letter.codeUnits[0] + 1;

    print('return: $nextChar');
    return nextChar;
  }

 searchByName(){
   return Firestore.instance.collection('test')
       .where('string', isGreaterThanOrEqualTo: 'AB' , isLessThan: utf8.decode([getLetter('B')]) ).
       .get();
 }

}

For full text search on descriptions and bigger fields, you can either use a paid service like Algolia, or break every string by every space 'description of something'.split(' ') and store and array of strings or runes in Firestore. Then use array-contains, array-contains-any or in depending on what you need. Heres is the oficial docs o does queries

BTW: I see you are using .getDocuments() that method has been deprecated so be sure to update your Fire Packages.

3 Likes

Amazing, thank you so much for all of this, I really really appreciate that.

1 Like

Happy to help man. If you any other thing let me know :wink:

3 Likes