سؤال

So lets say I have 100.000 entities of type Foo. Foo has a property bar that is indexed. Now, for a specific Foo A I want to get 10 Foos closest to A when sorting all Foos by bar.

The thing is, I do not want to do this manually since it will be really slow because of 100k+ entities.

One idea is to use offset and limit like this:

ofy().load().type(Foo.class).order("-bar").offset(offset).limit(limit).list();

but that requires that I know the position of my Foo A, which I don't.

I had a working solution to this that was a separate entitity that just stores the position of all Foos and updating that table once a minute. It got really slow and caused OutOfMemoryError, so I need something different.

Anyone wanna take a shot at this?

هل كانت مفيدة؟

المحلول

You can do something like this:

List<Foo> biger = ofy().load().type(Foo.class).filter("bar >=", A.getBar()).order("bar").limit(10).list();

List<Foo> lower = ofy().load().type(Foo.class).filter("bar <", A.getBar()).order("-bar").limit(10).list();

List<Foo> closest = getClosest(A, biger, lower);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top