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.entity;
17  
18  import org.codelibs.fess.mylasta.direction.FessConfig;
19  import org.codelibs.fess.util.ComponentUtil;
20  
21  /**
22   * Entity class containing highlighting configuration for search result text highlighting.
23   * This class manages highlighting parameters such as fragment size, number of fragments,
24   * and highlighting type for displaying search query matches in result snippets.
25   */
26  public class HighlightInfo {
27      /** The highlighting type (e.g., plain, html). */
28      private String type;
29      /** The size of each highlighted fragment in characters. */
30      private int fragmentSize;
31      /** The maximum number of highlighted fragments to return. */
32      private int numOfFragments;
33      /** The offset for fragment positioning. */
34      private int fragmentOffset;
35      /** Custom pre-tags for highlighting (null uses OpenSearch defaults). */
36      private String[] preTags;
37      /** Custom post-tags for highlighting (null uses OpenSearch defaults). */
38      private String[] postTags;
39  
40      /**
41       * Default constructor that initializes highlighting settings from Fess configuration.
42       * Loads default values for type, fragment size, number of fragments, and fragment offset.
43       */
44      public HighlightInfo() {
45          final FessConfig fessConfig = ComponentUtil.getFessConfig();
46          type = fessConfig.getQueryHighlightType();
47          fragmentSize = fessConfig.getQueryHighlightFragmentSizeAsInteger();
48          numOfFragments = fessConfig.getQueryHighlightNumberOfFragmentsAsInteger();
49          fragmentOffset = fessConfig.getQueryHighlightFragmentOffsetAsInteger();
50      }
51  
52      /**
53       * Gets the highlighting type.
54       *
55       * @return the highlighting type
56       */
57      public String getType() {
58          return type;
59      }
60  
61      /**
62       * Sets the highlighting type with fluent interface.
63       *
64       * @param type the highlighting type to set
65       * @return this HighlightInfo instance for method chaining
66       */
67      public HighlightInfo type(final String type) {
68          this.type = type;
69          return this;
70      }
71  
72      /**
73       * Gets the fragment size.
74       *
75       * @return the fragment size in characters
76       */
77      public int getFragmentSize() {
78          return fragmentSize;
79      }
80  
81      /**
82       * Sets the fragment size with fluent interface.
83       *
84       * @param fragmentSize the fragment size in characters
85       * @return this HighlightInfo instance for method chaining
86       */
87      public HighlightInfo fragmentSize(final int fragmentSize) {
88          this.fragmentSize = fragmentSize;
89          return this;
90      }
91  
92      /**
93       * Gets the number of fragments.
94       *
95       * @return the maximum number of highlighted fragments
96       */
97      public int getNumOfFragments() {
98          return numOfFragments;
99      }
100 
101     /**
102      * Sets the number of fragments with fluent interface.
103      *
104      * @param numOfFragments the maximum number of highlighted fragments
105      * @return this HighlightInfo instance for method chaining
106      */
107     public HighlightInfo numOfFragments(final int numOfFragments) {
108         this.numOfFragments = numOfFragments;
109         return this;
110     }
111 
112     /**
113      * Gets the fragment offset.
114      *
115      * @return the fragment offset value
116      */
117     public int getFragmentOffset() {
118         return fragmentOffset;
119     }
120 
121     /**
122      * Sets the fragment offset with fluent interface.
123      *
124      * @param fragmentOffset the fragment offset value
125      * @return this HighlightInfo instance for method chaining
126      */
127     public HighlightInfo fragmentOffset(final int fragmentOffset) {
128         this.fragmentOffset = fragmentOffset;
129         return this;
130     }
131 
132     /**
133      * Gets the custom pre-tags for highlighting.
134      *
135      * @return the pre-tags array, or null if using defaults
136      */
137     public String[] getPreTags() {
138         return preTags;
139     }
140 
141     /**
142      * Sets the custom pre-tags for highlighting with fluent interface.
143      *
144      * @param preTags the pre-tags to set
145      * @return this HighlightInfo instance for method chaining
146      */
147     public HighlightInfo preTags(final String... preTags) {
148         this.preTags = preTags;
149         return this;
150     }
151 
152     /**
153      * Gets the custom post-tags for highlighting.
154      *
155      * @return the post-tags array, or null if using defaults
156      */
157     public String[] getPostTags() {
158         return postTags;
159     }
160 
161     /**
162      * Sets the custom post-tags for highlighting with fluent interface.
163      *
164      * @param postTags the post-tags to set
165      * @return this HighlightInfo instance for method chaining
166      */
167     public HighlightInfo postTags(final String... postTags) {
168         this.postTags = postTags;
169         return this;
170     }
171 }