1 // Copyright (C) 2021 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef ICING_QUERY_SUGGESTION_PROCESSOR_H_ 16 #define ICING_QUERY_SUGGESTION_PROCESSOR_H_ 17 18 #include <cstdint> 19 #include <memory> 20 #include <vector> 21 22 #include "icing/text_classifier/lib3/utils/base/statusor.h" 23 #include "icing/index/embed/embedding-index.h" 24 #include "icing/index/index.h" 25 #include "icing/index/numeric/numeric-index.h" 26 #include "icing/proto/search.pb.h" 27 #include "icing/schema/schema-store.h" 28 #include "icing/store/document-store.h" 29 #include "icing/tokenization/language-segmenter.h" 30 #include "icing/transform/normalizer.h" 31 #include "icing/util/clock.h" 32 33 namespace icing { 34 namespace lib { 35 36 // Processes SuggestionSpecProtos and retrieves the specified TermMedaData that 37 // satisfies the prefix and its restrictions. This also performs ranking, and 38 // returns TermMetaData ordered by their hit count. 39 class SuggestionProcessor { 40 public: 41 // Factory function to create a SuggestionProcessor which does not take 42 // ownership of any input components, and all pointers must refer to valid 43 // objects that outlive the created SuggestionProcessor instance. 44 // 45 // Returns: 46 // An SuggestionProcessor on success 47 // FAILED_PRECONDITION if any of the pointers is null. 48 static libtextclassifier3::StatusOr<std::unique_ptr<SuggestionProcessor>> 49 Create(Index* index, const NumericIndex<int64_t>* numeric_index, 50 const EmbeddingIndex* embedding_index, 51 const LanguageSegmenter* language_segmenter, 52 const Normalizer* normalizer, const DocumentStore* document_store, 53 const SchemaStore* schema_store, const Clock* clock); 54 55 // Query suggestions based on the given SuggestionSpecProto. 56 // 57 // Returns: 58 // On success, 59 // - One vector that represents the entire TermMetadata 60 // INTERNAL_ERROR on all other errors 61 libtextclassifier3::StatusOr<std::vector<TermMetadata>> QuerySuggestions( 62 const SuggestionSpecProto& suggestion_spec, int64_t current_time_ms); 63 64 private: 65 explicit SuggestionProcessor(Index* index, 66 const NumericIndex<int64_t>* numeric_index, 67 const EmbeddingIndex* embedding_index, 68 const LanguageSegmenter* language_segmenter, 69 const Normalizer* normalizer, 70 const DocumentStore* document_store, 71 const SchemaStore* schema_store, 72 const Clock* clock); 73 74 // Not const because we could modify/sort the TermMetaData buffer in the lite 75 // index. 76 Index& index_; // Does not own. 77 const NumericIndex<int64_t>& numeric_index_; // Does not own. 78 const EmbeddingIndex& embedding_index_; // Does not own. 79 const LanguageSegmenter& language_segmenter_; // Does not own. 80 const Normalizer& normalizer_; // Does not own. 81 const DocumentStore& document_store_; // Does not own. 82 const SchemaStore& schema_store_; // Does not own. 83 const Clock& clock_; // Does not own. 84 }; 85 86 } // namespace lib 87 } // namespace icing 88 89 #endif // ICING_QUERY_SUGGESTION_PROCESSOR_H_ 90