查找最符合条件的实体

发布于 2024-10-12 03:32:44 字数 357 浏览 4 评论 0原文

我有一些属性,比方说,

color = blue
age = 22
name = Tom

在数据存储中的许多实体中,如何才能获得与大多数属性相匹配的属性?我当然可以这样做:

query.filter('color =', 'blue')
query.filter('age =', '22')
query.filter('name =', 'Tom')

但是如果不存在具有确切属性的实体,则它不会给出结果。如何获取至少两个过滤器匹配的实体,或者如果仍然不起作用,则使用一个过滤器..?它不需要计算值或其他内容的相似度,只需为我提供大多数过滤器尽可能匹配的实体即可。

I have some properties, let's say

color = blue
age = 22
name = Tom

Of a number of entities in the datastore, how can I get one that matches most of the properties? Of course I could do:

query.filter('color =', 'blue')
query.filter('age =', '22')
query.filter('name =', 'Tom')

But if no such entity with the exact properties exists, it does not give a result. How can I get entities where at least two filters match, or if that still does not work, one filter..? It doesn't need to calculate similarity of the values or something, just give me entities where most filters as possible match.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风筝有风,海豚有海 2024-10-19 03:32:44

您基本上要求使用 OR 运算符,这是不可能通过单个查询来完成的。我认为你有两个选择:

1)通过子类化 MultiQuery 添加 OR 运算符。这仍然会在后台执行多个查询,并且排序和游标将不起作用。

2) 预先计算三个属性的所有可能组合,将它们放入 StringListProperty 中,然后使用 IN 运算符。例如,您的模型将如下所示:

m.col_age_name = ['c:blue, a:22', 'c:blue, n:Tom', \
                  'a:22, n:Tom', 'a:22, c:blue, n:Tom']

然后运行过滤器:

q.filter('col_age_name IN', ['c:blue, a:22', 'c:blue, n:Tom', 'a:22, n:Tom'])

该解决方案存在问题:(a) 它在后台执行多个查询,(b) 您需要运行它三次(一次针对 1、2 和3 个属性),(c) 它不支持顺序或游标,(d) 它使数据管理变得一团糟。唯一的好处是,它将您需要进行的过滤器调用的最大数量从 7 个减少到 3 个。

这显然比它的价值更麻烦。我只会运行这个简单查询七次,它有缺点 (a) 和 (c),但没有缺点 (d)。

You are basically asking for an OR operator, which is not possible to do with a single query. I think you have two options:

1) Add an OR operator by subclassing MultiQuery. This will still execute several queries under the hood and ordering and cursors won't work.

2) Pre-compute all possible combinations of your three properties, put them into a StringListProperty, and then use the IN operator. For example, your model would look as follows:

m.col_age_name = ['c:blue, a:22', 'c:blue, n:Tom', \
                  'a:22, n:Tom', 'a:22, c:blue, n:Tom']

Then you run the filter:

q.filter('col_age_name IN', ['c:blue, a:22', 'c:blue, n:Tom', 'a:22, n:Tom'])

This solution has problems: (a) it performs several queries under the hood, (b) you need to run it three times (once for 1, 2, and 3 properties), (c) it won't support order or cursors, and (d) it makes managing the data a mess. The only benefit is that it reduces the maximum number of filter calls you need to make from 7 to 3.

It is clearly more hassle than it's worth. I would just run the simple query seven times, which has disadvantages (a) and (c) but not (d).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文