View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.score;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.logging.log4j.LogManager;
22  import org.apache.logging.log4j.Logger;
23  
24  /**
25   * This class updates scores of documents.
26   */
27  public class ScoreUpdater {
28      /**
29       * Constructor.
30       */
31      public ScoreUpdater() {
32          super();
33      }
34  
35      private static final Logger logger = LogManager.getLogger(ScoreUpdater.class);
36  
37      private final List<ScoreBooster> scoreBoosterList = new ArrayList<>();
38  
39      /**
40       * Executes all score boosters.
41       * @return The result of the execution.
42       */
43      public String execute() {
44          final StringBuilder resultBuf = new StringBuilder();
45          scoreBoosterList.forEach(b -> {
46              try {
47                  final long count = b.process();
48                  resultBuf.append(b.getClass().getSimpleName()).append(" : ").append(count).append('\n');
49              } catch (final Exception e) {
50                  logger.warn("Failed to update scores: booster={}", b.getClass().getSimpleName(), e);
51                  resultBuf.append(e.getMessage()).append('\n');
52              }
53          });
54          return resultBuf.toString();
55      }
56  
57      /**
58       * Adds a score booster.
59       * @param scoreBooster The score booster.
60       */
61      protected void addScoreBooster(final ScoreBooster scoreBooster) {
62          scoreBoosterList.add(scoreBooster);
63          scoreBoosterList.sort((b1, b2) -> b2.getPriority() - b1.getPriority());
64      }
65  }