简体   繁体   English

Firebase 查询“其中包含字符串”

[英]Firebase queries "where contains a string"

I'm trying to do a "where" query in Flutter-Firebase to filter by name (if the object name is "Una casa roja" and the user writes "casa", the object must be returned).我正在尝试在 Flutter-Firebase 中执行“where”查询以按名称过滤(如果 object 名称为“Una casa roja”并且用户写入“casa”,则必须返回 object)。 I don't know who can I do the query.我不知道我可以向谁查询。 I only have this, but is not what I am looking for:我只有这个,但不是我要找的:

.where('nombre', isGreaterThanOrEqualTo: nombre, isLessThan: nombre + 'z')

Thanks!谢谢!

Firestore doesn't support text-search by default. Firestore 默认不支持文本搜索。 However, there are a few workarounds.但是,有一些解决方法。

When you register a new user to your app you can store their username in firestore as an array that includes the possible ways you would search for a user (look at the attached image).当您将新用户注册到您的应用程序时,您可以将他们的用户名作为一个数组存储在 firestore 中,其中包括您搜索用户的可能方式(查看附件图像)。 在此处输入图像描述

You can do that by splitting the name string.您可以通过拆分名称字符串来做到这一点。

setSearchParameters(String name) {
        List<String> searchOptions = [];
        String temp = "";
        for (int i = 0; i < name.length; i++) {
          temp = temp + name[i];
          searchOptions.add(temp);
        }
        return searchOptions;
  }

Then you can query the users collection by searching in that array using arrayContains like this:然后,您可以通过使用arrayContains在该数组中搜索来查询用户集合,如下所示:

await usersCollection
        .where('searchOptions', arrayContains: searchText)
        .get()
        .then((value) =>
            value.docs.map((doc) => User.fromSnapShot(doc)).toList());

This solution works for simple cases but if you need advanced search capabilities then you'll have to use a 3rd party service such as Agolia .此解决方案适用于简单的情况,但如果您需要高级搜索功能,则必须使用 3rd 方服务,例如Agolia

Unfortunately, You can't do this.不幸的是,你不能这样做。 Firebase documentation says that you can't do it. Firebase 文档说你不能这样做。 I don't know why firebase doesn't support that.我不知道为什么 firebase 不支持。 But to do that you can use third-party APIs such as Agolia or Elastic.但要做到这一点,您可以使用第三方 API,例如 Agolia 或 Elastic。 For more information you can get it in their documentation in this link https://firebase.google.com/docs/firestore/solutions/search有关更多信息,您可以在此链接https://firebase.google.com/docs/firestore/solutions/search的文档中获取它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM