dpd7122 2015-04-09 03:35
浏览 33
已采纳

当没有提供祖先时,为什么查询不返回结果?

Why the filter doesn't work when the ancestor is not specified? Isn't it supposed to work on entity type regardless the ancestor?

My use case: I've set-up several entities with a parent key. The key corresponds with another entity (the main entity) so that I can get the children by ancestor(main entity key).

However the issue seems to be that I can't query the entity properties anymore unless I specify the ancestor. Is this the way it should work? Below is some pseudo code. I can provide working code if the issue is not clear.

type MyStruct{
  Unique int
}

 key1 := datastore.NewKey(c, "table1", "verylongstring", 0, nil)
 kparent :=  datastore.NewKey(c, "table1", "anotherlongstring", 0, key1)
 x := MyStruct{Unique:23}
 if _, err := datastore.Put(c, kparent, &x); err != nil {
        panic(err)
 }

// This works
 _, err := datastore.NewQuery("table1").Ascentor(kparent).Filter("Unique =", v.Unique).GetAll(cx, dst)

// Query with filter without ancestor doesn't work. Returns no results error.
 _, err := datastore.NewQuery("table1").Filter("Unique =", v.Unique).GetAll(cx, dst)
  • 写回答

2条回答 默认 最新

  • doulan7166 2015-04-09 05:28
    关注

    Short answer:

    Entities saved with a parent can be queried with non-ancestor queries (where you don't specify an ancestor with the Query.Ancestor() method). Obviously the filtered property must be indexed.

    To put things in place:

    In your example contrary to your naming key1 is the parent key and kparent is the key you save the entity with.

    When you create an Ancestor query with the Query.Ancestor() method, the ancestor filter limits the results to the specified entity and its descendants: so you specify a parent key, and the results will be the entities with this key (0 or 1 entity) and those where this is the parent key!

    In your example you find the result because the entity's key is exactly the one you specified. Usually ancestor queries are used in a way that the parent key is specified (not the entity's key itself) which in your example is key1.

    Important to note: Ancestor queries are strongly consistent. This means that if you save an entity with a parent and you perform an ancestor query right after that (where the ancestor filter is the same parent of course), you will see the saved entity immediately in the query results.

    Non-ancestor queries are only eventually consistent. This means that if you save an entity and you perform a non-ancestor query right after that, chances that the query will not include the newly saved entity is very high, which is most likely your case.

    An index by a property only depends on the property's value and is independent from the key of the entity, so it doesn't matter if the key has a parent or not. Once the index entry for the property is created for a new entity, queries filtering by that property will include the entity. This may take as short as a few millisec or as "long" as a few sec (unlikely).

    Please see this relevant answer: How to filter a GAE query? which also explains ancestor keys and queries.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题