Google Cloud Datastore query.add_filter 无法正常工作
Google Cloud Datastore query.add_filter doesn't work correctly
我已经设法将一些数据保存到 Google Cloud Datastore,现在我正计划查询它以获取有意义的数据。
我已尽力遵循 Google Cloud documentation,但我无法 add_filter 正确使用浮点数。
在此查询中,我尝试查找 currentPrice
超过 500 的所有产品,但我得到的所有结果只是按 currentPrice
排序:
from google.cloud import datastore
def create_client(project_id):
return datastore.Client(project_id)
client = create_client('product-catalog')
query = client.query(kind='Product')
query.add_filter('currentPrice', '>', 500)
for result in query.fetch():
for key in result:
print(key+"("+str(type(result[key]))+"): "+str(result[key]))
print('---')
returns:
title(<class 'str'>): Cheap Oven
url(<class 'str'>): /product/343
currentPrice(<class 'float'>): 109.0
createdAt(<class 'datetime.datetime'>): 2018-03-22 19:52:02.173806+00:00
---
title(<class 'str'>): Regular Oven
url(<class 'str'>): /product/1231
currentPrice(<class 'float'>): 549.0
createdAt(<class 'datetime.datetime'>): 2018-03-21 03:25:24.622558+00:00
---
title(<class 'str'>): Expensive Oven
url(<class 'str'>): /product/4234
currentPrice(<class 'float'>): 2399.0
createdAt(<class 'datetime.datetime'>): 2018-03-23 22:46:01.571207+00:00
---
我原以为我的查询结果只有 Regular Oven
和 Expensive Oven
。
为清楚起见,我只包含了三个结果,但在我的实际代码中有超过 50,000 种产品,而且我已经验证,查询结果是按当前价格排序的所有产品,这不仅仅是巧合。
经过一些测试,我发现问题是查询中有整数,而对应的值是浮点数。更改 500 -> 500.0 解决了这个问题。
所以这可以正常工作:
from google.cloud import datastore
def create_client(project_id):
return datastore.Client(project_id)
client = create_client('product-catalog')
query = client.query(kind='Product')
query.add_filter('currentPrice', '>', 500.0)
for result in query.fetch():
for key in result:
print(key+"("+str(type(result[key]))+"): "+str(result[key]))
print('---')
returns:
title(<class 'str'>): Regular Oven
url(<class 'str'>): /product/1231
currentPrice(<class 'float'>): 549.0
createdAt(<class 'datetime.datetime'>): 2018-03-21 03:25:24.622558+00:00
---
title(<class 'str'>): Expensive Oven
url(<class 'str'>): /product/4234
currentPrice(<class 'float'>): 2399.0
createdAt(<class 'datetime.datetime'>): 2018-03-23 22:46:01.571207+00:00
---
我看到您已经找到了问题的解决方案,但让我再提供一些详细信息,说明为什么 > 500
和 > 500.0
return 的内容类型不同,以便更好地理解报告的行为发生的原因。
对于数据存储,Floating-point numbers and Integers are completely different and independent property types. In this case, as explained in the section about how Datastore performs ordering by value type,整数在浮点数之前排序,这样任何具有浮点类型的值都将大于任何具有整数类型的值:
5 < 7 --- 5.0 < 7.0 --- 7 < 5.0
这就是为什么当您使用过滤器 query.add_filter('currentPrice', '>', 500)
时,所有实体都被 returned 的原因,因为在这种情况下,所有价格(浮动类型)都更高比 500
(整数类型):
500 < 109.0 --- 500 < 549.0 --- 500 < 2399.0
因此,在对数据存储区中的查询应用过滤器时,您应该使用与您尝试过滤的 属性 类型相同的 属性 类型。
我已经设法将一些数据保存到 Google Cloud Datastore,现在我正计划查询它以获取有意义的数据。
我已尽力遵循 Google Cloud documentation,但我无法 add_filter 正确使用浮点数。
在此查询中,我尝试查找 currentPrice
超过 500 的所有产品,但我得到的所有结果只是按 currentPrice
排序:
from google.cloud import datastore
def create_client(project_id):
return datastore.Client(project_id)
client = create_client('product-catalog')
query = client.query(kind='Product')
query.add_filter('currentPrice', '>', 500)
for result in query.fetch():
for key in result:
print(key+"("+str(type(result[key]))+"): "+str(result[key]))
print('---')
returns:
title(<class 'str'>): Cheap Oven
url(<class 'str'>): /product/343
currentPrice(<class 'float'>): 109.0
createdAt(<class 'datetime.datetime'>): 2018-03-22 19:52:02.173806+00:00
---
title(<class 'str'>): Regular Oven
url(<class 'str'>): /product/1231
currentPrice(<class 'float'>): 549.0
createdAt(<class 'datetime.datetime'>): 2018-03-21 03:25:24.622558+00:00
---
title(<class 'str'>): Expensive Oven
url(<class 'str'>): /product/4234
currentPrice(<class 'float'>): 2399.0
createdAt(<class 'datetime.datetime'>): 2018-03-23 22:46:01.571207+00:00
---
我原以为我的查询结果只有 Regular Oven
和 Expensive Oven
。
为清楚起见,我只包含了三个结果,但在我的实际代码中有超过 50,000 种产品,而且我已经验证,查询结果是按当前价格排序的所有产品,这不仅仅是巧合。
经过一些测试,我发现问题是查询中有整数,而对应的值是浮点数。更改 500 -> 500.0 解决了这个问题。
所以这可以正常工作:
from google.cloud import datastore
def create_client(project_id):
return datastore.Client(project_id)
client = create_client('product-catalog')
query = client.query(kind='Product')
query.add_filter('currentPrice', '>', 500.0)
for result in query.fetch():
for key in result:
print(key+"("+str(type(result[key]))+"): "+str(result[key]))
print('---')
returns:
title(<class 'str'>): Regular Oven
url(<class 'str'>): /product/1231
currentPrice(<class 'float'>): 549.0
createdAt(<class 'datetime.datetime'>): 2018-03-21 03:25:24.622558+00:00
---
title(<class 'str'>): Expensive Oven
url(<class 'str'>): /product/4234
currentPrice(<class 'float'>): 2399.0
createdAt(<class 'datetime.datetime'>): 2018-03-23 22:46:01.571207+00:00
---
我看到您已经找到了问题的解决方案,但让我再提供一些详细信息,说明为什么 > 500
和 > 500.0
return 的内容类型不同,以便更好地理解报告的行为发生的原因。
对于数据存储,Floating-point numbers and Integers are completely different and independent property types. In this case, as explained in the section about how Datastore performs ordering by value type,整数在浮点数之前排序,这样任何具有浮点类型的值都将大于任何具有整数类型的值:
5 < 7 --- 5.0 < 7.0 --- 7 < 5.0
这就是为什么当您使用过滤器 query.add_filter('currentPrice', '>', 500)
时,所有实体都被 returned 的原因,因为在这种情况下,所有价格(浮动类型)都更高比 500
(整数类型):
500 < 109.0 --- 500 < 549.0 --- 500 < 2399.0
因此,在对数据存储区中的查询应用过滤器时,您应该使用与您尝试过滤的 属性 类型相同的 属性 类型。