|
1 | 1 | /*
|
2 |
| - * Copyright 2017-2020 ObjectBox Ltd. All rights reserved. |
| 2 | + * Copyright 2017-2024 ObjectBox Ltd. All rights reserved. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
31 | 31 | import io.objectbox.BoxStore;
|
32 | 32 | import io.objectbox.InternalAccess;
|
33 | 33 | import io.objectbox.Property;
|
| 34 | +import io.objectbox.annotation.HnswIndex; |
34 | 35 | import io.objectbox.exception.NonUniqueResultException;
|
35 | 36 | import io.objectbox.reactive.DataObserver;
|
36 | 37 | import io.objectbox.reactive.DataSubscriptionList;
|
@@ -71,6 +72,10 @@ public class Query<T> implements Closeable {
|
71 | 72 |
|
72 | 73 | native long[] nativeFindIds(long handle, long cursorHandle, long offset, long limit);
|
73 | 74 |
|
| 75 | + native List<ObjectWithScore<T>> nativeFindWithScores(long handle, long cursorHandle, long offset, long limit); |
| 76 | + |
| 77 | + native List<IdWithScore> nativeFindIdsWithScores(long handle, long cursorHandle, long offset, long limit); |
| 78 | + |
74 | 79 | native long nativeCount(long handle, long cursorHandle);
|
75 | 80 |
|
76 | 81 | native long nativeRemove(long handle, long cursorHandle);
|
@@ -380,6 +385,68 @@ public LazyList<T> findLazyCached() {
|
380 | 385 | return new LazyList<>(box, findIds(), true);
|
381 | 386 | }
|
382 | 387 |
|
| 388 | + /** |
| 389 | + * Like {@link #findIdsWithScores()}, but can skip and limit results. |
| 390 | + * <p> |
| 391 | + * Use to get a slice of the whole result, e.g. for "result paging". |
| 392 | + * |
| 393 | + * @param offset If greater than 0, skips this many results. |
| 394 | + * @param limit If greater than 0, returns at most this many results. |
| 395 | + */ |
| 396 | + @Nonnull |
| 397 | + public List<IdWithScore> findIdsWithScores(final long offset, final long limit) { |
| 398 | + checkOpen(); |
| 399 | + return box.internalCallWithReaderHandle(cursorHandle -> nativeFindIdsWithScores(handle, cursorHandle, offset, limit)); |
| 400 | + } |
| 401 | + |
| 402 | + /** |
| 403 | + * Finds IDs of objects matching the query associated to their query score (e.g. distance in NN search). |
| 404 | + * <p> |
| 405 | + * This only works on objects with a property with an {@link HnswIndex}. |
| 406 | + * |
| 407 | + * @return A list of {@link IdWithScore} that wraps IDs of matching objects and their score, sorted by score in |
| 408 | + * ascending order. |
| 409 | + */ |
| 410 | + @Nonnull |
| 411 | + public List<IdWithScore> findIdsWithScores() { |
| 412 | + return findIdsWithScores(0, 0); |
| 413 | + } |
| 414 | + |
| 415 | + /** |
| 416 | + * Like {@link #findWithScores()}, but can skip and limit results. |
| 417 | + * <p> |
| 418 | + * Use to get a slice of the whole result, e.g. for "result paging". |
| 419 | + * |
| 420 | + * @param offset If greater than 0, skips this many results. |
| 421 | + * @param limit If greater than 0, returns at most this many results. |
| 422 | + */ |
| 423 | + @Nonnull |
| 424 | + public List<ObjectWithScore<T>> findWithScores(final long offset, final long limit) { |
| 425 | + ensureNoFilterNoComparator(); |
| 426 | + return callInReadTx(() -> { |
| 427 | + List<ObjectWithScore<T>> results = nativeFindWithScores(handle, cursorHandle(), offset, limit); |
| 428 | + if (eagerRelations != null) { |
| 429 | + for (int i = 0; i < results.size(); i++) { |
| 430 | + resolveEagerRelationForNonNullEagerRelations(results.get(i).getObject(), i); |
| 431 | + } |
| 432 | + } |
| 433 | + return results; |
| 434 | + }); |
| 435 | + } |
| 436 | + |
| 437 | + /** |
| 438 | + * Finds objects matching the query associated to their query score (e.g. distance in NN search). |
| 439 | + * <p> |
| 440 | + * This only works on objects with a property with an {@link HnswIndex}. |
| 441 | + * |
| 442 | + * @return A list of {@link ObjectWithScore} that wraps matching objects and their score, sorted by score in |
| 443 | + * ascending order. |
| 444 | + */ |
| 445 | + @Nonnull |
| 446 | + public List<ObjectWithScore<T>> findWithScores() { |
| 447 | + return findWithScores(0, 0); |
| 448 | + } |
| 449 | + |
383 | 450 | /**
|
384 | 451 | * Creates a {@link PropertyQuery} for the given property.
|
385 | 452 | * <p>
|
|
0 commit comments