xref: /aosp_15_r20/external/icing/icing/query/query-utils.cc (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
1*8b6cd535SAndroid Build Coastguard Worker // Copyright (C) 2022 Google LLC
2*8b6cd535SAndroid Build Coastguard Worker //
3*8b6cd535SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*8b6cd535SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*8b6cd535SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*8b6cd535SAndroid Build Coastguard Worker //
7*8b6cd535SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
8*8b6cd535SAndroid Build Coastguard Worker //
9*8b6cd535SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*8b6cd535SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*8b6cd535SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*8b6cd535SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*8b6cd535SAndroid Build Coastguard Worker // limitations under the License.
14*8b6cd535SAndroid Build Coastguard Worker 
15*8b6cd535SAndroid Build Coastguard Worker #include "icing/query/query-utils.h"
16*8b6cd535SAndroid Build Coastguard Worker 
17*8b6cd535SAndroid Build Coastguard Worker #include <string_view>
18*8b6cd535SAndroid Build Coastguard Worker #include <unordered_set>
19*8b6cd535SAndroid Build Coastguard Worker 
20*8b6cd535SAndroid Build Coastguard Worker #include "icing/text_classifier/lib3/utils/base/statusor.h"
21*8b6cd535SAndroid Build Coastguard Worker #include "icing/index/iterator/doc-hit-info-iterator-filter.h"
22*8b6cd535SAndroid Build Coastguard Worker #include "icing/schema/schema-store.h"
23*8b6cd535SAndroid Build Coastguard Worker #include "icing/store/document-filter-data.h"
24*8b6cd535SAndroid Build Coastguard Worker #include "icing/store/document-store.h"
25*8b6cd535SAndroid Build Coastguard Worker #include "icing/store/namespace-id.h"
26*8b6cd535SAndroid Build Coastguard Worker 
27*8b6cd535SAndroid Build Coastguard Worker namespace icing {
28*8b6cd535SAndroid Build Coastguard Worker namespace lib {
29*8b6cd535SAndroid Build Coastguard Worker 
30*8b6cd535SAndroid Build Coastguard Worker namespace {
31*8b6cd535SAndroid Build Coastguard Worker 
ConvertNamespaceToIds(const DocumentStore & document_store,const SearchSpecProto & search_spec)32*8b6cd535SAndroid Build Coastguard Worker std::unordered_set<NamespaceId> ConvertNamespaceToIds(
33*8b6cd535SAndroid Build Coastguard Worker     const DocumentStore& document_store, const SearchSpecProto& search_spec) {
34*8b6cd535SAndroid Build Coastguard Worker   std::unordered_set<NamespaceId> ids;
35*8b6cd535SAndroid Build Coastguard Worker   for (std::string_view name_space : search_spec.namespace_filters()) {
36*8b6cd535SAndroid Build Coastguard Worker     auto namespace_id_or = document_store.GetNamespaceId(name_space);
37*8b6cd535SAndroid Build Coastguard Worker 
38*8b6cd535SAndroid Build Coastguard Worker     // If we can't find the NamespaceId, just throw it away
39*8b6cd535SAndroid Build Coastguard Worker     if (namespace_id_or.ok()) {
40*8b6cd535SAndroid Build Coastguard Worker       ids.insert(namespace_id_or.ValueOrDie());
41*8b6cd535SAndroid Build Coastguard Worker     }
42*8b6cd535SAndroid Build Coastguard Worker   }
43*8b6cd535SAndroid Build Coastguard Worker   return ids;
44*8b6cd535SAndroid Build Coastguard Worker }
45*8b6cd535SAndroid Build Coastguard Worker 
ConvertExactSchemaTypeToIds(const SchemaStore & schema_store,const SearchSpecProto & search_spec)46*8b6cd535SAndroid Build Coastguard Worker std::unordered_set<SchemaTypeId> ConvertExactSchemaTypeToIds(
47*8b6cd535SAndroid Build Coastguard Worker     const SchemaStore& schema_store, const SearchSpecProto& search_spec) {
48*8b6cd535SAndroid Build Coastguard Worker   std::unordered_set<SchemaTypeId> ids;
49*8b6cd535SAndroid Build Coastguard Worker   ids.reserve(search_spec.schema_type_filters_size());
50*8b6cd535SAndroid Build Coastguard Worker   for (std::string_view schema_type : search_spec.schema_type_filters()) {
51*8b6cd535SAndroid Build Coastguard Worker     libtextclassifier3::StatusOr<SchemaTypeId> schema_type_id_or =
52*8b6cd535SAndroid Build Coastguard Worker         schema_store.GetSchemaTypeId(schema_type);
53*8b6cd535SAndroid Build Coastguard Worker 
54*8b6cd535SAndroid Build Coastguard Worker     // If we can't find the SchemaTypeId, just throw it away
55*8b6cd535SAndroid Build Coastguard Worker     if (schema_type_id_or.ok()) {
56*8b6cd535SAndroid Build Coastguard Worker       ids.insert(schema_type_id_or.ValueOrDie());
57*8b6cd535SAndroid Build Coastguard Worker     }
58*8b6cd535SAndroid Build Coastguard Worker   }
59*8b6cd535SAndroid Build Coastguard Worker   return ids;
60*8b6cd535SAndroid Build Coastguard Worker }
61*8b6cd535SAndroid Build Coastguard Worker 
62*8b6cd535SAndroid Build Coastguard Worker }  // namespace
63*8b6cd535SAndroid Build Coastguard Worker 
GetFilterOptions(const SearchSpecProto & search_spec,const DocumentStore & document_store,const SchemaStore & schema_store)64*8b6cd535SAndroid Build Coastguard Worker DocHitInfoIteratorFilter::Options GetFilterOptions(
65*8b6cd535SAndroid Build Coastguard Worker     const SearchSpecProto& search_spec, const DocumentStore& document_store,
66*8b6cd535SAndroid Build Coastguard Worker     const SchemaStore& schema_store) {
67*8b6cd535SAndroid Build Coastguard Worker   DocHitInfoIteratorFilter::Options options;
68*8b6cd535SAndroid Build Coastguard Worker 
69*8b6cd535SAndroid Build Coastguard Worker   // Precompute all the NamespaceIds
70*8b6cd535SAndroid Build Coastguard Worker   options.filter_by_namespace_id_enabled =
71*8b6cd535SAndroid Build Coastguard Worker       !search_spec.namespace_filters().empty();
72*8b6cd535SAndroid Build Coastguard Worker   options.target_namespace_ids =
73*8b6cd535SAndroid Build Coastguard Worker       ConvertNamespaceToIds(document_store, search_spec);
74*8b6cd535SAndroid Build Coastguard Worker 
75*8b6cd535SAndroid Build Coastguard Worker   // Precompute all the SchemaTypeIds
76*8b6cd535SAndroid Build Coastguard Worker   options.filter_by_schema_type_id_enabled =
77*8b6cd535SAndroid Build Coastguard Worker       !search_spec.schema_type_filters().empty();
78*8b6cd535SAndroid Build Coastguard Worker   options.target_schema_type_ids =
79*8b6cd535SAndroid Build Coastguard Worker       ConvertExactSchemaTypeToIds(schema_store, search_spec);
80*8b6cd535SAndroid Build Coastguard Worker   return options;
81*8b6cd535SAndroid Build Coastguard Worker }
82*8b6cd535SAndroid Build Coastguard Worker 
83*8b6cd535SAndroid Build Coastguard Worker }  // namespace lib
84*8b6cd535SAndroid Build Coastguard Worker }  // namespace icing
85