简体   繁体   English

GAE数据存储-如何创建非索引实体属性

[英]GAE Datastore - How to create non-indexed Entity Property

I'm using GAE flexible environment, .Net, and Datastore. 我正在使用GAE灵活环境,.Net和数据存储区。 The issue I'm having is that I cannot figure out how to remove the index that Datastore automatically adds to every property of an Entity. 我遇到的问题是我无法弄清楚如何删除数据存储自动添加到实体的每个属性的索引。

I know I can view the Entities on the GCP site and remove the index from there but it only removes the index for that specific Entity. 我知道我可以在GCP网站上查看实体并从那里删除索引,但它只会删除该特定实体的索引。 Any knew Entities added will still index that property. 添加的所有已知实体仍将对该属性建立索引。

For some of the other supported languages there are attributes you can add to the class that will tell Datastore not to index that property. 对于某些其他受支持的语言,您可以将一些属性添加到该类中,以告诉Datastore不要对该属性建立索引。 There must be a way to do this with .Net but I have not found it. .net必须有一种方法可以做到这一点,但我还没有找到。

Here is a sample class: 这是一个示例类:

public class MeetingRoom
{
        [Key]
        [Required]
        public long Id { get; set; }

        [Required]
        public string Name { get; set; }

        [DataType(DataType.MultilineText)]
        public string Description { get; set; }

        public ICollection<Notification> Notifications { get; set; }
}

Let's say the Notifications property will never be used in a query. 假设Notifications属性将永远不会在查询中使用。 So instead of paying the cost of having a child Entity for each Notification I simply JsonConvert the property and store it as a string. 因此,无需为每个Notification支付拥有子实体的开销,我只需JsonConvert属性并将其存储为字符串即可。

The resulting Entity looks like this: 产生的实体看起来像这样:

MeetingRoom room = GetMeetingRoom(34);
Entity entity = new Entity ()
{
    Key = room.Id.ToKey(),
    ["Name"] = room.Name,
    ["Description"] = room.Description,
    ["Notifications"] = JsonConvert.SerializeObject(room.Notifications)
}

For this example, Notifications expire after a month and are removed from the list. 对于此示例,通知将在一个月后过期,并从列表中删除。 Because of this, the Entity will never reach the 1MiB limit imposed by Datastore. 因此,实体永远不会达到数据存储区施加的1MiB限制。 However, because Notifications is an indexed string it is limitted to 1500 characters. 但是,由于Notifications是一个索引字符串,因此限制为1500个字符。

It's this 1500 character limit that I am banging up against. 我要挑战的正是这个1500个字符的限制。 If I can figure out how to keep Datastore from indexing that property then I can live happily within the 1MiB Entity limit. 如果我能弄清楚如何阻止数据存储对该属性建立索引,那么我可以在1MiB实体限制内愉快地生活。

Can anyone help me with this? 谁能帮我这个?


UPDATE UPDATE

Here is the solution as per the answer by @QuestionAndAnswer 这是@QuestionAndAnswer回答的解决方案

In the Entity, setting the Value property's ExcludeFromIndexes to true will prevent the property from being indexed. 在实体中,将Value属性的ExcludeFromIndexes设置为true将防止对该属性建立索引。

From my example above, the corrected Entity looks like this: 在上面的示例中,更正后的Entity如下所示:

MeetingRoom room = GetMeetingRoom(34);
Entity entity = new Entity ()
{
    Key = room.Id.ToKey(),
    ["Name"] = room.Name,
    ["Description"] = room.Description,
    ["Notifications"] = new Value()
    {
        StringValue = JsonConvert.SerializeObject(room.Notifications),
        ExcludeFromIndexes = true
    }
}

Looking at the example in documentation, you should pass ExcludeFromIndexes on entity creation step. 查看文档中的示例,您应该在实体创建步骤中传递ExcludeFromIndexes

https://cloud.google.com/datastore/docs/concepts/indexes#unindexed_properties https://cloud.google.com/datastore/docs/concepts/indexes#unindexed_properties

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM