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