Efficient way to query all records where a field i

2019-05-07 15:20发布

I want to query an indexed field efficiently to retrieve all records where the indexed field is not null (present in the index). The field to be queried contains a Ref<T> to another entity, in case this is relevant.

What I could do is an inequality search, like .filter/.filterKey("fieldname >=", "a"), where a is the smallest ASCII that I want to grab.

But is this efficient? Or can I do an equality search somehow, which returns all records that are present in the index?

--

This is how my data looks like: I want to filter all records where the "overlay" column has a key, and omit those where the field is not set. I would like to use an equality filter if possible, so I don't need a composite index (as I am filtering on other fields at the same time).

snapshot of data

--

I can use this to test for != null

.filter("user >", "\uFFFD");

and this to test for == null

.filter("user <", "\uFFFD");

I guess that's not the way it should be done. Is there a way to solve this problem using equality instead of inequality?

2条回答
Explosion°爆炸
2楼-- · 2019-05-07 15:55

If you want to query for entities which have a null value for an indexed field:

ofy().load().type(Thing.class).filter("fieldname !=", null)

However, this is not the same thing as an equality filter. Under the covers, GAE treats != as a pair of filters (> and <) and this brings with it the limitations of inequality filters.

If you need an equality filter on "not null", create a synthetic indexed field in your entity which is populated using an @OnSave method. You can use a partial index on the 'true' value to limit the cost of indexing this extra data (ie, @Index(IfTrue.class))

查看更多
Anthone
3楼-- · 2019-05-07 16:09

If your property is String, a more efficient way than "!= null" is to use:

ofy().load().type(Thing.class).filter("fieldname >=", "");

Similarly, you can use > 0 if your property is a positive number, etc.

查看更多
登录 后发表回答