[英]Sequelize query string prefix / starts with
I'm building an autofill function that takes a string input and returns a list of string suggestions. 我正在构建一个自动填充功能,该功能接受字符串输入并返回字符串建议列表。
Sequelize's iLike:query
returns every string in which the queried string appears. Sequelize的
iLike:query
返回出现查询的字符串的每个字符串。 I would like to favour strings for which the query is a prefix. 我希望使用查询作为前缀的字符串。 For example when
query='sh'
then the results should return strings that start with sh
instead of having sh
anywhere within the string. 例如,当
query='sh'
,结果应返回以sh
开头的字符串,而不是在字符串中的任何地方都包含sh
。
This is relatively simple to do after receiving the data from the DB, however I was wondering if there is a way to accomplish this via sequelize while querying the DB? 从数据库接收数据后,这相对简单,但是我想知道在查询数据库时是否有一种方法可以通过序列化来实现? If so how?
如果可以,怎么办?
The DB size will be between 10,000 and 100,000 strings of no more than a handful of words (company names to be exact). DB的大小将在10,000到100,000个字符串之间,且不超过几个单词(确切地说是公司名称)。
Optional question: DB's usually have superior performance to generically written code, in this circumstance should there even be a noticeable difference? 可选问题:数据库通常比通用代码具有更好的性能,在这种情况下,甚至应该有明显的区别吗? Or should I just collect all the data from the DB and apply some other filters on it after via vanilla JS.
或者我应该只是通过Vanilla JS从数据库中收集所有数据并对其应用一些其他过滤器。
let suggestions = yield db.Company.findAll({
limit: 7,
where: {
company_name: {
$iLike: '%'+this.data.query
}
}
})
Seems like this is super easy! 好像这样超级容易! The
'%'
acts like a *
from regex. '%'
作用类似于正则表达式中的*
。 So query + '%'
returns any results where query is the prefix. 因此,
query + '%'
返回以query为前缀的所有结果。
let suggestions = yield db.Company.findAll({
limit: 5,
where: { $or: [
{ stock_ticker: { $ilike: query + '%' } },
{ company_name: { $ilike: query + '%' } }
]},
order: '"volume" DESC'
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.