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