【发布时间】:2015-08-19 04:49:58
【问题描述】:
我正在尝试实现一个搜索,它根据系统描述、系统名称和系统标签中匹配单词的数量以及系统被其他人查看的次数组合来对结果进行排序用户(system_hits)。我正在尝试使用混合属性来执行此操作,但我的实现有问题,我不断收到错误 “SQL 表达式对象或字符串预期,得到类型的对象”,任何见解为什么我会收到此错误?
app.py
>@hybrid_method
def relevance(self,searchTag):
wordsInName = self.system_name.split() #turn string into list of strings
wordsInDescription = self.system_description.split()
wordsInTags = self.tags.split()
nameOccurrences = 0
descriptionOccurrences = 0
tagOccurrences = 0
for word in wordsInName:
if word == searchTag:
nameOccurrences += 1
for word in wordsInDescription:
if word == searchTag:
descriptionOccurrences += 1
for word in wordsInTags:
if word == searchTag:
tagOccurrences += 1
occurrenceRating = (nameOccurrences+descriptionOccurrences*.3+tagOccurrences*2)
return self.system_hits*occurrenceRating
@relevance.expression(cls,searchTag):
return self.system_hits
results = System.query.order_by(System.relevance)
如您所见,在表达式部分我还没有弄清楚如何正确地处理我的函数,我只是按点击次数排序。如果您对如何使用 sqlalchemy 编写上述函数有任何建议,那也会有所帮助,但这种担忧并不那么直接。
【问题讨论】:
标签: python sqlalchemy flask-sqlalchemy