From 9e6253bca157a57e7a210a80b3e5509084b4bf53 Mon Sep 17 00:00:00 2001 From: Tim Barron Date: Wed, 16 Mar 2022 15:20:29 -0700 Subject: Sync from upstream. This sync will bring goog/upstream-master into agreement with aosp/upstream-master. Unfortunately merging any permutation of aosp/upstream-master or androidx-main into goog/upstream-master or androidx-platform-dev isn't doable because I don't own some of the commits in the chain. Descriptions: ====================================================================== Refactor DocumentStore::Initialize to improve readability of document store recovery. ====================================================================== Remove non-NDK API usages of ICU4C in libicing. ====================================================================== Move IcuDataFileHelper to the testing directory since it is a test-only util. ====================================================================== Support dump function for DocumentStore ====================================================================== Switch to use PRead rather than MMap in the proto log. ====================================================================== Support dump function for main/lite index and lexicon ====================================================================== Fix LiteIndex::AppendHits ====================================================================== Enable and fix DocumentStoreTest.LoadScoreCacheAndInitializeSuccessfully ====================================================================== Fix MainIndex::GetStorageInfo. ====================================================================== Fix icing-search-engine_fuzz_test by making IcuLanguageSegmenterIterator::Advance non-recursive. ====================================================================== Allow to return additional information for deleted documents in DeleteByQuery ====================================================================== Using enum class in Token::Type for better type safety. ====================================================================== Normalize Tokens by Token type when retrieving snippets ================ Rename max_window_bytes to max_window_utf32_length, Delete the max_tokens_per_doc field in IcingSearchEngineOptions. ================ Handle suggestion namespace ownership. ================ Fix OkStatus() is not a valid argument to StatusOr in Main_index.RetrieveMoreHits. ================ Allow advancing when current indices are negative in CharacterIterator ================ Adds support for verbatim tokenization and indexing in IcingLib ================ Renames TokenizerIterator Reset functions ================ Add term_match_type to SuggestionSpec proto ================ Unify the C++ proto enum style ================ Allow zero property weights in IcingLib ================ Replace refs to c lib headers w/ c++ stdlib equivalents. ================ Update IDF component of BM25F Calculator in IcingLib ================ Expose QuerySuggestions API. ================ Change the tokenizer used in QuerySuggest. ================ Add SectionWeights API to Icing. ================ Apply SectionWeights to BM25F Scoring. ================ Replaces uses of u_strTo/FromUTF32 w/ u_strTo/FromUTF8. ================ Implement ranking in FindTermByPrefix. ================ Fork proto's GzipStream into Icing Lib. ================ Remove token limit behavior from index-processor. ================ Remove no-longer-used write paths for file-backed-proto-log. ================ Modify segmentation rules to consider any segment that begins with a non-Ascii alphanumeric character as valid ================= Implement CalculateNormalizedMatchLength for IcuNormalizer. ================ Add additional benchmark cases that were useful in developing submatching and CalculateNormalizedMatchLength for IcuNormalizer ================= Switch NormalizationMap from static const std::unordered_map& to static const std::unordered_map *const. ================== Add Initialization Count marker file to break out of crash loops. ================== Delete the Simple Normalizer. ================== Add submatch information to identify a submatch within a document. ================== Bug: 147509515 Bug: 149610413 Bug: 152934343 Bug: 158089703 Bug: 185845269 Bug: 195720764 Bug: 196257995 Bug: 196771754 Bug: 202308641 Bug: 203700301 Bug: 204333391 Bug: 205209589 Bug: 206147728 Bug: 209071710 Bug: 209993976 Bug: 218413237 Bug: 223549255 Change-Id: I87c5da3a80463d62b8d24e8ff8a9a91952d3fc64 --- .../google/android/icing/IcingSearchEngine.java | 25 ++++++++++++ .../android/icing/IcingSearchEngineTest.java | 45 ++++++++++++++++++++++ 2 files changed, 70 insertions(+) (limited to 'java') diff --git a/java/src/com/google/android/icing/IcingSearchEngine.java b/java/src/com/google/android/icing/IcingSearchEngine.java index 1f5fb51..95e0c84 100644 --- a/java/src/com/google/android/icing/IcingSearchEngine.java +++ b/java/src/com/google/android/icing/IcingSearchEngine.java @@ -43,6 +43,8 @@ import com.google.android.icing.proto.SearchSpecProto; import com.google.android.icing.proto.SetSchemaResultProto; import com.google.android.icing.proto.StatusProto; import com.google.android.icing.proto.StorageInfoResultProto; +import com.google.android.icing.proto.SuggestionResponse; +import com.google.android.icing.proto.SuggestionSpecProto; import com.google.android.icing.proto.UsageReport; import com.google.protobuf.ExtensionRegistryLite; import com.google.protobuf.InvalidProtocolBufferException; @@ -369,6 +371,26 @@ public class IcingSearchEngine implements Closeable { } } + @NonNull + public SuggestionResponse searchSuggestions(@NonNull SuggestionSpecProto suggestionSpec) { + byte[] suggestionResponseBytes = nativeSearchSuggestions(this, suggestionSpec.toByteArray()); + if (suggestionResponseBytes == null) { + Log.e(TAG, "Received null suggestionResponseBytes from native."); + return SuggestionResponse.newBuilder() + .setStatus(StatusProto.newBuilder().setCode(StatusProto.Code.INTERNAL)) + .build(); + } + + try { + return SuggestionResponse.parseFrom(suggestionResponseBytes, EXTENSION_REGISTRY_LITE); + } catch (InvalidProtocolBufferException e) { + Log.e(TAG, "Error parsing suggestionResponseBytes.", e); + return SuggestionResponse.newBuilder() + .setStatus(StatusProto.newBuilder().setCode(StatusProto.Code.INTERNAL)) + .build(); + } + } + @NonNull public DeleteByNamespaceResultProto deleteByNamespace(@NonNull String namespace) { throwIfClosed(); @@ -604,4 +626,7 @@ public class IcingSearchEngine implements Closeable { private static native byte[] nativeGetStorageInfo(IcingSearchEngine instance); private static native byte[] nativeReset(IcingSearchEngine instance); + + private static native byte[] nativeSearchSuggestions( + IcingSearchEngine instance, byte[] suggestionSpecBytes); } diff --git a/java/tests/instrumentation/src/com/google/android/icing/IcingSearchEngineTest.java b/java/tests/instrumentation/src/com/google/android/icing/IcingSearchEngineTest.java index 0cee80c..a46814c 100644 --- a/java/tests/instrumentation/src/com/google/android/icing/IcingSearchEngineTest.java +++ b/java/tests/instrumentation/src/com/google/android/icing/IcingSearchEngineTest.java @@ -51,7 +51,11 @@ import com.google.android.icing.proto.StatusProto; import com.google.android.icing.proto.StorageInfoResultProto; import com.google.android.icing.proto.StringIndexingConfig; import com.google.android.icing.proto.StringIndexingConfig.TokenizerType; +import com.google.android.icing.proto.SuggestionResponse; +import com.google.android.icing.proto.SuggestionSpecProto; +import com.google.android.icing.proto.SuggestionSpecProto.SuggestionScoringSpecProto; import com.google.android.icing.proto.TermMatchType; +import com.google.android.icing.proto.TermMatchType.Code; import com.google.android.icing.proto.UsageReport; import com.google.android.icing.IcingSearchEngine; import java.io.File; @@ -623,6 +627,47 @@ public final class IcingSearchEngineTest { assertThat(match).isEqualTo("𐀂𐀃"); } + @Test + public void testSearchSuggestions() { + assertStatusOk(icingSearchEngine.initialize().getStatus()); + + SchemaTypeConfigProto emailTypeConfig = createEmailTypeConfig(); + SchemaProto schema = SchemaProto.newBuilder().addTypes(emailTypeConfig).build(); + assertThat( + icingSearchEngine + .setSchema(schema, /*ignoreErrorsAndDeleteDocuments=*/ false) + .getStatus() + .getCode()) + .isEqualTo(StatusProto.Code.OK); + + DocumentProto emailDocument1 = + createEmailDocument("namespace", "uri1").toBuilder() + .addProperties(PropertyProto.newBuilder().setName("subject").addStringValues("fo")) + .build(); + DocumentProto emailDocument2 = + createEmailDocument("namespace", "uri2").toBuilder() + .addProperties(PropertyProto.newBuilder().setName("subject").addStringValues("foo")) + .build(); + assertStatusOk(icingSearchEngine.put(emailDocument1).getStatus()); + assertStatusOk(icingSearchEngine.put(emailDocument2).getStatus()); + + SuggestionSpecProto suggestionSpec = + SuggestionSpecProto.newBuilder() + .setPrefix("f") + .setNumToReturn(10) + .setScoringSpec( + SuggestionScoringSpecProto.newBuilder() + .setScoringMatchType(Code.EXACT_ONLY) + .build()) + .build(); + + SuggestionResponse response = icingSearchEngine.searchSuggestions(suggestionSpec); + assertStatusOk(response.getStatus()); + assertThat(response.getSuggestionsList()).hasSize(2); + assertThat(response.getSuggestions(0).getQuery()).isEqualTo("foo"); + assertThat(response.getSuggestions(1).getQuery()).isEqualTo("fo"); + } + private static void assertStatusOk(StatusProto status) { assertWithMessage(status.getMessage()).that(status.getCode()).isEqualTo(StatusProto.Code.OK); } -- cgit v1.2.3