• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "icing/scoring/scoring-processor.h"
16 
17 #include <memory>
18 #include <utility>
19 #include <vector>
20 
21 #include "icing/text_classifier/lib3/utils/base/status.h"
22 #include "icing/text_classifier/lib3/utils/base/statusor.h"
23 #include "icing/index/hit/doc-hit-info.h"
24 #include "icing/index/iterator/doc-hit-info-iterator.h"
25 #include "icing/scoring/ranker.h"
26 #include "icing/scoring/scored-document-hit.h"
27 #include "icing/scoring/scorer.h"
28 #include "icing/store/document-store.h"
29 #include "icing/util/status-macros.h"
30 
31 namespace icing {
32 namespace lib {
33 
34 namespace {
35 constexpr double kDefaultScoreInDescendingOrder = 0;
36 constexpr double kDefaultScoreInAscendingOrder =
37     std::numeric_limits<double>::max();
38 }  // namespace
39 
40 libtextclassifier3::StatusOr<std::unique_ptr<ScoringProcessor>>
Create(const ScoringSpecProto & scoring_spec,const DocumentStore * document_store,const SchemaStore * schema_store)41 ScoringProcessor::Create(const ScoringSpecProto& scoring_spec,
42                          const DocumentStore* document_store,
43                          const SchemaStore* schema_store) {
44   ICING_RETURN_ERROR_IF_NULL(document_store);
45   ICING_RETURN_ERROR_IF_NULL(schema_store);
46 
47   bool is_descending_order =
48       scoring_spec.order_by() == ScoringSpecProto::Order::DESC;
49 
50   ICING_ASSIGN_OR_RETURN(
51       std::unique_ptr<Scorer> scorer,
52       Scorer::Create(scoring_spec,
53                      is_descending_order ? kDefaultScoreInDescendingOrder
54                                          : kDefaultScoreInAscendingOrder,
55                      document_store, schema_store));
56   // Using `new` to access a non-public constructor.
57   return std::unique_ptr<ScoringProcessor>(
58       new ScoringProcessor(std::move(scorer)));
59 }
60 
Score(std::unique_ptr<DocHitInfoIterator> doc_hit_info_iterator,int num_to_score,std::unordered_map<std::string,std::unique_ptr<DocHitInfoIterator>> * query_term_iterators)61 std::vector<ScoredDocumentHit> ScoringProcessor::Score(
62     std::unique_ptr<DocHitInfoIterator> doc_hit_info_iterator, int num_to_score,
63     std::unordered_map<std::string, std::unique_ptr<DocHitInfoIterator>>*
64         query_term_iterators) {
65   std::vector<ScoredDocumentHit> scored_document_hits;
66   scorer_->PrepareToScore(query_term_iterators);
67 
68   while (doc_hit_info_iterator->Advance().ok() && num_to_score-- > 0) {
69     const DocHitInfo& doc_hit_info = doc_hit_info_iterator->doc_hit_info();
70     // TODO(b/144955274) Calculate hit demotion factor from HitScore
71     double hit_demotion_factor = 1.0;
72     // The final score of the doc_hit_info = score of doc * demotion factor of
73     // hit.
74     double score =
75         scorer_->GetScore(doc_hit_info, doc_hit_info_iterator.get()) *
76         hit_demotion_factor;
77     scored_document_hits.emplace_back(
78         doc_hit_info.document_id(), doc_hit_info.hit_section_ids_mask(), score);
79   }
80 
81   return scored_document_hits;
82 }
83 
84 }  // namespace lib
85 }  // namespace icing
86