xref: /aosp_15_r20/external/icing/icing/query/suggestion-processor.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
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/feature-flags.h"
24 #include "icing/index/embed/embedding-index.h"
25 #include "icing/index/index.h"
26 #include "icing/index/numeric/numeric-index.h"
27 #include "icing/index/term-metadata.h"
28 #include "icing/proto/search.pb.h"
29 #include "icing/schema/schema-store.h"
30 #include "icing/store/document-store.h"
31 #include "icing/tokenization/language-segmenter.h"
32 #include "icing/transform/normalizer.h"
33 #include "icing/util/clock.h"
34 
35 namespace icing {
36 namespace lib {
37 
38 // Processes SuggestionSpecProtos and retrieves the specified TermMedaData that
39 // satisfies the prefix and its restrictions. This also performs ranking, and
40 // returns TermMetaData ordered by their hit count.
41 class SuggestionProcessor {
42  public:
43   // Factory function to create a SuggestionProcessor which does not take
44   // ownership of any input components, and all pointers must refer to valid
45   // objects that outlive the created SuggestionProcessor instance.
46   //
47   // Returns:
48   //   An SuggestionProcessor on success
49   //   FAILED_PRECONDITION if any of the pointers is null.
50   static libtextclassifier3::StatusOr<std::unique_ptr<SuggestionProcessor>>
51   Create(Index* index, const NumericIndex<int64_t>* numeric_index,
52          const EmbeddingIndex* embedding_index,
53          const LanguageSegmenter* language_segmenter,
54          const Normalizer* normalizer, const DocumentStore* document_store,
55          const SchemaStore* schema_store, const Clock* clock,
56          const FeatureFlags* feature_flags);
57 
58   // Query suggestions based on the given SuggestionSpecProto.
59   //
60   // Returns:
61   //   On success,
62   //     - One vector that represents the entire TermMetadata
63   //   INTERNAL_ERROR on all other errors
64   libtextclassifier3::StatusOr<std::vector<TermMetadata>> QuerySuggestions(
65       const SuggestionSpecProto& suggestion_spec, int64_t current_time_ms);
66 
67  private:
68   explicit SuggestionProcessor(
69       Index* index, const NumericIndex<int64_t>* numeric_index,
70       const EmbeddingIndex* embedding_index,
71       const LanguageSegmenter* language_segmenter, const Normalizer* normalizer,
72       const DocumentStore* document_store, const SchemaStore* schema_store,
73       const Clock* clock, const FeatureFlags* feature_flags);
74 
75   // Not const because we could modify/sort the TermMetaData buffer in the lite
76   // index.
77   Index& index_;                                 // Does not own.
78   const NumericIndex<int64_t>& numeric_index_;   // Does not own.
79   const EmbeddingIndex& embedding_index_;        // Does not own.
80   const LanguageSegmenter& language_segmenter_;  // Does not own.
81   const Normalizer& normalizer_;                 // Does not own.
82   const DocumentStore& document_store_;          // Does not own.
83   const SchemaStore& schema_store_;              // Does not own.
84   const Clock& clock_;                           // Does not own.
85   const FeatureFlags& feature_flags_;            // Does not own.
86 };
87 
88 }  // namespace lib
89 }  // namespace icing
90 
91 #endif  // ICING_QUERY_SUGGESTION_PROCESSOR_H_
92