1 // Copyright (C) 2019 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_SCORING_SCORING_PROCESSOR_H_ 16 #define ICING_SCORING_SCORING_PROCESSOR_H_ 17 18 #include <cstdint> 19 #include <memory> 20 #include <string> 21 #include <unordered_map> 22 #include <utility> 23 #include <vector> 24 25 #include "icing/text_classifier/lib3/utils/base/statusor.h" 26 #include "icing/feature-flags.h" 27 #include "icing/index/embed/embedding-query-results.h" 28 #include "icing/index/iterator/doc-hit-info-iterator.h" 29 #include "icing/join/join-children-fetcher.h" 30 #include "icing/proto/logging.pb.h" 31 #include "icing/proto/scoring.pb.h" 32 #include "icing/schema/schema-store.h" 33 #include "icing/scoring/scored-document-hit.h" 34 #include "icing/scoring/scorer.h" 35 #include "icing/store/document-store.h" 36 37 namespace icing { 38 namespace lib { 39 40 // ScoringProcessor is the top-level class that handles scoring. 41 class ScoringProcessor { 42 public: 43 // Factory function to create a ScoringProcessor which does not take ownership 44 // of any input components, and all pointers must refer to valid objects that 45 // outlive the created ScoringProcessor instance. 46 // 47 // Returns: 48 // A ScoringProcessor on success 49 // FAILED_PRECONDITION on any null pointer input 50 static libtextclassifier3::StatusOr<std::unique_ptr<ScoringProcessor>> Create( 51 const ScoringSpecProto& scoring_spec, 52 SearchSpecProto::EmbeddingQueryMetricType::Code 53 default_semantic_metric_type, 54 const DocumentStore* document_store, const SchemaStore* schema_store, 55 int64_t current_time_ms, const JoinChildrenFetcher* join_children_fetcher, 56 const EmbeddingQueryResults* embedding_query_results, 57 const FeatureFlags* feature_flags); 58 59 // Assigns scores to DocHitInfos from the given DocHitInfoIterator and returns 60 // a vector of ScoredDocumentHits. The size of results is no more than 61 // num_to_score. The order of results is the same as DocHitInfos from 62 // DocHitInfoIterator. 63 // 64 // If necessary, query_term_iterators is used to compute the BM25F relevance 65 // score. NOTE: if the scoring spec doesn't require a scoring strategy, all 66 // ScoredDocumentHits will be assigned a default score 0. 67 std::vector<ScoredDocumentHit> Score( 68 std::unique_ptr<DocHitInfoIterator> doc_hit_info_iterator, 69 int num_to_score, 70 std::unordered_map<std::string, std::unique_ptr<DocHitInfoIterator>>* 71 query_term_iterators = nullptr, 72 QueryStatsProto::SearchStats* search_stats = nullptr); 73 74 private: ScoringProcessor(std::unique_ptr<Scorer> scorer)75 explicit ScoringProcessor(std::unique_ptr<Scorer> scorer) 76 : scorer_(std::move(scorer)) {} 77 78 // The component that assigns scores to documents. 79 std::unique_ptr<Scorer> scorer_; 80 }; 81 82 } // namespace lib 83 } // namespace icing 84 85 #endif // ICING_SCORING_SCORING_PROCESSOR_H_ 86