Firestore - 简单的全文搜索解决方案 [英] Firestore - Simple full text search solution

查看:18
本文介绍了Firestore - 简单的全文搜索解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Firestore 不支持全文搜索,它为我们提供了使用第三方服务的解决方案.但是,我找到了simple全文搜索"的简单解决方案,我认为这可能会帮助那些不想像我一样使用第三方服务来完成如此简单任务的其他人.我正在尝试搜索保存在我的 companyName 下的 firestore 集合中的公司名称,它可以是任何格式,例如My Awesome Company".当使用 companyName 添加新公司或更新 companyName 中的值时,我还保存了 searchName ,它与公司名称的值相同,但小写没有空格

I know that firestore doesn't support full text search and it giving us solution to use third party services. However I found a simple solution to simple "full text search" and I think this might help others who doesn't want to use third party services as me for such a simple task. I'm trying to search for company name which is saved in firestore collection under my companyName which can be in any format for example "My Awesome Company". When adding new company with companyName or updating a value in companyName I'm also saving searchName with it which is the same value as company name but in lower case without spaces

searchName: removeSpace(companyName).toLowerCase() 

removeSpace 是我的简单自定义函数,可以删除文本中的所有空格

removeSpace is my simple custom function which remove all spaces from a text

export const removeSpace = (string) => {
    return string.replace(/s/g, '');
}

这会将我们的公司名称变为 myawesomecompany,并保存在 searchName

That turns our company name to myawesomecompany which is saved in searchName

现在我有了一个 firestore 函数来搜索通过 searchName 建立索引并返回 companyName 的公司.最小搜索值是没有最后一个字符的搜索值,最大搜索值是添加了zzzzzzzzzzzzzzzzzzzzzzzz"转换为小写的搜索值.这意味着如果您搜索 My Aw,那么最小值将是 mya,最大值将是 myawzzzzzzzzzzzzzzzzzzzzzzz

Now I've got a firestore function to search for company which indexing through searchName and returning companyName. Minumum search value is a searched value without last character and maximum search value is a searched value with added "zzzzzzzzzzzzzzzzzzzzzzzz" transformed to lower case. That means if you search for My Aw then min value will be mya and max value will be myawzzzzzzzzzzzzzzzzzzzzzzz

exports.handler = ((data) => {
const searchValue = data.value.replace(/s/g, '').toLowerCase()
const minName = searchValue.substr(0, searchName.length-1)
const maxName = searchValue + "zzzzzzzzzzzzzzzzzzzzzzzz"
let list = []
const newRef = db.collection("user").where("profile.searchName", ">=", minName).where("profile.searchName", "<=", maxName)
return newRef.get()
.then(querySnapshot => {
    querySnapshot.forEach(doc => {
        list.push({ name: doc.data().profile.companyName})
    })
    return list
})
})

我没有时间对其进行全面测试,但到目前为止它可以正常工作.如果您发现它有任何问题,请告诉我.现在的问题是

I didn't have time to fully test it but so far it works without any problems. Please let me know if you spot anything wrong with it. Now the question is

z"字符是firestore中的最高值字符,还是有其他更体面的方法可以在不添加zzzzzzzzzzzzz"的情况下添加到搜索值最大值?

Is "z" character the highest value character in firestore or is there any other more decent way to add into the search value maximum amount without adding "zzzzzzzzzzzzz"?

推荐答案

对之前的答案稍作修改,我又做了一个简单的文本搜索.我将关键字保存到数组中,而不是像这样保存在对象中

With a little modification of previous answer I have made another simple text search. I'm saving keyword to an array instead of saving it in object like this

nameIndex: textIndexToArray(companyName)

textIndexToArray 是我的自定义函数

where textIndexToArray is my custom function

export const textIndexToArray = (str) => {
const string = str.trim().replace(/ +(?= )/g,'')
let arr = []
for (let i = 0; i < string.trim().length; i++) {
    arr.push(string.substr(0,i+1).toLowerCase());
}
return arr
}

将文本传输到数组中.例如

which transfer a text into array. For example

"My Company"

将返回

[m, my, my , my c, my co, my com, my comp, my compa, my compan, my company]

nameIndex 保存在 firestore 中,我们可以简单地通过 nameIndex 查询数据并返回 companyName

with nameIndex saved in firestore we can simply query the data thorough nameIndex and return companyName

exports.handler = ((data) => {
const searchValue = data.value.toLowerCase()
let list = []
const newRef = db.collection("user").where("nameIndex", "array-contains", searchValue)
return newRef.get()
.then(querySnapshot => {
    querySnapshot.forEach(doc => {
        list.push({ name: doc.data().companyName, })
    })
    return list
})
})

这篇关于Firestore - 简单的全文搜索解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆