0

我有一个存储在 GAE 中的 60000 对双语词典数据库,如下所示:

日期、标签、值

日期,可见,螺丝

日期,可见,螺丝

日期, vis à bois, 木螺丝

date, vis à bout pointu, 锥尖螺钉

日期,相对于 braser,钎焊螺丝

date, vis à métaux, 机械螺丝

.

今天使用以下python脚本:

def get_value(self, tag):
  entry = db.GqlQuery("SELECT * FROM StoredData where tag = :1", tag).get()
  value = entry.value
  value = unicode(value)
  value = value.encode('ascii','xmlcharrefreplace')

如果我要求“vis”,我只会得到“screw”作为回应。

问题:

  1. 我还想得到“螺丝钉”作为第二个答案,也就是说,检索所有具有相同标签的实体。

  2. 我还想获取包含子字符串“screw”的所有实体(用户至少输入 3 个字符):

螺丝, 螺丝, 木螺丝, 锥尖螺丝, 钎焊螺丝, 机械螺丝

=> 我必须进行哪些 GQL 查询才能在此脚本生成的表中显示它们?

### Show the tags and values as a table.
###def show_stored_data(self):
###  self.response.out.write('''
###    <p><table border=1>
###      <tr>
###         <th>Key</th>
###         <th>Value</th>
###         <th>Created (GMT)</th>
###      </tr>''')
###  entries = db.GqlQuery("          ***GQL query n°1 or n°2***           ")
###  for e in entries:
###    entry_key_string = str(e.key())
###    self.response.out.write('<tr>')
###    self.response.out.write('<td>%s</td>' % e.tag)
###    self.response.out.write('<td>%s</td>' % e.value)
###    self.response.out.write('<td><font size="-1">%s</font></td>\n' % e.date.ctime())
###    self.response.out.write('''
###      <td><form action="/deleteentry" method="post"
###            enctype=application/x-www-form-urlencoded>
###     <input type="hidden" name="entry_key_string" value="%s">
###     <input type="hidden" name="tag" value="%s">
###            <input type="hidden" name="fmt" value="html">
###     <input type="submit" style="background-color: red" value="Delete"></form></td>\n''' %
###                            (entry_key_string, e.tag))
###    self.response.out.write('</tr>')
###  self.response.out.write('</table>')

非常感谢您的帮助。

菲利普

4

2 回答 2

0

听起来你想要全文搜索:https ://developers.google.com/appengine/docs/python/search/overview

于 2012-10-11T16:18:07.543 回答
0

只得到一个结果

是您的问题-您正在对象.get()上使用该方法GqlQuery。文档说:

调用查询的get()方法,以获取在 Datastore 中找到的第一个匹配实体:

如果你这样做:

def get_value(self, tag):
  entries = db.GqlQuery("SELECT * FROM StoredData where tag = :1", tag).get()
  for entry in entries:
    value = entry.value
    value = unicode(value)
    value = value.encode('ascii','xmlcharrefreplace')
    print value

我希望您会看到您将同时获得screwscrews

子串

至于用 抓住所有字符串"screw",这将比你想象的要难得多。看看这个 SO question。一些答案建议您使用SearchableModel.

例如vis à <blank>,如果您知道该短语始终以相关子标签开头,您可能可以使用>and<来获得您要查找的内容,也许可以查看标签是否在visand之间vis zzzzzzz,或者其他类似的东西行——我对 GQL 的字符串排序顺序不是 100% 清楚,而且我现在找不到关于它的文档。

于 2012-10-11T05:26:06.513 回答