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

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

问题描述

我知道firestore不支持全文搜索,它为我们提供了使用第三方服务的解决方案.但是,我找到了简单全文搜索"的简单解决方案,并且我认为这可能对不想使用像我这样的第三方服务的其他人有所帮助.我正在尝试搜索公司名称,该公司名称保存在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的公司.最小搜索值是不带最后一个字符的搜索值,最大搜索值是将"zzzzzzzzzzzzzzzzzzzzzzzzzzzz"添加为小写形式的搜索值.这意味着,如果您搜索我的Aw ,则最小值将为 mya ,最大值将为 myawzzzzzzzzzzzzzzzzzzzzzzzzzzz

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中的最高值字符,还是还有其他更体面的方式可以在不添加"zzzzzzzzzzzzzzz"的情况下添加到搜索值最大数量中?

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天全站免登陆