post_scorer.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Tencent is pleased to support the open source community by making wwsearch
  3. * available.
  4. *
  5. * Copyright (C) 2018-present Tencent. All Rights Reserved.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  8. * use this file except in compliance with the License. You may obtain a copy of
  9. * the License at
  10. *
  11. * https://opensource.org/licenses/Apache-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OF ANY KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations under the License.
  17. */
  18. #pragma once
  19. #include <memory>
  20. #include "document.h"
  21. #include "index_field.h"
  22. namespace wwsearch {
  23. class ScoreStrategy {
  24. public:
  25. virtual DocumentScore Score(Document *document, int match_field_id) = 0;
  26. };
  27. class ScoreStrategyFactory {
  28. public:
  29. static std::shared_ptr<ScoreStrategy> NewScoreStrategy(
  30. const std::string &pattern, size_t terms_size, FieldID target_field_id);
  31. };
  32. class StringScoreStrategy : public ScoreStrategy {
  33. private:
  34. std::string pattern_;
  35. size_t terms_size_;
  36. FieldID target_field_id_;
  37. size_t weight_{10};
  38. public:
  39. StringScoreStrategy(const std::string &pattern, size_t terms_size,
  40. FieldID target_field_id)
  41. : pattern_(pattern),
  42. terms_size_(terms_size),
  43. target_field_id_(target_field_id) {}
  44. virtual ~StringScoreStrategy() {}
  45. virtual DocumentScore Score(Document *document, int match_field_id) override;
  46. };
  47. class PostScorer {
  48. private:
  49. // std::vector<std::shared_ptr<ScoreStrategy>> *score_strategy_list_;
  50. public:
  51. PostScorer() {}
  52. ~PostScorer() {}
  53. inline bool operator()(Document *lhs, Document *rhs) const {
  54. if (lhs->Score() > rhs->Score()) {
  55. return false;
  56. } else {
  57. return true;
  58. }
  59. }
  60. };
  61. } // namespace wwsearch