AppEngine datastore query for all entities that have a given property (Java)

為{幸葍}努か 提交于 2019-12-23 16:10:24

问题


I am trying to figure out an elegant way to query all of the entities in an AppEngine datastore that have a certain property. Since entities that lack a property aren't included in an index, basically what I want to do is to retrieve the index for a given property. I'm sure it's possible to do something like:

Filter bigger = new FilterPredicate(PROPERTY,
                  FilterOperator.GREATER_THAN_OR_EQUAL,
                  0);

Filter smaller = new FilterPredicate(PROPERTY,
                  FilterOperator.LESS_THAN_OR_EQUAL,
                  0);

Filter present = CompositeFilterOperator.or(bigger, smaller);

Query q = new Query(KIND).setFilter(present);

but it doesn't look like a very elegant (or efficient) solution. Does anyone have a better idea?


回答1:


If you don't need entities with null values, you can use this:

Filter filter = new FilterPredicate(PROPERTY, FilterOperator.NOT_EQUAL, null);

It may look simpler, but NOT_EQUAL filter actually results in two separate queries - just as your solution, unless App Engine is smart about null values - that I don't know.




回答2:


The property I am interested in is actually a list which may have lots of values, so I decided to leave it unindexed, and add a separate boolean property HAS_LIST. That allows me to do a single query for results at the cost of a slightly bigger entity. Still not a very elegant solution, but perhaps a little more efficient.



来源:https://stackoverflow.com/questions/22199729/appengine-datastore-query-for-all-entities-that-have-a-given-property-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!