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.llm;
17  
18  import java.util.Collections;
19  import java.util.List;
20  
21  /**
22   * Result of relevance evaluation for search results.
23   */
24  public class RelevanceEvaluationResult {
25  
26      private final List<String> relevantDocIds;
27      private final List<Integer> relevantIndexes;
28      private final boolean hasRelevantResults;
29  
30      private RelevanceEvaluationResult(final List<String> relevantDocIds, final List<Integer> relevantIndexes,
31              final boolean hasRelevantResults) {
32          this.relevantDocIds = relevantDocIds != null ? Collections.unmodifiableList(relevantDocIds) : Collections.emptyList();
33          this.relevantIndexes = relevantIndexes != null ? Collections.unmodifiableList(relevantIndexes) : Collections.emptyList();
34          this.hasRelevantResults = hasRelevantResults;
35      }
36  
37      /**
38       * Returns the list of relevant document IDs.
39       *
40       * @return the relevant document IDs
41       */
42      public List<String> getRelevantDocIds() {
43          return relevantDocIds;
44      }
45  
46      /**
47       * Returns the list of relevant indexes (1-based) from search results.
48       *
49       * @return the relevant indexes
50       */
51      public List<Integer> getRelevantIndexes() {
52          return relevantIndexes;
53      }
54  
55      /**
56       * Returns whether relevant results were found.
57       *
58       * @return true if relevant results exist
59       */
60      public boolean isHasRelevantResults() {
61          return hasRelevantResults;
62      }
63  
64      /**
65       * Creates a result with relevant documents found.
66       *
67       * @param relevantDocIds list of relevant document IDs
68       * @param relevantIndexes list of relevant indexes (1-based) from search results
69       * @return evaluation result
70       */
71      public static RelevanceEvaluationResult withRelevantDocs(final List<String> relevantDocIds, final List<Integer> relevantIndexes) {
72          final boolean hasRelevant = relevantDocIds != null && !relevantDocIds.isEmpty();
73          return new RelevanceEvaluationResult(relevantDocIds, relevantIndexes, hasRelevant);
74      }
75  
76      /**
77       * Creates a result with no relevant documents found.
78       *
79       * @return evaluation result with no relevant documents
80       */
81      public static RelevanceEvaluationResult noRelevantResults() {
82          return new RelevanceEvaluationResult(Collections.emptyList(), Collections.emptyList(), false);
83      }
84  
85      /**
86       * Creates a fallback result that includes all documents as relevant.
87       * Used when evaluation fails and we want to include all search results.
88       *
89       * @param allDocIds all document IDs from search results
90       * @return evaluation result with all documents marked as relevant
91       */
92      public static RelevanceEvaluationResult fallbackAllRelevant(final List<String> allDocIds) {
93          return new RelevanceEvaluationResult(allDocIds, Collections.emptyList(), !allDocIds.isEmpty());
94      }
95  
96      @Override
97      public String toString() {
98          return "RelevanceEvaluationResult{relevantDocIds=" + relevantDocIds + ", relevantIndexes=" + relevantIndexes
99                  + ", hasRelevantResults=" + hasRelevantResults + "}";
100     }
101 }