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