如何使用 GAE 创建自动完成功能?
我使用 jQuery UI 自动完成小部件。我还有 GAE 数据存储:
class Person(db.Model):
# key_name contains person id in format 'lastname-firstname-middlename-counter',
# counter and leading dash are omitted, if counter=0
first_name = db.StringProperty()
last_name = db.StringProperty()
middle_name = db.StringProperty()
当用户可以输入姓氏、名字和/或中间名时,如何在自动完成小部件中搜索人员?
因此,我将用户输入字符串作为 self.request.get('term')
获取。我应该如何在我的数据存储中搜索相同的内容(因为我需要查看每个字段,并且可能需要查看 3 个字段的组合值)?如何优化这样的查询? 我也不清楚回复格式应该是什么。 jQuery 文档说:
数据源可以是:
包含本地数据的数组 一个字符串,指定 URL 回调
本地数据可以是一个简单的字符串数组,或者它包含 数组中每个项目的对象,带有标签或值 财产或两者兼而有之。
I use jQuery UI autocomplete widget. Also I have GAE datastore:
class Person(db.Model):
# key_name contains person id in format 'lastname-firstname-middlename-counter',
# counter and leading dash are omitted, if counter=0
first_name = db.StringProperty()
last_name = db.StringProperty()
middle_name = db.StringProperty()
How can I search the person in the autocomplete widget, when user can input there surname, first name and/or middle name?
So, I am getting user input string as self.request.get('term')
. How should I search for the same in my datastore (since I need to look at each field and probably for combined value of 3 fields)? How to optimize such query?
I am also not clear what should be the reply format. jQuery doc says:
A data source can be:
an Array with local data a String, specifying a URL a Callback
The local data can be a simple Array of Strings, or it contains
Objects for each item in the array, with either a label or value
property or both.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有一些巧妙的技巧。考虑这个增强模型:
您需要使名称_lower 与真实字段保持同步,例如:
您可以使用 派生属性。
现在,您的查询:
这将为您提供:
因此,对“smi”的查询将返回任何名称以“smi”开头的任何人。
将小写名称复制到 ListProperty 可以实现不区分大小写的匹配,并允许我们通过一个查询搜索所有 3 个字段。 “\ufffd”是最大可能的 unicode 字符,因此它是我们的子字符串匹配的上限。如果出于某种原因您想要完全匹配,请改为过滤
'names_lower =', term
。编辑:
原始解决方案中已经考虑到了这一点。通过获取 3 个字段并将它们复制到单个 ListProperty,我们实际上是在创建一个索引,其中每个人有多个条目。如果我们有一个名为 Bob J Smith 的人,他在我们的索引中将有 3 个命中:
这消除了在每个字段上运行不同查询的需要。
仔细阅读文档。格式化 jQuery 的输出应该非常简单。您的数据源将是指定 URL 的字符串,并且您需要将响应格式化为 JSON。
There are a few neat tricks here. Consider this augmented model:
You'll need to keep names_lower in sync with the real fields, e.g.:
You can do this more elegantly with a DerivedProperty.
And now, your query:
This gives you:
So a query for "smi" will return any person with any name starting with "smi" in any case.
Copying lower-cased names to a ListProperty enables case-insensitive matching, and allows us to search all 3 fields with one query. "\ufffd" is the highest possible unicode character, so it's the upper limit for our substring match. If for some reason you want an exact match, filter for
'names_lower =', term
instead.Edit:
This is already accounted for in the original solution. By taking the 3 fields and copying them to a single ListProperty, we're essentially creating a single index with multiple entries per person. If we have a person named Bob J Smith, he'll have 3 hits in our index:
This eliminates the need to run distinct queries on each field.
Read the docs carefully. Formatting output for jQuery should be pretty straightforward. Your data source will be a string specifying a URL, and you'll want to format the response as JSON.
基本上同意德鲁写的所有内容我发布了我的博客的链接 具有相当详细的示例,用于在数据存储中搜索信息时自动完成选择关键字。
所有这些都在 GAE 中使用 Python 完成,并使用 YUI3 而不是 jQuery(插入 jQuery 或任何其他库是微不足道的)。
简而言之,这个想法是数据存储包含使用关键字(使用关系索引实体)索引的文档集。当用户输入要搜索的单词时,系统会使用这些文档中的关键字自动完成它们。
Basically agreeing on everything Drew wrote I post a link to my blog with rather elaborate example for auto-completing selecting keywords when searching for information in the datastore.
All done in GAE with Python and using YUI3 instead of jQuery (plugging in jQuery or any other library instead would be trivial).
Shortly, the idea is that datastore contains set of documents that are indexed using keywords (using Relation Index Entity). And when user enters words to search for, the system autocompletes them with the keywords from those documents.