使用部分字符串作为过滤器搜索Go GAE数据存储区中的条目

我在数据存储区中有一组条目,我想在用户类型查询时搜索/检索它们。如果我有完整的字符串,这很容易:

q := datastore.NewQuery("Products").Filter("Name =", name).Limit(20)

但我不知道如何使用部分字符串,请帮忙。


慕容森
浏览 217回答 3
3回答

UYOU

q&nbsp;:=&nbsp;datastore.NewQuery("Products").Filter("Name&nbsp;>",&nbsp;name).Limit(20)like应用引擎上没有任何操作,但是您可以使用“ <”和“>”例子:'moguz'>'moguzalp'

交互式爱情

嘎!我刚刚意识到您的问题是特定于Go的。我下面的代码适用于Python。道歉。我对Go运行时也很熟悉,以后可以将其翻译成Python。但是,如果所描述的原理足以使您朝正确的方向前进,请告诉我,我也不会打扰。AppEngine数据存储区不直接支持此类操作,因此您必须滚动自己的功能来满足此需求。这是一个快速,可行的解决方案:class StringIndex(db.Model):&nbsp; &nbsp; matches = db.StringListProperty()&nbsp; &nbsp; @classmathod&nbsp; &nbsp; def GetMatchesFor(cls, query):&nbsp; &nbsp; &nbsp; &nbsp; found_index = cls.get_by_key_name(query[:3])&nbsp; &nbsp; &nbsp; &nbsp; if found_index is not None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if query in found_index.matches:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Since we only query on the first the characters,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # we have to roll through the result set to find all&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # of the strings that matach query. We keep the&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # list sorted, so this is not hard.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; all_matches = []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; looking_at = found_index.matches.index(query)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matches_len = len(foundIndex.matches)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while start_at < matches_len and found_index.matches[looking_at].startswith(query):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; all_matches.append(found_index.matches[looking_at])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; looking_at += 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return all_matches&nbsp; &nbsp; &nbsp; &nbsp; return None&nbsp; &nbsp; @classmethod&nbsp; &nbsp; def AddMatch(cls, match) {&nbsp; &nbsp; &nbsp; &nbsp; # We index off of the first 3 characters only&nbsp; &nbsp; &nbsp; &nbsp; index_key = match[:3]&nbsp; &nbsp; &nbsp; &nbsp; index = cls.get_or_insert(index_key, list(match))&nbsp; &nbsp; &nbsp; &nbsp; if match not in index.matches:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # The index entity was not newly created, so&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # we will have to add the match and save the entity.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index.matches.append(match).sort()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index.put()要使用此模型,您每次添加可能在其上进行搜索的实体时,都需要调用AddMatch方法。在您的示例中,您有一个Product模型,用户将在上进行搜索Name。在您的Product类中,您可能具有AddNewProduct创建新实体并将其放入数据存储区的方法。您将添加该方法StringIndex.AddMatch(new_product_name)。然后,在从AJAXy搜索框调用的请求处理程序中,您将使用StringIndex.GetMatchesFor(name)来查看所有以中的字符串开头的存储产品name,并以JSON或其他形式返回这些值。代码内部发生的事情是,名称的前三个字符用于包含字符串列表的实体的key_name,所有存储的名称均以这三个字符开头。使用三(相对于其他数字)绝对是任意的。系统的正确数字取决于您索引的数据量。可以存储在StringListProperty中的字符串数量有限制,但是您还想平衡数据存储中StringString实体的数量。进行一点数学运算即可为您提供合理数量的字符。

富国沪深

如果关键字数量有限,则可以考虑添加部分搜索字符串的索引列表属性。请注意,每个实体最多只能有5000个索引,实体总大小不能超过1MB。但是您也可以等待Go&nbsp;SQL运行时可以使用Cloud SQL和全文搜索API。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go