简体   繁体   English

实体的某些属性需要全文搜索,而其他则不需要

[英]Some properties of an Entity need full-text search, others don't

In my app I have an entity called Product that holds the details of the product listings to be shown on the site. 在我的应用程序中,我有一个名为Product的实体,其中包含要在网站上显示的产品清单的详细信息。 Since the name (title) and description of the product need to be full-text searchable, I am putting them in a separate Document (as defined by Google's App Engine Search API ). 由于产品的名称(标题)和描述需要全文搜索,因此我将它们放在单独的Document (由Google的App Engine 搜索API定义)。 All other properties remain in the Product entity, which is stored as per usual in the Google Datastore using Objectify . 所有其他属性都保留在Product实体中,该实体按照通常的方式使用Objectify 存储Google数据存储区中。

A slug created from the name property of the Product is the doc_id of the Document . 从创建的蛞蝓name的物业Productdoc_id中的Document This same slug is the ( String ) ID of an entity (let's call it ProductLookup ) that I will use to get the Key to the corresponding/matching Product . 相同的标记是实体(将其称为ProductLookup )的( String )ID,我将使用它来获取对应/匹配ProductKey

NOTE: The Product has an auto-generated Long id as the name property (and therefore the slug used to lookup a Product ) can change even after the creation of the Product — this way I just create a new ProductLookup entity when name changes. 注意:Product具有自动生成的Long ID,因为即使创建了Product后, name属性(因此用于查找Product的块)的name属性也可能会更改-这样,当name更改时,我只是创建一个新的ProductLookup实体。

Since I am splitting the information that would normally be in a single entity into two different objects (the Product and the Document , not to mention the extra ProductLookup entity), are there any special cases I should be watching out for? 由于我通常将单个实体中的信息拆分为两个不同的对象(“ Product和“ Document ,更不用说额外的ProductLookup实体了),我是否应该注意一些特殊情况? Any suggestions? 有什么建议么?


If I have a reference to a Product , I can use the slug to get the correspoding Document . 如果我有对Product的引用,则可以使用该插件获取相应的Document

If I have a slug, I can use ProductLookup to get a Product and use the slug as the doc_id of the Document . 如果我有一个子弹,我可以使用ProductLookup来获得一个Product并将该doc_id用作Documentdoc_id

If I find a set of Document s via Search API, I can use their doc_id s to get the ProductLookup , then the Product . 如果我通过Search API找到了一组Document ,则可以使用其doc_id来获取ProductLookup ,然后获取Product

My current project has a similar situation. 我当前的项目也有类似情况。 Originally I was using a technique like yours, and the only caveat I know of is the extra care you have to take to ensure adding and deleting products from the two sources always succeed or fail together. 最初,我使用的是像您这样的技术,唯一需要注意的是,您必须格外小心,以确保从两个来源添加和删除产品始终会成功或失败。 Datastore operations can be put in a transaction, but Search API operations will not obey the rules of that transaction. 数据存储区操作可以放入事务中,但是Search API操作将不遵守该事务的规则。 For example, if you delete from both the datastore and the search api within a transaction, and that transaction fails, the product will not be deleted from the datastore, but it may still be deleted from the search api. 例如,如果您从事务中的数据存储和搜索api中删除,但该事务失败,则不会从数据存储中删除产品,但仍可能从搜索api中删除该产品。 To ensure they stay synchronized, use a technique like this , and be sure your search api operations are last within your transactions. 为了确保它们保持同步,请使用类似这样的技术,并确保您的Search api操作位于事务的最后位置。

I have since amended my approach to save the entire object within the datastore, and also store a copy of the searchable fields in the search api. 此后,我修改了将整个对象保存在数据存储区中的方法,并且还在搜索API中存储了可搜索字段的副本。 The drawback to this approach is that you pay for the additional storage. 这种方法的缺点是您需要支付额外的存储空间。 The benefit is that when you aren't searching, retrievals are cheaper. 好处是,当您不进行搜索时,检索会更便宜。 This is especially true when Objectify finds your objects in memcache. 当Objectify在内存缓存中找到您的对象时,尤其如此。

To support my approach, I wrote my own "mini-objectify" that persists objects to the search API. 为了支持我的方法,我编写了自己的“ mini-objectify”,将对象持久保存到搜索API。 It has its own version of @Igore , so I can use the same POJO for both, and only persist the fields I choose to the search api. 它具有自己的@Igore版本,因此我可以对两者使用相同的POJO,并且仅将选择的字段保留到搜索api中。

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

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