ndb查询部分字符串匹配

meh*_*kar 6 google-app-engine djangoappengine app-engine-ndb

这似乎应该是一个简单的问题.但是文档似乎没有回答它.使用他们的例子,我想这样做:

Account.query(Account.title == "best")
Run Code Online (Sandbox Code Playgroud)

除了我想匹配部分字符串.所以在这种情况下:

acct = Account(title="the best account in the world")
Run Code Online (Sandbox Code Playgroud)

带参数"best"的ndb查询将匹配acct.

我看到目前唯一的选择是遍历Account.query()并相互匹配titlere.search在Python模块.这似乎不是一个好的解决方案.

更新:我也在看gql.这样做:

acct = ndb.gql('SELECT * from Account WHERE title LIKE '%best%')
Run Code Online (Sandbox Code Playgroud)

返回一个 Parse Error: Invalid WHERE Condition at symbol LIKE

Kri*_*ian 9

注意:这并不能完全回答这个问题,但是有人在starts with寻找这个答案可能会有用.

NDB的String字段的索引方式是您可以执行大于(>=)和小于(<)的搜索.假设以下Person型号:

class Person(ndb.Model):
    name         = ndb.StringProperty()
    name_lower   = ndb.ComputedProperty(lambda self: self.name.lower())
Run Code Online (Sandbox Code Playgroud)

您可以执行以下操作:

def search_by_text(text):
  text = text.lower()
  limit = text[:-1] + chr(ord(text[-1]) + 1)
  return Person.query(Person.name_lower >= text, Person.name_lower < limit).fetch(50)

p = search_by_text('kri')
Run Code Online (Sandbox Code Playgroud)

limit此示例中的变量将包含字符串'krj'并成为搜索值的限制.以上内容将为您提供名称大于kri但小于krj并限制前50个发现的所有人.由于限制,名称喜欢krosslark将被过滤掉.

注意:您必须ndb.ComputedProperty包含要搜索的字段的小写版本.别忘了添加!


Sha*_*men 7

GQL没有通配符匹配,要实现这一点,您需要使用全文搜索.