xref: /aosp_15_r20/external/icing/icing/query/query-utils.cc (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 #include "icing/query/query-utils.h"
16 
17 #include <string_view>
18 #include <unordered_set>
19 
20 #include "icing/text_classifier/lib3/utils/base/statusor.h"
21 #include "icing/index/iterator/doc-hit-info-iterator-filter.h"
22 #include "icing/schema/schema-store.h"
23 #include "icing/store/document-filter-data.h"
24 #include "icing/store/document-store.h"
25 #include "icing/store/namespace-id.h"
26 
27 namespace icing {
28 namespace lib {
29 
30 namespace {
31 
ConvertNamespaceToIds(const DocumentStore & document_store,const SearchSpecProto & search_spec)32 std::unordered_set<NamespaceId> ConvertNamespaceToIds(
33     const DocumentStore& document_store, const SearchSpecProto& search_spec) {
34   std::unordered_set<NamespaceId> ids;
35   for (std::string_view name_space : search_spec.namespace_filters()) {
36     auto namespace_id_or = document_store.GetNamespaceId(name_space);
37 
38     // If we can't find the NamespaceId, just throw it away
39     if (namespace_id_or.ok()) {
40       ids.insert(namespace_id_or.ValueOrDie());
41     }
42   }
43   return ids;
44 }
45 
ConvertExactSchemaTypeToIds(const SchemaStore & schema_store,const SearchSpecProto & search_spec)46 std::unordered_set<SchemaTypeId> ConvertExactSchemaTypeToIds(
47     const SchemaStore& schema_store, const SearchSpecProto& search_spec) {
48   std::unordered_set<SchemaTypeId> ids;
49   ids.reserve(search_spec.schema_type_filters_size());
50   for (std::string_view schema_type : search_spec.schema_type_filters()) {
51     libtextclassifier3::StatusOr<SchemaTypeId> schema_type_id_or =
52         schema_store.GetSchemaTypeId(schema_type);
53 
54     // If we can't find the SchemaTypeId, just throw it away
55     if (schema_type_id_or.ok()) {
56       ids.insert(schema_type_id_or.ValueOrDie());
57     }
58   }
59   return ids;
60 }
61 
62 }  // namespace
63 
GetFilterOptions(const SearchSpecProto & search_spec,const DocumentStore & document_store,const SchemaStore & schema_store)64 DocHitInfoIteratorFilter::Options GetFilterOptions(
65     const SearchSpecProto& search_spec, const DocumentStore& document_store,
66     const SchemaStore& schema_store) {
67   DocHitInfoIteratorFilter::Options options;
68 
69   // Precompute all the NamespaceIds
70   options.filter_by_namespace_id_enabled =
71       !search_spec.namespace_filters().empty();
72   options.target_namespace_ids =
73       ConvertNamespaceToIds(document_store, search_spec);
74 
75   // Precompute all the SchemaTypeIds
76   options.filter_by_schema_type_id_enabled =
77       !search_spec.schema_type_filters().empty();
78   options.target_schema_type_ids =
79       ConvertExactSchemaTypeToIds(schema_store, search_spec);
80   return options;
81 }
82 
83 }  // namespace lib
84 }  // namespace icing
85