xref: /aosp_15_r20/external/icing/icing/result/result-retriever-v2.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
1 // Copyright (C) 2022 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_RESULT_RETRIEVER_V2_H_
16 #define ICING_RESULT_RETRIEVER_V2_H_
17 
18 #include <cstdint>
19 #include <memory>
20 #include <unordered_map>
21 #include <utility>
22 #include <vector>
23 
24 #include "icing/text_classifier/lib3/utils/base/statusor.h"
25 #include "icing/proto/search.pb.h"
26 #include "icing/result/page-result.h"
27 #include "icing/result/result-state-v2.h"
28 #include "icing/result/snippet-retriever.h"
29 #include "icing/schema/schema-store.h"
30 #include "icing/scoring/scored-document-hit.h"
31 #include "icing/store/document-store.h"
32 #include "icing/tokenization/language-segmenter.h"
33 #include "icing/transform/normalizer.h"
34 
35 namespace icing {
36 namespace lib {
37 
38 class GroupResultLimiterV2 {
39  public:
GroupResultLimiterV2()40   GroupResultLimiterV2() {}
41 
42   virtual ~GroupResultLimiterV2() = default;
43 
44   // Returns true if the scored_document_hit should be removed.
45   virtual bool ShouldBeRemoved(
46       const ScoredDocumentHit& scored_document_hit,
47       const std::unordered_map<int32_t, int>& entry_id_group_id_map,
48       const DocumentStore& document_store,
49       std::vector<int>& group_result_limits,
50       ResultSpecProto::ResultGroupingType result_group_type,
51       int64_t current_time_ms) const;
52 };
53 
54 class ResultRetrieverV2 {
55  public:
56   // Factory function to create a ResultRetrieverV2 which does not take
57   // ownership of any input components, and all pointers must refer to valid
58   // objects that outlive the created ResultRetrieverV2 instance.
59   //
60   // Returns:
61   //   A ResultRetrieverV2 on success
62   //   FAILED_PRECONDITION on any null pointer input
63   static libtextclassifier3::StatusOr<std::unique_ptr<ResultRetrieverV2>>
64   Create(const DocumentStore* doc_store, const SchemaStore* schema_store,
65          const LanguageSegmenter* language_segmenter,
66          const Normalizer* normalizer,
67          std::unique_ptr<const GroupResultLimiterV2> group_result_limiter =
68              std::make_unique<const GroupResultLimiterV2>());
69 
70   // Retrieves results (pairs of DocumentProtos and SnippetProtos) with the
71   // given ResultState which holds document and snippet information. It pulls
72   // out the next top rank documents from ResultState, retrieves the documents
73   // from storage, updates ResultState, and finally wraps the result + other
74   // information into PageResult. The expected number of documents to return is
75   // min(num_per_page, the number of all scored document hits) inside
76   // ResultState.
77   //
78   // The number of snippets to return is based on the total number of snippets
79   // needed and number of snippets that have already been returned previously
80   // for the same query. The order of results returned will be sorted by
81   // scored_document_hit_comparator inside ResultState.
82   //
83   // An additional boolean value will be returned, indicating if ResultState has
84   // remaining documents to be retrieved next round.
85   //
86   // All errors will be ignored. It will keep retrieving the next document and
87   // valid documents will be included in PageResult.
88   //
89   // Returns:
90   //   std::pair<PageResult, bool>
91   std::pair<PageResult, bool> RetrieveNextPage(ResultStateV2& result_state,
92                                                int64_t current_time_ms) const;
93 
94  private:
ResultRetrieverV2(const DocumentStore * doc_store,std::unique_ptr<SnippetRetriever> snippet_retriever,std::unique_ptr<const GroupResultLimiterV2> group_result_limiter)95   explicit ResultRetrieverV2(
96       const DocumentStore* doc_store,
97       std::unique_ptr<SnippetRetriever> snippet_retriever,
98       std::unique_ptr<const GroupResultLimiterV2> group_result_limiter)
99       : doc_store_(*doc_store),
100         snippet_retriever_(std::move(snippet_retriever)),
101         group_result_limiter_(std::move(group_result_limiter)) {}
102 
103   const DocumentStore& doc_store_;
104   std::unique_ptr<SnippetRetriever> snippet_retriever_;
105   const std::unique_ptr<const GroupResultLimiterV2> group_result_limiter_;
106 };
107 
108 }  // namespace lib
109 }  // namespace icing
110 
111 #endif  // ICING_RESULT_RETRIEVER_V2_H_
112